有几次我因为建议使用以下方法而受到批评:
在Swing组件上.当我想在显示的组件之间定义比例时,我没有看到任何替代它们的用法.我被告知这个:
对于布局,答案总是相同的:使用合适的LayoutManager
我在网上搜索了一下,但我没有找到任何关于这个主题的综合分析.所以我有以下问题:
我有一个来自的小组JPanel.我有一个自定义控件派生自JLabel.我试图将这个自定义JLabel放在我的面板上.
我知道这样做的唯一方法就是使用一个null布局(setLayout(null)),然后计算自定义JLabel的setLocation()点,使它在正确的位置.
JLabel在这个应用程序中,自定义从一个面板物理移动到此面板,我相信之前设置的位置setLocation正在影响事物.但是,当我将其设置为(0,0)时,组件会进入左上角.
BorderLayout不起作用,因为当只提供并放入一个组件时BorderLayout.CENTER,中央部分会扩展以填充所有空间.
我使用的切割和从其他网站粘贴示例BoxLayout及component.setAlignmentX(Component.CENTER_ALIGNMENT).这也不起作用.
另一个答案提到了覆盖小组的getInset()功能(我认为这就是所谓的),但事实证明这是一个死胡同.
到目前为止,我正在使用带有GridBagLayout布局的面板,GridBagConstraints当我将自定义JLabel插入面板时,我会包含一个对象.但这是低效的.有没有更好的方法让JLabel我的中心JPanel?
在被动渲染模式中,可以使用KeyListener和ActionListener接口来处理来自用户的事件.
全屏模式下事件处理的正确方法是什么?请扩展此骨架,提供鼠标点击和按键事件的实现,请不要膨胀您的示例(示例启动全屏独占模式,使用Timer更新窗口中的图形):
import java.applet.Applet;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.Timer;
public class applet extends Applet
{
Timer timer;
JFrame frame;
DisplayMode[] displayModes = new DisplayMode[] {
new DisplayMode(1280, 800, 32, 60)
};
BufferStrategy bufferStrategy;
Rectangle bounds;
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public void init()
{
GraphicsEnvironment …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个刽子手游戏,到目前为止,它正在进行中,但布局设计似乎并没有落实到位!字母表应该以FlowLayoutHangman图片顶部的顺序结束,底部有"重启","帮助","添加新单词"和"退出"按钮!我究竟做错了什么?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Hangman extends JFrame
{
int i = 0;
static JPanel panel;
static JPanel panel2;
static JPanel panel3;
public Hangman()
{
JButton[] buttons = new JButton[26];
panel = new JPanel(new FlowLayout());
panel2 = new JPanel();
panel3 = new JPanel();
JButton btnRestart = new JButton("Restart");
btnRestart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
}
});
JButton btnNewWord = new JButton("Add New Word");
btnNewWord.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try …Run Code Online (Sandbox Code Playgroud) 我正在做一些练习来理解Java和Swing API.为什么在Disegno构造函数中有nullPointerException?我想打印两个矩形的坐标,但它们似乎没有初始化.
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Disegno extends JFrame{
Disegno(){
this.setSize(500, 500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
MyPanel aba = new MyPanel();
this.setContentPane(aba);
this.setVisible(true);
System.out.println(aba.rect.blue.x + "-" + aba.rect.blue.y);
System.out.println(aba.rect.yellow.x + "-" + aba.rect.yellow.y);
}
public static void main(String[] args){
new Disegno();
}
}
class MyPanel extends JPanel{
JPanel up, down;
RectArea rect;
MyPanel(){
this.setLayout(new BorderLayout());
up = new JPanel();
this.add(up, BorderLayout.NORTH);
up.setBackground(Color.red);
up.setVisible(true);
down = new JPanel();
down.setBackground(Color.green);
this.add(down, BorderLayout.SOUTH);
down.setVisible(true);
rect = new RectArea();
this.add(rect, BorderLayout.CENTER);
this.setVisible(true);
} …Run Code Online (Sandbox Code Playgroud)