JOptionPane showConfirmDialog,如果else语句不起作用

问题描述 投票:0回答:1

[无论我做什么,NO部分永远都不会起作用,我已经尝试了另一个if,else if,else,括号,没有括号。无论我单击什么,它都只会打个招呼。如果我按NO,我希望系统退出

import javax.swing.*;

class ok
{
    public static void main(String[] args)
    { 
        int dialogButton = JOptionPane.YES_NO_OPTION;
        JOptionPane.showConfirmDialog (null,"Can you come at my house on 18th??" ," SERIOUS QUESTION",dialogButton,3);
        if (dialogButton == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "HELLO");
        } else if (dialogButton == JOptionPane.NO_OPTION)
            System.exit(0);
    }
}
java swing if-statement joptionpane
1个回答
1
投票

您没有捕获JOptionPane.showConfirmDialog的返回值。像这样的东西:

class ok
{
    public static void main(String[] args)
    { 
        int options = JOptionPane.YES_NO_OPTION;
        int result = JOptionPane.showConfirmDialog(null, "Can you come at my house on 18th?" ,"SERIOUS QUESTION", options, 3);
        if (result == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null, "HELLO");
        } else if (result == JOptionPane.NO_OPTION) {
            System.exit(0);
        } 
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.