Java 正在等待按下JButton
我是Java编程新手 我想为我的游戏制作一份菜单。我使用Swing作为GUI。我已经制作了所需的框架 程序从主菜单开始,主菜单应等待按下JButton。我已经为每个JButton制作了ActionListener,但我无法让程序等待按下其中一个按钮 我希望我的菜单如下所示: 如果你按下newGameB,菜单框关闭,游戏框开始动作。如果按下exitB,程序应自行关闭 我已经读过关于使用wait()和notifyAll()的内容,但我无法使程序正常工作Java 正在等待按下JButton,java,swing,jbutton,Java,Swing,Jbutton,我是Java编程新手 我想为我的游戏制作一份菜单。我使用Swing作为GUI。我已经制作了所需的框架 程序从主菜单开始,主菜单应等待按下JButton。我已经为每个JButton制作了ActionListener,但我无法让程序等待按下其中一个按钮 我希望我的菜单如下所示: 如果你按下newGameB,菜单框关闭,游戏框开始动作。如果按下exitB,程序应自行关闭 我已经读过关于使用wait()和notifyAll()的内容,但我无法使程序正常工作 public class Main { pub
public class Main {
public enum CurrentState{
MENU, GAME, EXIT
}
public static void main(String[] args) {
CurrentState currentState = CurrentState.MENU;
boolean isRunning = true;
while(isRunning) {
switch(currentState) {
case MENU:
Menu menu = new Menu();
menu.runMenu(currentState);
break;
case EXIT:
isRunning = false;
break;
case GAME:
System.out.println("game");
PlayGame game = new PlayGame();
game.runGame(currentState);
break;
default:
System.exit(0);
break;
}
}
}
}
}
}
}您可以使用易失性布尔变量,该变量在JButton发生ActionEvent时设置为true。鉴于此,您可以在代码中有一个部分,其中while循环将一直执行,直到按下其中一个按钮为止
此外,您可以使用多个布尔变量来检测按下了哪个按钮,并基于此调用某些方法。
wait()
和notifyAll()
绝对不是您想要使用的。不要在main
方法中循环;创建UI并显示它。然后,您将编写代码来响应UI中的事件。这是一个非常好的关于创建Swing应用程序的简明教程,它将向您展示创建Swing应用程序的正确方法:Oracle的教程:。跳过Netbeans部分。我真的很好奇,如果你在你访问过的网站上发布链接,告诉你如何使用Netbeans编写游戏,我将不胜感激Swing@Abra这个游戏只是一个基于文本的游戏,你读一个故事,然后在两个答案选项中进行选择,所以它不需要太多的图形。我现在只使用jlabel和jbutton。所以我只读过这类东西,比如如何制作框架。
public class MenuFrame extends Frame {
CurrentState currentState;
JPanel panel = new JPanel();
FlowLayout layout = new FlowLayout();
JLabel emptyLabel = new JLabel();
JButton continueGameB = new JButton("Folytatás");
JButton newGameB = new JButton("Új játék");
JButton scoresB = new JButton("Eredmények");
JButton exitB = new JButton("Kilépés");
public void setCurrentState(CurrentState state) {
currentState = state;
}
public MenuFrame(CurrentState currentState) {
this.currentState = currentState;
frame.getContentPane().add(emptyLabel);
emptyLabel.setPreferredSize(new Dimension(175, 100));
panel.setLayout(layout);
panel.setBackground(background);
panel.add(continueGameB);
panel.add(newGameB);
panel.add(scoresB);
panel.add(exitB);
frame.add(panel, BorderLayout.SOUTH);
}
public void runFrame() {
openFrame();
exitB.addActionListener(new ButtonListener());
newGameB.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exitB) {
setCurrentState(CurrentState.EXIT);
}
else if(e.getSource() == newGameB) {
setCurrentState(CurrentState.GAME);
}
}
}
public class Menu {
MenuFrame menuFrame;
public void runMenu(CurrentState currentState) {
this.menuFrame = new MenuFrame(currentState);
menuFrame.runFrame();
}
public class Frame {
JFrame frame;
Color background = new Color(255, 255, 255, 255);
public Frame() {
frame = new JFrame("Progházi");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setUndecorated(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width, screenSize.height);
frame.getContentPane().setBackground(background);
}
public void openFrame() {
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public void closeFrame() {
frame.dispose();
}