JOptionPane是或否窗口

编程入门 行业动态 更新时间:2024-10-06 12:27:54
本文介绍了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是或否窗口

本文发布于:2023-11-28 03:13:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   JOptionPane

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!