[英]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.