我创建了一个JOptionPane
类型showInputDialog
。当它打开它时,它会显示两个按钮:OK和Cancel。当我按下按钮时,我想处理该动作Cancel,但我不知道如何达到它。我怎么才能得到它?
问问题
76111 次
6 回答
25
例如:
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)) {
}
于 2012-07-15T17:55:04.647 回答
8
这是一个老问题,我是 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)
}
于 2014-09-21T16:12:01.247 回答
6
showMessageDialog 不应显示两个按钮,因此您的代码或您对它的解释有问题。无论如何,如果您想给用户一个选择并想要检测该选择,请不要使用 showMessageDialog 而是使用 showConfirmDialog,并获取返回的 int 并测试它是否为 JOptoinPane.OK_OPTION。
于 2012-07-15T17:54:36.783 回答
0
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;
}
}
}
}
于 2017-12-03T04:59:39.153 回答
0
你可以这样做:
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);
}
于 2018-06-03T23:47:46.983 回答
-1
这可能是你的答案:
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;
}
}
}
}
于 2015-03-25T16:52:42.873 回答