JOptionPane Yes or No window

后端 未结 8 2170
盖世英雄少女心
盖世英雄少女心 2020-12-13 15:55

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 m

相关标签:
8条回答
  • 2020-12-13 16:27

    "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);
    }
    
    0 讨论(0)
  • 2020-12-13 16:28

    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

    0 讨论(0)
提交回复
热议问题