【发布时间】:2015-06-30 20:21:56
【问题描述】:
有没有一种方法可以为JOptionPane.showInputDialog() 中的“确定”和“取消”按钮创建单独的条件语句?
我想这样做的原因是因为我不希望将空字符串或仅充满空格的字符串设置为s。
当我在空白文本字段中单击“确定”或单击“取消”时,s 将设置为 null。
有没有一种方法可以为确定和取消按钮选择不同的选项,如果我单击取消,窗口将关闭,如果我单击确定,代码将继续执行条件语句?
public boolean stringIsNullOrOnlyWhiteSpaces(String s) {
//checks if string is null or if string only contains white spaces
}
private void nameOfString() {
String s = JOptionPane.showInputDialog(this, "Enter String Name");
if (!stringIsNullOrOnlyWhiteSpaces(s)) {
// do action
}
else { // if "ok" were to be clicked and not "cancel"
JOptionPane.showMessageDialog(this, "String cannot be empty");
nameOfString();
}
}
【问题讨论】:
-
你有没有经过this
-
在您的示例代码中,您可以在输入对话框的顶部有一个消息对话框。如果您创建自己的 JDialog,则可以在同一个对话框中包含 JTextField 和 JLabel 消息。由于您正在创建 JDialog,因此您可以控制左键单击“确定”和“取消”按钮时发生的情况。
标签: java joptionpane