JOptionPane是或否窗口

时间:2011-12-06 08:05:43

标签: java swing return-value joptionpane

我正在尝试使用“是”或“否”按钮创建消息。然后会出现一个窗口,其中包含某条消息,该消息取决于用户是单击是还是否。

这是我的代码:

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);
    }
}

现在它会打印HELLO,无论你是否按否。当用户选择否时,如何让它显示GOODBYE?

8 个答案:

答案 0 :(得分:64)

“if(true)”将永远为真,它永远不会成为其他人。如果您希望它正常工作,您必须这样做:

    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);
        }

答案 1 :(得分:8)

您总是在检查是否存在真实情况,因此您的信息将始终显示。

您应该将if (true)语句替换为if ( n == JOptionPane.YES_OPTION)

  

当其中一个showXxxDialog方法返回一个整数时,可能   值是:

     

YES_OPTION NO_OPTION CANCEL_OPTION OK_OPTION CLOSED_OPTION

来自here

答案 2 :(得分:4)

您可以使用以下方法修复它:

if(n == JOptionPane.YES_OPTION)
{
    JOptionPane.showMessageDialog(null, "HELLO");
}
else
{
    JOptionPane.showMessageDialog(null, "GOODBYE");
}

答案 3 :(得分:3)

是和否消息的代码

      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);

答案 4 :(得分:3)

你可以更简单地做到这一点:

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 :(得分:2)

您正在撰写if(true),因此它将始终显示“Hello”消息。

您应该根据返回的n的价值做出决定。

答案 6 :(得分:1)

为更好地了解其工作原理!

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;
    }

答案 7 :(得分:0)

这些方面的东西......

   //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);

您可能还想查看DialogDemo