(Java Swing GUI)如何使方法等待按钮的输入?

时间:2023-01-27 15:47:29

EDIT: NEVER MIND EVERYTHING I SAID!!!

编辑:从来没有我想过的一切!

I just realized that there's no need to use loops at all. Each method that sets up the GUI ends right after it finishes doing so, and the entire program "waits" for me to press a button after that. I just need to set up the methods so if necessary, they can "loop".

我刚刚意识到根本不需要使用循环。设置GUI的每个方法在完成之后立即结束,并且整个程序“等待”我之后按下按钮。我只需要设置方法,如果有必要,它们可以“循环”。

I'm not sure I'm explaining it correctly, but I think it's going to work. Thanks for the help anyways!

我不确定我是否正确解释它,但我认为它会起作用。不管怎样,谢谢你的帮助!

Just one more question, to be safe: when I set up the main window in the "main()" method, the program will only exit if it hits a certain condition that I've set up (i.e the player dies), and will not "end" on its own?

还有一个问题,为了安全:当我在“main()”方法中设置主窗口时,程序只有在遇到我设置的某个条件(即玩家死亡)时才会退出,并且不会自己“结束”?


Ok, so here's my problem:

好的,所以这是我的问题:

I'm working on a very primitive RPG that uses Swing as its GUI. Since this means I need a huge load of methods to make it work, and I need to control everything with a small amount of buttons (to keep things simple), I decided to use only a set number of buttons, and create several different ActionListeners. When a certain method is called, it sets up the GUI by setting the necessary number of buttons to "visible", setting their text, and assigning the right ActionListeners to them.

我正在研究一个非常原始的RPG,它使用Swing作为GUI。因为这意味着我需要大量的方法才能使它工作,我需要用少量按钮来控制所有东西(为了简单起见),我决定只使用一定数量的按钮,并创建几个不同的ActionListeners 。调用某个方法时,它会通过将必要数量的按钮设置为“可见”,设置文本以及为它们分配正确的ActionListener来设置GUI。

Here is an example (the player fights an NPC):

这是一个例子(玩家与NPC战斗):

public int Combat(NPC e){
    game.setButtons(0, true, "Attack", 1);
    game.setButtons(1, true, "Ability", 1);
    game.setButtons(2, false, "", 1);
    game.setButtons(3, false, "", 1);
    game.setButtons(4, false, "", 1);
    game.setButtons(5, true, "Flee", 5);

    while(e.gethp() > 0 && this.hp > 0){
        if (!game.getProceed())
            break;

    }
    if (e.gethp() <= 0)
        return 1;
    else if(hp <= 0)
        return 2;
    else return 3;
    }

("setButtons":)

public void setButtons(int b, boolean v, String s, int act){
    button[b].setVisible(v);
    button[b].setText(s);
    switch(act){
        case 1: button[b].addActionListener(comlist[b]); break;
        case 2: button[b].addActionListener(maplist[b]); break;
        case 3: button[b].addActionListener(loclist[b]); break;
        case 4: button[b].addActionListener(invlist[b]); break;
        case 5: button[b].addActionListener(mainlist[b]); break;
        case 6: button[b].addActionListener(playlist[b]); break;
        case 7: button[b].addActionListener(intlist[b]); break;
        case 8: button[b].addActionListener(proc); break;
    }
}

(the "CombatListener":)

class CombatListener implements ActionListener{

    int but_num;

    public CombatListener(int n){
        but_num = n;
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        player.CombatAction(but_num);           
    }

}

(The other listener I set up is the "Proceed" listener. This sets a boolean "proceed" variable in the game to false. The idea here was this would be my "back" button, that would allow me to get back from a menu to the previous one, or in this case: flee from the battle. This is why I set it up in that while loop, but I'm not sure this is the right way to do it.)

(我设置的另一个监听器是“Proceed”监听器。这会将游戏中的布尔“继续”变量设置为false。这里的想法是这将是我的“后退”按钮,这将允许我从一个回来菜单到上一个,或者在这种情况下:逃离战斗。这就是为什么我在那个while循环中设置它,但我不确定这是正确的方法。)

I'm not sure how much code I should provide for this to make any kind of sense (the whole thing is incredibly huge). If necessary, I'll post all of it here.

我不确定我应该为此提供多少代码才能产生任何意义(整个事情非常巨大)。如有必要,我会在这里发布所有内容。

My question: How do I make sure that when my program enters the "Combat" method, then it will be able to stay there while I give commands using the buttons? The complication here is that these buttons often make the program enter an additional method, which could also set up the GUI in a different way, and when these methods complete, the GUI needs to be "reset" to work with the previous method.

我的问题:我如何确保当我的程序进入“战斗”方法时,它会在我使用按钮发出命令时保持在那里?这里的复杂之处在于这些按钮经常使程序进入另一种方法,这种方法也可以以不同的方式设置GUI,当这些方法完成时,GUI需要“重置”以使用前一种方法。

I hope all of that made sense... :)

我希望所有这一切都有道理...... :)

2 个解决方案

#1


2  

In a SWING application you have no control over the so called "main loop".

在SWING应用程序中,您无法控制所谓的“主循环”。

Therefore, in a SWING application you don't do things like "wait for user input, process, loop".

因此,在SWING应用程序中,您不会执行“等待用户输入,处理,循环”之类的操作。

SWING applications are "event driven" or "state machines" as you prefer. You must set up event handlers to deal with user input, then, given input X or Y, your application must evolve it's state accordingly.

SWING应用程序是您喜欢的“事件驱动”或“状态机”。您必须设置事件处理程序来处理用户输入,然后,给定输入X或Y,您的应用程序必须相应地改进它的状态。

For example, suppose a turn based combat game. During the player's move the application is in "player move state", during an enemy's move it is in "enemy move state". In "player move state" the application will offer interaction, then react to the user's choice, than transition to "enemy move state", then something else happens etc. After entering "player move state" and before the user's input there is no method active, no loop blocked on read or write. The application is "idle" waiting for some handler to become active.

例如,假设一个基于回合的战斗游戏。在玩家移动期间,应用程序处于“玩家移动状态”,在敌人移动期间它处于“敌人移动状态”。在“玩家移动状态”中,应用程序将提供交互,然后对用户的选择作出反应,而不是转换为“敌人移动状态”,然后发生其他事情等。进入“玩家移动状态”之后和用户输入之前没有方法活动,读取或写入时没有阻塞循环。应用程序“空闲”等待某个处理程序变为活动状态。

#2


0  

If your method is invoked via a button press, send that event to your ActionListener where you should have set your conditions to show the desired buttons when that event is triggered.

如果通过按下按钮调用方法,请将该事件发送到ActionListener,您应该设置条件以在触发该事件时显示所需的按钮。

void actionPerformed(ActionEvent e) {
    btn1.setVisible(true);
    btn2.setVisible(true);
    btn3.setVisible(false);
    }
}

#1


2  

In a SWING application you have no control over the so called "main loop".

在SWING应用程序中,您无法控制所谓的“主循环”。

Therefore, in a SWING application you don't do things like "wait for user input, process, loop".

因此,在SWING应用程序中,您不会执行“等待用户输入,处理,循环”之类的操作。

SWING applications are "event driven" or "state machines" as you prefer. You must set up event handlers to deal with user input, then, given input X or Y, your application must evolve it's state accordingly.

SWING应用程序是您喜欢的“事件驱动”或“状态机”。您必须设置事件处理程序来处理用户输入,然后,给定输入X或Y,您的应用程序必须相应地改进它的状态。

For example, suppose a turn based combat game. During the player's move the application is in "player move state", during an enemy's move it is in "enemy move state". In "player move state" the application will offer interaction, then react to the user's choice, than transition to "enemy move state", then something else happens etc. After entering "player move state" and before the user's input there is no method active, no loop blocked on read or write. The application is "idle" waiting for some handler to become active.

例如,假设一个基于回合的战斗游戏。在玩家移动期间,应用程序处于“玩家移动状态”,在敌人移动期间它处于“敌人移动状态”。在“玩家移动状态”中,应用程序将提供交互,然后对用户的选择作出反应,而不是转换为“敌人移动状态”,然后发生其他事情等。进入“玩家移动状态”之后和用户输入之前没有方法活动,读取或写入时没有阻塞循环。应用程序“空闲”等待某个处理程序变为活动状态。

#2


0  

If your method is invoked via a button press, send that event to your ActionListener where you should have set your conditions to show the desired buttons when that event is triggered.

如果通过按下按钮调用方法,请将该事件发送到ActionListener,您应该设置条件以在触发该事件时显示所需的按钮。

void actionPerformed(ActionEvent e) {
    btn1.setVisible(true);
    btn2.setVisible(true);
    btn3.setVisible(false);
    }
}