- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 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`
最佳答案
你忘了:
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());
只要字符串
与枚举值完全匹配,该方法就可以工作。
关于java - JOptionPane 的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176482/
我正在使用 JOptionPane 来获取字符串。单击“确定”即可保存字符串。现在我希望弹出第二个 JOptionPane 以输入第二个必要的字符串。是否有机会将 ActionListener 添加到
我有一个显示来自 JOptionPane 的确认消息的过程。此过程从 JMenuItem 的 Actionlistener 内的 SwingUtilities.invokeLater(runnable
我使用以下代码来获取InputDialog: String c = JOptionPane.showInputDialog("Select number",JOptionPane.OK_OPTION)
正如你从我上面的主题中看到的, 我想知道我怎么能解雇一个 JOptionPane 因为另一个 JOptionPane 而变得无关紧要,因为用户出于某种原因没有通过自己点击确定按钮(例如)解雇第一个。
没有收到“文件已成功接收”消息,请帮助我。将文件从客户端传输到服务器时,即使文件已收到,也不会显示该消息。 这是使用套接字将文件从客户端传输到服务器的程序,接口(interface)也是使用java
JOptionPane.showInputDialog 有一个不需要父组件参数的表单。 JOptionPane.showConfirmDialog 确实如此。这是为什么? 最佳答案 JOptionPa
我正在用 Java 创建一个基于 Tic Tac Toe GUI 的游戏,并且正在努力将 2D 数组与 JOptionPane 结合使用。到目前为止,我已经能够创建可供选择的按钮: import ja
使用 JOprionPane 时,光标出现了一些问题。我将光标设置到 pharent 框架,然后使用这个显示一个对话框: Object[] possibilities = {"ham", "spam"
我创建了一个打开 JOptionPane 的按钮。它允许用户输入 string..>> String str = JOptionPane.showInputDialog 如何获取用户输入到 jopti
该代码正在运行。但问题是,当选择 NO_OPTION 时,窗口就会被释放。我想在选择 NO_OPTION 时保留窗口?你能给点建议吗? int dialogButton = JOptionPa
我正在 Java 上开发 Tic Tac Toe 游戏(eclipse)。在我的计算机上,我的对话框非常小。我一直在努力把它做得更大。我没有任何运气。我希望这里有人能引导我走向正确的方向。下面的代码是
我正在使用脚本 API 为我玩的游戏的机器人制作脚本,但是每当我覆盖机器人管理器时,就会出现一个 JOptionPane 来阻止执行,直到我关闭它,但是我想在不运行该脚本的情况下运行此脚本人为干预,所
我正在用 Java 开发一个游戏,我试图在 JOptionPane 确认框打开时暂停它。这是代码: window.addWindowListener(new WindowAdapter() {
这个问题已经有答案了: Calling one JFrame from another using Timer without any buttons (1 个回答) 已关闭 9 年前。 需要帮助制作
很抱歉代码这么长,我只需要知道我缺少什么来初始化我的所有决定。编译器提示我的变量没有被初始化。谢谢你。 import javax.swing.JOptionPane; public class Sem
我想使用 JOptionPane 处理一些异常。这是主要方法: public class MainRun { public static void main(String args[]){
不允许使用数组,该函数正在工作,但只是返回 0,就好像它没有计算正确的输入字符一样,但现在它给了我一个“字符串超出范围:3” 这应该运行,打开一个窗口,要求我输入一个字符串,在本例中是一个单词,然后打
我正在使用 Jfilechooser,如果我选择文件,它将计算文件名的字符数,但是如果文件超过 3kb,它将限制 Joptionpane 将显示。我的问题是即使文件是0kb,Joptionpane也会
我创建了一个应用于框架的名为 addSupplier 的按钮,然后创建了一个操作监听器,因此一旦按下 addSupplier 按钮,它将创建一个 JOptionPane,其中有一个附加了 JTextF
我知道解决方案是使用 for 循环来逐步遍历数组并显示在 Pane 中。但是我没有找到对此的直接解释。我需要一个下一个和一个上一个按钮来显示每个数组元素,并且在按下下一个按钮时到达第一个元素后仅返回到
我是一名优秀的程序员,十分优秀!