简体   繁体   English

Java-JButton没有出现在JFrame中

[英]Java - Jbuttons not appearing in JFrame

I want to make a title screen for my game which involves a title screen. 我想为我的游戏制作一个标题屏幕,其中涉及一个标题屏幕。 Having just a jframe works and lets me use absolute positioning(i know its not great but its what i intend to use unless i can work out how to reposition jbuttons on a layout). 只有一个jframe可以工作,并且可以让我使用绝对定位(我知道它不太好,但是我打算使用什么,除非我能弄清楚如何在布局上重新定位jbutton)。 However, when i add a jpanel so that i can repaint and revalidate, the buttons dont show up and i dont know why. 但是,当我添加一个jpanel以便我可以重新粉刷和重新验证时,按钮不显示,我也不知道为什么。 In summary, i want to know why my jbuttons arent showing and how i could fix it(and it would be GREAT if you could tell me how to reposition jbuttons on a flowlayout or any other layout!!!) This is my code. 总而言之,我想知道为什么我的jbuttons arent不能显示以及如何解决(如果您能告诉我如何在流布局或任何其他布局上重新放置jbuttons,那将是非常棒的!)这是我的代码。 Thanks! 谢谢! :) :)

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();
    }

}

You are setting the JFrame visible before adding your components, so it makes sense that they won't show. 您在添加组件之前将JFrame设置为可见,因此它们不会显示是有道理的。 solution: set visible after adding components. 解决方案:添加组件设置可见。

Other issues: 其他事宜:

  • Over use of static: Your components are static and none of them should be since overuse of static makes for inflexible classes that are difficult to re-use. 过度使用静态:您的组件是静态的,不应使用任何组件,因为过度使用静态会导致难以重用的僵化类。
  • Use of null layout and setBounds(...) : This leads to physically inflexible GUI's that look terrible on all platforms but your own and that are hard if not impossible to enhance or change. 使用null布局和setBounds(...) :这导致物理上不灵活的GUI,在您自己的平台上,在所有平台上看起来都很糟糕,并且即使不是不可能的增强或更改也很难。 Instead learn about and use the layout the managers. 而是了解并使用管理器的布局。
  • Removing and adding components: You're better off using a CardLayout and swapping JPanels. 删除和添加组件:最好使用CardLayout并交换JPanels。

Addendum: 附录:
You comment: 您评论:

Didn't work, still doesn't show the JBUttons 没有工作,仍然没有显示JBUttons

Yep, you shot yourself in the foot with the null layout. 是的,您以空布局拍摄自己的脚。 Because your JFrame's layout is null, the JPanel defaults to a size of [0, 0]. 由于JFrame的布局为null,因此JPanel的默认大小为[0,0]。 Solution: don't have it use a null layout . 解决方案: 不要使用null布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM