[英]Java - Jbuttons not appearing in JFrame
我想为我的游戏制作一个标题屏幕,其中涉及一个标题屏幕。 只有一个jframe可以工作,并且可以让我使用绝对定位(我知道它不太好,但是我打算使用什么,除非我能弄清楚如何在布局上重新定位jbutton)。 但是,当我添加一个jpanel以便我可以重新粉刷和重新验证时,按钮不显示,我也不知道为什么。 总而言之,我想知道为什么我的jbuttons arent不能显示以及如何解决(如果您能告诉我如何在流布局或任何其他布局上重新放置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();
}
}
您在添加组件之前将JFrame设置为可见,因此它们不会显示是有道理的。 解决方案:添加组件后设置可见。
其他事宜:
setBounds(...)
:这导致物理上不灵活的GUI,在您自己的平台上,在所有平台上看起来都很糟糕,并且即使不是不可能的增强或更改也很难。 而是了解并使用管理器的布局。 附录:
您评论:
没有工作,仍然没有显示JBUttons
是的,您以空布局拍摄自己的脚。 由于JFrame的布局为null,因此JPanel的默认大小为[0,0]。 解决方案: 不要使用null布局 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.