我想为我的游戏制作一个涉及标题画面的标题画面。只有一个jframe工作,让我使用绝对定位(我知道它不是很好,但我打算使用,除非我可以弄清楚如何重新定位布局上的jbuttons)。但是,当我添加一个jpanel,以便我可以重新绘制和重新验证时,按钮不显示,我不知道为什么。总之,我想知道为什么我的jbuttons没有显示以及我如何解决它(如果你能告诉我如何在flowlayout或任何其他布局上重新定位jbuttons,那将是很棒的!)这是我的代码。谢谢! :)
public class Launcher extends JFrame{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Field of the Dead Pre-Release 0.1";
protected static JButton b3;
protected static JButton b4;
protected static JButton b5;
protected static JButton b6;
protected static JButton b7;
protected static JButton b8;
protected static JButton b9;
protected static JButton b10;
protected static JButton b11;
JFrame frame = new JFrame("Field of the Dead");
JPanel panel = new JPanel();
public Launcher() {
JFrame.setDefaultLookAndFeelDecorated(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
frame.add(panel);
panel.setLayout(null);
b3 = new JButton("Quit Game");
b3.setBounds(600, 500, 150, 30);
panel.add(b3);
b4 = new JButton("Options");
b4.setBounds(600, 450, 150, 30);
panel.add(b4);
b5 = new JButton("Officer Store");
b5.setBounds(600, 50, 150, 30);
panel.add(b5);
b6 = new JButton("Offline Campaign");
b6.setBounds(50, 50, 150, 30);
panel.add(b6);
b7 = new JButton("Offline Training");
b7.setBounds(50, 200, 150, 30);
panel.add(b7);
b8 = new JButton("Online Campaign");
b8.setBounds(50, 350, 150, 30);
panel.add(b8);
b9 = new JButton("Online PvP Games");
b9.setBounds(50, 500, 150, 30);
panel.add(b9);
b10 = new JButton("Bonus Store");
b10.setBounds(600, 100, 150, 30);
panel.add(b10);
b11 = new JButton("Screen Resolution");
b3HandlerClass b3handler = new b3HandlerClass();
b4HandlerClass b4handler = new b4HandlerClass();
b5HandlerClass b5handler = new b5HandlerClass();
b6HandlerClass b6handler = new b6HandlerClass();
b7HandlerClass b7handler = new b7HandlerClass();
b8HandlerClass b8handler = new b8HandlerClass();
b9HandlerClass b9handler = new b9HandlerClass();
b3.addActionListener(b3handler);
b4.addActionListener(b4handler);
b5.addActionListener(b5handler);
b6.addActionListener(b6handler);
b7.addActionListener((ActionListener) b7handler);
b8.addActionListener(b8handler);
b9.addActionListener(b9handler);
}
private class b3HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
private class b4HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event) {
panel.remove(b3);
panel.remove(b4);
panel.remove(b5);
panel.remove(b6);
panel.remove(b7);
panel.remove(b8);
panel.remove(b9);
panel.remove(b10);
b11 = new JButton("Screen Resolution");
panel.add(b11);
panel.revalidate();
panel.repaint();
}
}
答案 0 :(得分:3)
您在添加组件之前将JFrame设置为可见,因此它们将无法显示。解决方案:在添加组件后设置可见。
其他问题:
setBounds(...)
:这导致物理上不灵活的GUI在所有平台上看起来都很糟糕,但是你自己的并且即使不是不可能增强或改变也很难。而是了解并使用经理的布局。附录:
你评论:
是的,你用空布局拍摄自己的脚。因为JFrame的布局为null,所以JPanel的默认大小为[0,0]。解决方案:没有使用空布局。没有用,仍然没有显示JBUttons