JOptionPane是或否窗口 [英] JOptionPane Yes or No window

查看:145
本文介绍了JOptionPane是或否窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用是或否按钮创建消息。然后会出现一个窗口,其中包含某条消息,该消息取决于用户是否单击是或否。

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.

这是我的代码:

public class test{
    public static void main(String[] args){

        //default icon, custom title
        int n = JOptionPane.showConfirmDialog(
            null,
            "Would you like green eggs and ham?",
            "An Inane Question",
            JOptionPane.YES_NO_OPTION);

        if(true){
            JOptionPane.showMessageDialog(null, "HELLO");
        }
        else {
            JOptionPane.showMessageDialog(null, "GOODBYE");
        }

        System.exit(0);
    }
}

现在无论是否按是打印HELLO或者。当用户选择否时如何让它显示GOODBYE?

Right now it prints HELLO whether or not you press Yes or No. How do I get it to show GOODBYE when the user chooses No?

推荐答案

if(true)将永远是真的,它永远不会成为别的。如果你想让它正常工作,你必须这样做:

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

这篇关于JOptionPane是或否窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆