Car*_*son 2 java user-interface swing
所以我有这个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);
}
Run Code Online (Sandbox Code Playgroud)
这只是创建一个项目列表和一个按钮.单击该按钮时,它将获取所选项目并将其返回到控制器然后进行处理(对于我们的项目,它会更改玩家口袋妖怪).
*/
@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
}
}
Run Code Online (Sandbox Code Playgroud)
setCurrentPokemon与控制器没有任何关系.我只是想确保它现在能够得到选择.但是我在等待选择的其余代码时遇到问题.
我认为Swing和Java的输入应该暂停并等待输入,然后继续使用其余的代码.但是,现在它运行打开选择菜单,但然后在控制器中将选定的宠物小猫设置为null.我想添加一个while循环来等待并解决这个问题,但我觉得有一个更容易的方法来构建Swing.
有没有办法,所以我可以让我的其余代码等到选择按钮并处理操作?
提前致谢.
用一个JOptionPane.它将为您构建模态对话框和按钮.模态对话框将停止执行,直到关闭为止.
有关更多信息和工作示例,请阅读有关如何创建对话框的Swing教程中的部分.
add("Center", centerPanel);
Run Code Online (Sandbox Code Playgroud)
不要使用"魔法"值.API将定义应使用的值.这也不是向Container添加组件的方法.相反,你应该使用:
add(centerPanel, BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)