【问题标题】:JOptionPane showConfirmDialog if else statement not workingJOptionPane showConfirmDialog if else 语句不起作用
【发布时间】:2020-04-14 20:48:29
【问题描述】:

无论我做什么,NO 部分都不起作用,我尝试了另一个 if,else if,else,brackets ,没有括号。无论我点击什么,它都会打印你好。如果我按 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);
    }
}

【问题讨论】:

  • 你没有捕捉到JOptionPane.showConfirmDialog的返回值
  • 我该怎么做?我是编程新手

标签: java swing if-statement joptionpane


【解决方案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);
        } 
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多