【问题标题】:Small Issue with JOptionPaneJOptionPane 的小问题
【发布时间】:2015-10-16 17:43:11
【问题描述】:

我正在尝试使用 JOptionPane 运行我的程序,但它没有输出错误窗口。我的程序必须从文件中读取乘客,他们都有名字、姓氏、航班等级、护照代码和清关统计信息。如果乘客有黄色、橙色或红色的清关警告,则应输出 JOptionPane 对话框。消息框的标题应为“安全许可警报”。

-如果 ClearanceException 为黄色,则显示“谨慎操作”。 - 如果 ClearanceException 为橙色,则显示“已识别可能的威胁。”。 - 如果 ClearanceException 为红色,则显示“已识别威胁。联系执法部门”。

我的问题是每个乘客都会出现一个黄色警告对话框,而橙色和红色警告永远不会出现。这是我的代码

驱动程序/主程序

package lab.assignment.pkg11;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.SecurityException;
import java.nio.file.Paths;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.JOptionPane;

public class ClearanceException 
{
    private static Scanner input;

    public static void main(String[] args) 
    {
        String fName;
        String lName;
        String fClass;
        String pCode;
        String securityStatus;

        code clCode = code.Yellow;

        try 
        {
            input = new Scanner(Paths.get("Passengers.dat"));
            input.useDelimiter("[\r\n,]");

            while (input.hasNext()) 
            {
                fName = input.next();
                lName = input.next();
                fClass = input.next();
                pCode = input.next();

                System.out.printf("%s, %s, %s,%s, %s%n", fName, lName, fClass, pCode, clCode);

                switch (clCode) 
                {
                case Yellow:
                    JOptionPane.showMessageDialog(null, "Proceed with caution.", "Code Yellow", JOptionPane.ERROR_MESSAGE);
                    break;

                case Orange:
                    JOptionPane.showMessageDialog(null, "Possible threat identified.", "Code Orange", JOptionPane.ERROR_MESSAGE);
                    break;

                case Red:
                    JOptionPane.showMessageDialog(null, "Threat identified. Contact law enforcement.", "Code Red", JOptionPane.ERROR_MESSAGE);
                    break;
                }
            }
        } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
        enum code {Green, Yellow, Orange, Red;}
    }

`package lab.assignment.pkg11;

public class ClearanceMain extends Exception
{
    public ClearanceMain (String message)
    {
        super (message);
    }

    ClearanceMain() 
    {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}`

记事本文件

`Bryan,Buonaiuto,econo,USA,Green
Emily,Cativo,busin,USA,Yellow
Edmond,Wint,first,USA,Orange
Eric,Monforte,busin,ITA,Red
James,Kilmeade,econo,USA,Green
Alexander,Antonacci,econo,PRT,Green
Gabriella,Johnson,first,CAN,Green
Barbara,Martinez,first,COL,Orange
Enam,Safi,econo,YEM,Orange
Sean,Yakub,busin,YEM,Orange
Christina,Tarin,busin,CMR,Green
Emily,Sharma,econo,CMR,Green
Charnelle,Kupfer,econo,DEU,Green
Aaron,Gossett,econo,DEU,Green
Conrad,Golder,econo,USA,Green
Carla,Vasquez,first,USA,Green
Melinda,Osorio,first,DOM,Green
Antonio,Espinoza,econo,DOM,Green
Seth,Howell,busin,USA,Orange
Navpreet,Afzal,busin,PAK,Red
Thomas,Przywara,busin,BEL,Green
Lea,Gaang,econo,BEL,Green
Phoebe,Starks,first,USA,Green
Netel,Abdelghani-Serour,econo,PAK,Green
Ayush,Juzumas,econo,AGO,Green
Ayesha,Saagber,first,AGO,Green
Darla,Zagorski,busin,DEU,Green
Ling,Weng,first,CHIN,Yellow
Chin,Weng,first,CHIN,Yellow
Adbdul,Islam,econo,PAK,Red
Gissele,Bencosme,econo,USA,Green
Ismanel,Kefalas,busin,IND,Green
Lee,Kang,busin,KOR,Green
Graciela,Quinones,econo,SLV,Green
Jorges,Quinones,econo,SLV,Green
Lissette,Quinones,econo,SLV,Green
Nutan,Patel,first,IND,Green
Wilson,Singh,first,IND,Green`

【问题讨论】:

  • 在尽可能窄的范围内声明变量是一个好习惯——在您的情况下,在循环内部而不是在循环外部。你根本不需要默认值Yellow

标签: java joptionpane


【解决方案1】:

你忘了:

clCode = input.next();

之后:

fName = input.next();
lName = input.next();
fClass = input.next();
pCode = input.next();

您没有从文本文件中获取颜色,因此在此处设置clCode 始终为Yellow

code clCode = code.Yellow;

当然,input.next() 返回一个String,因此您将不得不解析它以找出它对应于您的枚举中的哪个值。你可以使用Enum#valueOf()的方法来做到这一点:

code clCode = code.valueOf(input.next());

只要Strings 与枚举值完全匹配,这将起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2012-12-01
    • 2012-09-24
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多