gpt4 book ai didi

java - JOptionPane showConfirmDialog if else 语句不起作用

转载 作者:行者123 更新时间:2023-12-02 08:43:46 25 4
gpt4 key购买 nike

无论我做什么,“NO”部分都不起作用,我尝试过另一个 if、else if、else、括号、无括号。无论我点击什么,它都只会打印 hello。如果我按“否”,我希望系统退出

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的返回值。像这样的事情:

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

关于java - JOptionPane showConfirmDialog if else 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61216971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com