Maz*_*zzy 10 java swing jbutton joptionpane
我创造了一种JOptionPane
类型showInputDialog
.打开它时,它会显示两个按钮:OK和Cancel.当我按下Cancel按钮时,我想处理动作,但我不知道如何触及它.我怎么才能得到它?
ten*_*ica 23
例如:
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 {
}
Run Code Online (Sandbox Code Playgroud)
或者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 {
}
Run Code Online (Sandbox Code Playgroud)
编辑: showInputDialog
String response = JOptionPane.showInputDialog(owner, "Input:", "");
if ((response != null) && (response.length() > 0)) {
}
Run Code Online (Sandbox Code Playgroud)
小智 7
这是一个老问题,我是 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)
}
Run Code Online (Sandbox Code Playgroud)
showMessageDialog不应该显示两个按钮,因此您的代码或对它的解释会有问题.无论如何,如果您想让用户选择并想要检测该选择,请不要使用showMessageDialog而是使用showConfirmDialog,并返回返回的int并测试它以查看它是否为JOptoinPane.OK_OPTION.
归档时间: |
|
查看次数: |
61562 次 |
最近记录: |