基本上我有代码显示一个对话框,允许用户在2到4个玩家之间进行选择。它工作正常,但我希望能够控制'确定'和'取消'按钮做什么,但我无法弄清楚如何访问它们。单击“确定”按钮时,我想调用一个方法,如果单击“取消”,我将终止程序(System.exit(0))。另外,如何检查用户是否单击对话框顶角的“x”?
public void numPlayersDialog()
{
Object[] possibilities = {"Two Players", "Three Players", "Four Players"};
String s = (String)JOptionPane.showInputDialog(
null,
"Enter the number of Players\n",
"Initial Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"Two Players");
if(s.equals("Two Players"))
{
setNumOfPlayers(2);
}
else if (s.equals("Three Players"))
{
setNumOfPlayers(3);
}
else
{
setNumOfPlayers(4);
}
}
我对Java中的GUI内容很新,所以任何帮助都会受到赞赏。感谢
答案 0 :(得分:0)
答案 1 :(得分:0)
你可以这样做:
if (s == null) {/////////////mean you click on the Cancel button
System.exit(0);
} else {////////////mean you click on OK button
if (s.equals("Two Players")) {
setNumOfPlayers(2);
} else if (s.equals("Three Players")) {
setNumOfPlayers(3);
} else {
setNumOfPlayers(4);
}
}