英文:
Wait for button to be pressed JAVA GUI
问题
目前,我正在将一个基于文本的程序重写为具有图形用户界面(GUI)的程序。我遇到的问题之一是我希望程序在满足特定条件之前等待。这个条件可以通过用户点击“Walk”按钮来满足,直到player.walked属性等于5为止。在使用基于文本的界面时,这非常简单,只需要使用一个while循环,在其中使用一个输入函数。
while (player.getWalked() < 5) {
//通过终端等待用户输入。
}
然而,当使用GUI并且希望采用模型-视图-控制器(即将游戏机制和用户界面分开)的方法时,情况变得相当困难。在尝试实现GUI后,我的程序因为while循环现在为空而出现冻结。我将尝试在下面进行解释,但这可能会有些混乱。如果这显得不太专业,我为此道歉。
World类:
public static void play(Player player) throws FileNotFoundException, IOException, ClassNotFoundException{
welcome(player);
while (player.getWalked() < 5) {
//等待条件满足
}
}
GUI类:
Button walk_button = new Button("Walk");
walk_button.setBounds(195, 395, 100,100);
add(walk_button);
walk_button.addActionListener((new ActionListener(){
public void actionPerformed(ActionEvent evt) {
try{
label1.setVisible(false);
label.setText(player.interaction("W"));
label.setBounds(200,50,400,100);
}
catch (FileNotFoundException e) {System.out.println(e.getMessage());} catch (IOException e) {System.out.println(e.getMessage());} catch (ClassNotFoundException e) {System.out.println(e.getMessage());}
}
}));
Player类中包含interaction
方法:
public String interaction(String input) throws FileNotFoundException, IOException, ClassNotFoundException{
if (input.equals("W")) {
return walk(World.dice(4,1));
}
}
如果有人能够找到解决这个问题的方法,我将不胜感激。最终目标是使程序保持运行(允许玩家不断按下“Walk”按钮),直到while循环被打破。
非常感谢,如果这篇内容过长、混乱或不够专业,请接受我的道歉。
英文:
At the moment I'm currently re-writing a text-based program to have a GUI. One of the problems I am experiencing is that I want the program to wait until a certain condition is met. This condition can be met through the user clicking the "Walk" button until the player.walked attribute = 5. When using a text-based interface this is quite simple, use a while loop and inside have an input function.
while (player.getWalked() < 5) {
//wait for user input via terminal through the scanner.
}
However when using a GUI and wanting to follow the approach of the Model-View Controller (i.e. keeping game mechanics and user interface stuff separate) it becomes rather difficult. After attempting to implement a GUI My program keeps freezing as the while loop is now empty. I will attempt to evidence this below but it is rather confusing. I apologise if this is unprofessional.
World Class:
public static void play(Player player) throws FileNotFoundException, IOException, ClassNotFoundException{ // play method is the centralised place for all in-game simulation and user interaction.
welcome(player);
while (player.getWalked() <5) {
}
GUI Class:
Button walk_button = new Button("Walk");
walk_button.setBounds(195, 395, 100,100);
add(walk_button);
walk_button.addActionListener((new ActionListener(){
public void actionPerformed(ActionEvent evt) {
try{
label1.setVisible(false);
label.setText(player.interaction("W"));
label.setBounds(200,50,400,100);
}
catch (FileNotFoundException e) {System.out.println(e.getMessage());} catch (IOException e) {System.out.println(e.getMessage());} catch (ClassNotFoundException e) {System.out.println(e.getMessage());}
}
}));
Player class consisting of the interaction
method:
public String interaction(String input) throws FileNotFoundException, IOException, ClassNotFoundException{
//String input = World.input("You have walked "+getWalked()+" miles so far.\nOnly "+(END_POINT - walked)+" miles until you reach the end of the town.\nPress 'w' to walk.\nPress 'c' to change weapon equipped.\nPress 's' to save and exit.");
if (input.equals("W")) {
return walk(World.dice(4,1));
}
If anyone can find a solution to this I would be much appreciated. The end goal is for the program to keep running (allow the player to keep pressing the "Walk" button) until the while loop is broken.
Thank you very much and apologies if this is rather long, confusing and unprofessional.
答案1
得分: 1
代码部分不要翻译,只返回翻译好的部分:
"空的 while 循环不是一个好主意,所以我建议在设置玩家位置的同一个方法中(就在按钮触发 actionPerformed
后面)使用 if 语句来检查玩家的位置,然后从那里继续。不过,我不能给你一个具体的实现,因为我不知道你想要做什么。
编辑:为了澄清,我的意思是这样的:
public void actionPerformed(ActionEvent evt) {
//设置玩家行走距离...
//...
if (player.getWalked() > 5) {
//在这里放置所有的逻辑,或者提取到一个方法中
}
}
顺便提一下:不要使用多个 catch 块来做相同的事情,只需使用 FileNotFoundException | ClassNotFoundException
等。"
英文:
It's not a great idea to have empty while loops, so I would suggest checking the player's position with an if-statement in the same method where it is set (right after the button trigger actionPerformed
), and then continuing from there. I can't give you a specific implementation though because I don't know what you want to do.
EDIT:To clarify, I meant something like this:
public void actionPerformed(ActionEvent evt) {
//set player's walked distance ...
//...
if (player.getWalked() > 5) {
//put all your logic here, or extract to a method
}
}
Side note: Instead of having multiple catch blocks where you do the same thing, just use FileNotFoundException | ClassNotFoundException
| etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论