等待按钮被按下JAVA GUI [英] Wait for button to be pressed JAVA GUI

查看:99
本文介绍了等待按钮被按下JAVA GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我目前正在重新编写一个具有GUI的基于文本的程序.我遇到的问题之一是我希望程序等待直到满足特定条件.用户可以通过单击步行"按钮直到player.walked属性= 5来满足此条件.使用基于文本的界面时,这非常简单,请使用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.
}

但是,当使用GUI并希望遵循Model-View Controller的方法(即,将游戏机制和用户界面内容分开)时,变得相当困难.尝试实现GUI后,由于while循环现在为空,因此我的程序保持冻结.我将在下面尝试证明这一点,但这相当令人困惑.如果不专业,我深表歉意.

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.

世界一流:

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类:

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

interaction方法组成的播放器类:

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

如果有人能找到解决方案,我将不胜感激.最终目标是使程序继续运行(允许播放器继续按步行"按钮),直到while循环中断为止.

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.

推荐答案

让while循环为空并不是一个好主意,因此,我建议您使用if语句在设置位置的同一方法中检查玩家的位置(在按钮触发actionPerformed之后),然后从那里继续.我无法为您提供具体的实现方式,因为我不知道您想做什么.

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.

为澄清起见,我的意思是这样的:

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
  }
}

侧面说明:不用在执行相同操作的地方有多个catch块,只需使用FileNotFoundException | ClassNotFoundException |等

Side note: Instead of having multiple catch blocks where you do the same thing, just use FileNotFoundException | ClassNotFoundException | etc.

这篇关于等待按钮被按下JAVA GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆