简体   繁体   English

在继续之前等待在Java Swing中按下按钮?

[英]Waiting for a button to be pressed in Java Swing before moving on?

So I have this GUI 所以我有这个GUI

public ChangePokemonView(Controller c)
{
    this.controller = c;
    this.currentBattleEnvironment = currentBattleEnvironment.getInstance();
    populateInactivePokemon(); //REMOVE LATER REMOVE LATER  REMOVE LATER  REMOVE LATER  REMOVE LATER 
    this.pokemonList = new JList(inactivePlayerPokemon);
    pokemonList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //Only one thing can be selected at a time.
    this.pokemonLabel = new JLabel("Choose your Pokemon!");
    this.confirmSelection = new JButton("Confirm Selection");
    this.confirmSelection.addActionListener(this);

    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes GUI window when close, may need to change later
    JPanel centerPanel = new JPanel(new GridLayout(3, 1));
    centerPanel.add(pokemonLabel);
    centerPanel.add(pokemonList);
    centerPanel.add(confirmSelection);

    add("Center", centerPanel);
    pack();
    setVisible(true);
}

That just creates a list of items and a button. 这只是创建一个项目列表和一个按钮。 When the button is clicked, it will get the selected item and return it to a controller to then process it (in that, for our project it changes the players pokemon). 单击该按钮时,它将获取所选项目并将其返回到控制器然后进行处理(对于我们的项目,它会更改玩家口袋妖怪)。

 */
@Override
public void actionPerformed(ActionEvent event)
{
    if (event.getSource() == confirmSelection)
    {
        this.pokemonSelected = (String) pokemonList.getSelectedValue();
        this.controller.setCurrentPokemon(this.pokemonSelected);
        //JOptionPane.showConfirmDialog(null, "You pressed: "+output); //USED FOR TESTING, THIS WILL OUPUT JUST THE NAME THAT WAS SELECTED
    }

}

The setCurrentPokemon has nothing really in it with the controller. setCurrentPokemon与控制器没有任何关系。 I am just trying to make sure it can get the selection right now. 我只是想确保它现在能够得到选择。 However I am having trouble with the rest of the code waiting till the selection is made. 但是我在等待选择的其余代码时遇到问题。

I thought Swing and input in general with Java should pause and wait for the input before moving on with the rest of the code. 我认为Swing和Java的输入应该暂停并等待输入,然后继续使用其余的代码。 However, right now it runs opens the selection menu but then sets the selected pokemon to null in the controller. 但是,现在它运行打开选择菜单,但然后在控制器中将选定的宠物小猫设置为null。 I thought about adding a while loop to kind of wait and get around this, but I feel like there is an easier way to do this built into Swing. 我想添加一个while循环来等待并解决这个问题,但我觉得有一个更容易的方法来构建Swing。

Is there a way so I can have the rest of my code wait until the button is selected and the action is handled? 有没有办法,所以我可以让我的其余代码等到选择按钮并处理操作?

Thanks in advance. 提前致谢。

Use a JOptionPane . 使用JOptionPane It will build the modal dialog and buttons for you. 它将为您构建模态对话框和按钮。 A modal dialog stops execution until it is closed. 模态对话框将停止执行,直到关闭为止。

Read the section from the Swing tutorial on How to Make Dialogs for more information and working examples. 有关更多信息和工作示例,请阅读有关如何创建对话框的Swing教程中的部分。

add("Center", centerPanel);

Don't use "magic" values. 不要使用“魔法”值。 The API will define the values that should be used. API将定义应使用的值。 That is also not the way to add components to a Container. 这也不是向Container添加组件的方法。 Instead you should be using: 相反,你应该使用:

add(centerPanel, BorderLayout.CENTER);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM