如何构建一个java主菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23034675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:10:52  来源:igfitidea点击:

How to build a java Main Menu

javaswingmenumaincardlayout

提问by Mechanic45

Obviously I am trying to make a main menu using the swing components. I understand that in order to make my menu happen, I have to utilize CardLayout, which I do in the code below:

显然,我正在尝试使用摆动组件制作主菜单。我知道为了让我的菜单出现,我必须使用CardLayout,我在下面的代码中这样做:

(Everything is imported of course)

(当然一切都是进口的)

 public class Screen extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;

int width, height;

JButton play = new JButton("play");
JButton settings = new JButton("settings");
JButton exit = new JButton("exit");
JButton mainMenu = new JButton("main menu");

CardLayout layout = new CardLayout();

JPanel panel = new JPanel();
JPanel game = new JPanel();
JPanel menu = new JPanel(); 

public Screen(int width, int height) {
    this.width = width;
    this.height = height;

    panel.setLayout(layout);        
    addButtons();

    setSize(width, height);
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(true);
    setTitle("BUILD YOUR EMPIRE");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    requestFocus();

}

private void addButtons() {

    play.addActionListener(this);
    settings.addActionListener(this);
    exit.addActionListener(this);
    mainMenu.addActionListener(this);

    //menu buttons
    menu.add(play);
    menu.add(settings);
    menu.add(exit);

    //game buttons
    game.add(mainMenu);

    //background colors
    game.setBackground(Color.MAGENTA);
    menu.setBackground(Color.GREEN);

    //adding children to parent Panel
    panel.add(menu,"Menu");
    panel.add(game,"Game");

    add(panel);
    layout.show(panel,"Menu");

}

public void actionPerformed(ActionEvent event) {

    Object source = event.getSource();

    if (source == exit) {
        System.exit(0);
    } else if (source == play) {
        layout.show(panel, "Game");
    } else if (source == settings){

    } else if (source == mainMenu){
        layout.show(panel, "Menu");
    }

    }
}

But when I run it, only the exit button works. When I hit the settings button nothing happens (as expected) but when I hit play button it crashes and gives me this error:

但是当我运行它时,只有退出按钮有效。当我点击设置按钮时没有任何反应(如预期的那样)但是当我点击播放按钮时它崩溃并给我这个错误:

 Exception in thread "main" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(Unknown Source)
at java.awt.CardLayout.show(Unknown Source)
at Screen.Buttons(Screen.java:69)
at Screen.<init>(Screen.java:31)
at Window.main(Window.java:29)

I dont understand what I am doing wrong. Any help on this will be greately appreciated thanks in advance.

我不明白我做错了什么。对此的任何帮助将不胜感激,提前致谢。

采纳答案by DSquare

Found it!

找到了!

It was a matter of wrong order:

这是一个错误的顺序问题:

Buttons();
panel.setLayout(layout);        
layout.addLayoutComponent(panel, "Menu");

is wrong because you obviously need to set up the CardLayout in the panelbefore doing anything (like show) with it. So:

是错误的,因为您显然需要在对 CardLayoutpanel进行任何操作(例如show)之前对其进行设置。所以:

panel.setLayout(layout);        
layout.addLayoutComponent(panel, "Menu");
Buttons();

Although I don't know why do you need the line:

虽然我不知道你为什么需要这条线:

layout.addLayoutComponent(panel, "Menu");

looking at the tutorialit does not mention the necessity to use it.

查看教程它没有提到使用它的必要性。

Also method names should start in lower case, I was confused for a moment thinking that Buttonswas a class. And you should use more descriptive names:

方法名称也应该以小写开头,我一度认为这Buttons是一个类,我感到很困惑。您应该使用更具描述性的名称:

addButtons()instead of Buttons()

addButtons()代替 Buttons()

gamePanelinstead of game

gamePanel代替 game

and so on.

等等。

Good job providing (not very extensive) relevant code, next time you should try adding a mainalso so we can just execute and test it :)

很好地提供(不是很广泛)相关代码,下次您应该尝试添加一个,main这样我们就可以执行和测试它:)

回答by MadProgrammer

Try swapping these lines...

尝试交换这些行...

panel.setLayout(layout);
addButtons();

Basically, when you try and showthe first screen, the CardLayoutis not associated with your panel, which is, obviously, a invalid parent

基本上,当您尝试show第一个屏幕时,CardLayout与您的 无关panel,这显然是无效的父级