我创建了一个类型为showInputDialog
的JOptionPane
。当它打开时,会显示两个按钮:OK和Cancel。我想在按下Cancel按钮时处理动作,但我不知道如何处理。怎样才能获取它?
我创建了一个类型为showInputDialog
的JOptionPane
。当它打开时,会显示两个按钮:OK和Cancel。我想在按下Cancel按钮时处理动作,但我不知道如何处理。怎样才能获取它?
int n = JOptionPane.showConfirmDialog(
frame, "Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
使用showOptionDialog
方法也可以:
Object[] options = {"Yes, please", "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (n == JOptionPane.YES_OPTION) {
} else if (n == JOptionPane.NO_OPTION) {
} else {
}
请阅读如何创建对话框以获取更多信息。
编辑:showInputDialog
String response = JOptionPane.showInputDialog(owner, "Input:", "");
if ((response != null) && (response.length() > 0)) {
}
showInputDialog
。 - tenorsax这是一个老问题,我是Java新手,所以可能有更好的解决方案,但我想知道同样的问题,也许可以帮助其他人,我的方法是检查响应是否为空。
如果用户点击“取消”,响应将为空。 如果他们点击“确定”而没有输入任何文本,则响应将为空字符串。
这对我有效:
//inputdialog
JOptionPane inpOption = new JOptionPane();
//Shows a inputdialog
String strDialogResponse = inpOption.showInputDialog("Enter a number: ");
//if OK is pushed then (if not strDialogResponse is null)
if (strDialogResponse != null){
(Code to do something if the user push OK)
}
//If cancel button is pressed
else{
(Code to do something if the user push Cancel)
}
showMessageDialog不应该显示两个按钮,因此你的代码或解释可能有问题。无论如何,如果你想要给用户一个选择并想要检测这个选择,不要使用showMessageDialog,而是使用showConfirmDialog,并获取返回的int值,然后测试它是否为JOptionPane.OK_OPTION。
String val = JOptionPane.showInputDialog("Value: ");
if(val == null){
// nothing goes here if yo don't want any action when canceled, or
// redirect it to a cancel page if needed
}else{
//insert your code if ok pressed
// JOptionPane return an String, as you was talking about int
int value = Integer.ParseInt(val);
}
showInputDialog
如果点击取消则返回 NULL
,即使字符串为空也会返回 String 对象。
所以你只需要测试它是否为 Null,以及它是否不为空。
package Joptionpane;
import javax.swing.JOptionPane;
public class Cancle_on_JOptionPane {
public static void main(String[] args) {
String s;
int i;
for (i=0;i<100;i++){
s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
try {
if (s.equals("")) {
JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=2;
} else {
JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100;
}
}
}
}
这可能是你的答案:
package Joptionpane;
import javax.swing.JOptionPane;
public class Cancle_on_JOptionPane {
public static void main(String[] args) {
String s;
int i;
// Set the process limit to any number, that use when you need to use for comparability and limited to get out of the program when the answer is correct.(in this case is 100)
for(i=0;i<100;i++){
s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
// Resolve process by compare answer with character to got the right answer, If it's no character it would show question box until user type answer or click cancel.
try{
if(s.equals("")) {
JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=2; // Set i=2 to repeating question box loop until user give answer.
}
else {
JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100; // Get out from question box when the variable is not empty.
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
i=100; // Get out from question box when user click cancel and showing "cancel message", You can link to another action here.
}
}
}
}