简体   繁体   English

对JOptionPane使用布尔值True / False.YES_NO_OPTION

[英]Using Boolean True/False with JOptionPane.YES_NO_OPTION

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 (eg, Yes/Yes/Yes/No -> link A, Yes/No/No/Yes -> link B, etc). 我在四个单独的对话框中有一系列四个是/否选择,其累积结果将导致十二个单独的链接之一(例如,是/是/是/否->链接A,是/否/否/是->链接B等)。 The branching logic uses boolean values. 分支逻辑使用布尔值。

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 0 being returned, NO results in a 1 . 现在,单击YES的结果将返回0 ,否则返回1 Is this normal, seems counterintuitive, and there's nothing at docs.oracle.java that shows a clear example of the output values except this which seems to suggest that the public static final int YES_NO_OPTION default in 0. 这是正常现象,似乎是违反直觉的,并且docs.oracle.java中没有任何东西可以显示输出值的清晰示例,除了似乎表明public static final int YES_NO_OPTION缺省为0。

Additionally, the line System.out.println(contactServerUp); 此外,该行System.out.println(contactServerUp); comes back with an error that the field contactServerUp might not have been initialized when I un-comment it, so I can't see if my convert-int-to-boolean is working. 会返回一个错误,当我取消注释该字段时, contactServerUp might not have been initialized字段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. 首先:看来JOptionPane方法不包含任何布尔返回值……除了getWantsInput()返回了wantInput属性的值之外……所以我认为我已经是使用此方法效率最高的方法了。 I'd like to know if there's an easier way. 我想知道是否有更简单的方法。

Second, what am I missing that prevents my console output statement from recognizing the contactServerUp ? 其次,我缺少什么阻止我的控制台输出语句识别contactServerUp Where's my misplaced semicolon? 我放错位置的分号在哪里?

According to the javadoc , when one of the showXxxDialog methods returns an integer, the possible values are: 根据javadoc ,当showXxxDialog方法之一返回整数时,可能的值为:

  • YES_OPTION YES_OPTION
  • NO_OPTION NO_OPTION
  • CANCEL_OPTION CANCEL_OPTION
  • OK_OPTION OK_OPTION
  • CLOSED_OPTION CLOSED_OPTION

You should test against those constants: 您应该针对这些常量进行测试:

contactServerUp = (contactServerEntry == JOptionPane.YES_OPTION);

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表示false,1表示true,但是对于对话框可以具有的不同按钮,值更多。

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

Since a dialog a JOptionPane can have more than two possible 'answers' a boolean would be a poor representation. 由于对话框,JOptionPane可以具有两个以上的“答案”,因此布尔值将表示不佳。 You are forgetting about the YES, NO and CANCEL option, or what about just a OK answer. 您忘记了“是”,“否”和“取消”选项,或者只是一个好的答案。

If it would have been written today, I suspect a Enum would have been used instead of an int. 如果今天写的话,我怀疑应该用Enum代替int。

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 ? 例如,如果contactServerEntry == JOptionPane.CLOSED_OPTION怎么办? In that case, your boolean value is never initialized. 在这种情况下,永远不会初始化您的布尔值。 You need to add an else clause at the end of your if-else chain, or initialize contactServerUp value to a default value in the beginning. 您需要在if-else链的末尾添加else子句,或者在开始处将contactServerUp值初始化为默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM