JOptionPane Yes or No window

后端 未结 8 2204
盖世英雄少女心
盖世英雄少女心 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:13

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

提交回复
热议问题