可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to create a message with a Yes or No button. Then a window will appear with a certain message that depends on if the user clicked Yes or No.
Here is my code:
public class test{
public static void main(String[] args){
//default icon, custom title
int n = JOptionPane.showConfirmDialog(
null,
"Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
if(true){
JOptionPane.showMessageDialog(null, "HELLO");
}
else {
JOptionPane.showMessageDialog(null, "GOODBYE");
}
System.exit(0);
}
}
Right now it prints HELLO whether or not you press Yes or No. How do I get it to show GOODBYE when the user chooses No?
回答1:
"if(true)" will always be true and it will never make it to the else. If you want it to work correctly you have to do this:
int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "HELLO");
}
else {
JOptionPane.showMessageDialog(null, "GOODBYE");
System.exit(0);
}
回答2:
You are always checking for a true condition, hence your message will always show.
You should replace your if (true)
statement with if ( n == JOptionPane.YES_OPTION)
When one of the showXxxDialog methods returns an integer, the possible
values are:
YES_OPTION NO_OPTION CANCEL_OPTION OK_OPTION CLOSED_OPTION
From here
回答3:
You can fix it with this:
if(n == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "HELLO");
}
else
{
JOptionPane.showMessageDialog(null, "GOODBYE");
}
回答4:
You can do this a whole simpler:
int test = JOptionPane.showConfirmDialog(null, "Would you like green eggs and ham?", "An insane question!");
switch(test) {
case 0: JOptionPane.showMessageDialog(null, "HELLO!"); //Yes option
case 1: JOptionPane.showMessageDialog(null, "GOODBYE!"); //No option
case 2: JOptionPane.showMessageDialog(null, "GOODBYE!"); //Cancel option
}
回答5:
You are writing if(true)
so it will always show "Hello " message.
You should take decision on the basis of value of n
returned.
回答6:
Code for Yes and No Message
int n = JOptionPane.showConfirmDialog(
null,
"sample question?!" ,
"",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null, "Opening...");
}
else
{
JOptionPane.showMessageDialog(null, "Goodbye");
System.exit(0);
回答7:
Something along these lines ....
//default icon, custom title
int n = JOptionPane.showConfirmDialog(null,"Would you like green eggs and ham?","An Inane Question",JOptionPane.YES_NO_OPTION);
String result = "?";
switch (n) {
case JOptionPane.YES_OPTION:
result = "YES";
break;
case JOptionPane.NO_OPTION:
result = "NO";
break;
default:
;
}
System.out.println("Replace? " + result);
you may also want to look at DialogDemo
回答8:
For better understand how it works!
int n = JOptionPane.showConfirmDialog(null, "Yes No Cancel", "YesNoCancel", JOptionPane.YES_NO_CANCEL_OPTION);
if(n == 0)
{
JOptionPane.showConfirmDialog(null, "You pressed YES\n"+"Pressed value is = "+n);
}
else if(n == 1)
{
JOptionPane.showConfirmDialog(null, "You pressed NO\n"+"Pressed value is = "+n);
}
else if (n == 2)
{
JOptionPane.showConfirmDialog(null, "You pressed CANCEL\n"+"Pressed value is = "+n);
}
else if (n == -1)
{
JOptionPane.showConfirmDialog(null, "You pressed X\n"+"Pressed value is = "+n);
}
OR
int n = JOptionPane.showConfirmDialog(null, "Yes No Cancel", "YesNoCancel", JOptionPane.YES_NO_CANCEL_OPTION);
switch (n) {
case 0:
JOptionPane.showConfirmDialog(null, "You pressed YES\n"+"Pressed value is = "+n);
break;
case 1:
JOptionPane.showConfirmDialog(null, "You pressed NO\n"+"Pressed value is = "+n);
break;
case 2:
JOptionPane.showConfirmDialog(null, "You pressed CANCEL\n"+"Pressed value is = "+n);
break;
case -1:
JOptionPane.showConfirmDialog(null, "You pressed X\n"+"Pressed value is = "+n);
break;
default:
break;
}