2015-05-24 34 views
0

我正在做一个项目到大学,我有一个JOptionPane.showInputDialog询问你的名字,另一个有2个单选按钮。事情是,我可以把它留空,游戏继续。我想保持不动,直到你为它命名并选择两个单选按钮中的一个。 这意味着,它必须回答这些问题。JOptionPane.showInputDialog和JOptionPane.showInputDialog必须回应

+0

如果创建两个名字输入区域和单选按钮自己的自定义对话框,这会更容易。当单击其中一个按钮时,可以在文本框中检查非空名称条目。 – swingMan

+0

这会简单得多,但我对评估有要求,我需要将它们分开。对不起 –

回答

0

不知道我是否完全理解这个问题。也许可以帮忙吗?

String name = null; 
do{ 
    name = JOptionPane.showInputDialog(...); 
}while(name == null || name.isEmpty()); 

这将强制用户输入一个名称,如果用户点击取消或X,消息将会重新出现。 (如果name == null,name.isEmpty()将不会被调用,从而避免NullPointerException)。

如果你想在程序退出,如果名称为null,你可以试试:

String name = null; 
do{ 
    name = JOptionPane.showInputDialog(...); 
    //Exits the program if the name is null, 
    //you can also use a "break;" here and handle the exit after the loop 
    if(name == null) System.exit(0); 
}while(name.isEmpty()); 
+0

我不能这样做,因为那时我需要字符串名称,以便稍后告诉其他消息输入中的名称。如果已关闭,则不再被识别。 –

相关问题