java 将布尔值 True/False 与 JOptionPane.YES_NO_OPTION 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10176832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:56:35  来源:igfitidea点击:

Using Boolean True/False with JOptionPane.YES_NO_OPTION

javabooleanjoptionpane

提问by dwwilson66

I have a series of four yes/no choices in four separate dialog boxes, the cumulative results of which will lead to one of twelve separate links (e.g., Yes/Yes/Yes/No -> link A, Yes/No/No/Yes -> link B, etc). The branching logic uses boolean values.

我在四个单独的对话框中有一系列四个是/否选项,其累积结果将导致十二个单独链接之一(例如,是/是/是/否 - > 链接 A,是/否/否/是 -> 链接 B 等)。分支逻辑使用布尔值。

Here's what I have so far...just the first dialog box and printing the results for validation.

到目前为止,这是我所拥有的……只是第一个对话框并打印结果以进行验证。

public class OutageGuideSelector{
    public static void main(String[] args){
        boolean contactServerUp;
        boolean vistaUp;
        boolean stormOutage;
        boolean vistaCSUp;
//
        int contactServerEntry = JOptionPane.showConfirmDialog(null,
                                 "Is the contact server up", "Please select",
                                 JOptionPane.YES_NO_OPTION);
        System.out.println("result from entry " + contactServerEntry);
        if(contactServerEntry==1) 
           contactServerUp = true;
        else
          if(contactServerEntry==0)
             contactServerUp = false; 
        /* System.out.println(contactServerUp); */
       }}

Right now, the results of clicking YES reults in a 0being returned, NO results in a 1. Is this normal, seems counterintuitive, and there's nothing at docs.oracle.java that shows a clear example of the output values except thiswhich seems to suggest that the public static final int YES_NO_OPTIONdefault in 0.

现在,单击 YES 的结果0是返回, NO 的结果是1. 这是否正常,似乎违反直觉,并且 docs.oracle.java 中没有任何内容显示输出值的清晰示例,除了似乎表明public static final int YES_NO_OPTION默认值为 0。

Additionally, the line System.out.println(contactServerUp);comes back with an error that the field contactServerUp might not have been initializedwhen I un-comment it, so I can't see if my convert-int-to-boolean is working.

此外,当我取消注释System.out.println(contactServerUp);该字段contactServerUp might not have been initialized时,该行返回一个错误,因此我看不到我的 convert-int-to-boolean 是否正常工作。

First: It appears that JOptionPane method does not include any boolean returns...except getWantsInput()which returns the value of the wantsInput property...so I assume I'm already being the most efficient I can with this. I'd like to know if there's an easier way.

第一:看起来 JOptionPane 方法不包含任何布尔值返回...除了getWantsInput()返回 WantsInput 属性的值...所以我假设我已经是最有效的了。我想知道是否有更简单的方法。

Second, what am I missing that prevents my console output statement from recognizing the contactServerUp? Where's my misplaced semicolon?

其次,我缺少什么阻止我的控制台输出语句识别contactServerUp? 我错位的分号在哪里?

回答by assylias

According to the javadoc, when one of the showXxxDialog methods returns an integer, the possible values are:

根据javadoc,当 showXxxDialog 方法之一返回整数时,可能的值为:

  • YES_OPTION
  • NO_OPTION
  • CANCEL_OPTION
  • OK_OPTION
  • CLOSED_OPTION
  • YES_OPTION
  • NO_OPTION
  • 取消_选项
  • OK_OPTION
  • CLOSED_OPTION

You should test against those constants:

您应该针对这些常量进行测试:

contactServerUp = (contactServerEntry == JOptionPane.YES_OPTION);

回答by XGouchet

The value returned by the the JOptionPane dialogs are values defined as constant fields in the class.

JOptionPane 对话框返回的值是定义为类中的常量字段的值。

Although, indeed, one could assume that 0 means false and 1 means true, the values are more ids for the different buttons a dialog can have.

尽管确实可以假设 0 表示假而 1 表示真,但这些值是对话框可以具有的不同按钮的更多 id。

To know if the user pressed yes or no, you can compare the return value to the constant fields described here. For example, in your case :

要知道用户是否按下是或否,您可以将返回值与此处描述的常量字段进行比较。例如,在您的情况下:

contactServerUp = (contactServerEntry == JOptionPane.YES_OPTION);

回答by willcodejavaforfood

Since a dialog a JOptionPane can have more than two possible 'answers' a boolean would be a poor representation. You are forgetting about the YES, NO and CANCEL option, or what about just a OK answer.

由于对话框 JOptionPane 可以有两个以上可能的“答案”,因此布尔值的表示效果不佳。您忘记了 YES、NO 和 CANCEL 选项,或者只是一个 OK 答案。

If it would have been written today, I suspect a Enum would have been used instead of an int.

如果它是今天写的,我怀疑会使用 Enum 而不是 int。

回答by Alderath

As for the second question, the compiler does not allow access to uninitialized variables. When you do the following, there is a chance that the variable might not be initialized:

至于第二个问题,编译器不允许访问未初始化的变量。执行以下操作时,变量可能未初始化:

   if(contactServerEntry==1) 
       contactServerUp = true;
    else
      if(contactServerEntry==0)
         contactServerUp = false;

What if, for example, contactServerEntry == JOptionPane.CLOSED_OPTION? In that case, your boolean value is never initialized. You need to add an elseclause at the end of your if-else chain, or initialize contactServerUp value to a default value in the beginning.

例如,如果contactServerEntry == JOptionPane.CLOSED_OPTION? 在这种情况下,您的布尔值永远不会被初始化。您需要else在 if-else 链的末尾添加一个子句,或者在开头将 contactServerUp 值初始化为默认值。