在Java中,如何在system.exit之前显示消息
我试图在使用JOptionPane.showInputDialog对话框时介绍可能的选项。用户必须输入Y才能继续运行代码,N将取消该过程,单击“取消”按钮应与键入N相同。但是,当用户单击“取消”时,我想在System.exit0运行之前显示一条消息,就像您选择取消订单一样。我无法显示该消息。以下是我目前掌握的代码:在Java中,如何在system.exit之前显示消息,java,Java,我试图在使用JOptionPane.showInputDialog对话框时介绍可能的选项。用户必须输入Y才能继续运行代码,N将取消该过程,单击“取消”按钮应与键入N相同。但是,当用户单击“取消”时,我想在System.exit0运行之前显示一条消息,就像您选择取消订单一样。我无法显示该消息。以下是我目前掌握的代码: inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)"); if(inputStr.equals
inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if(inputStr.equalsIgnoreCase("N")){
JOptionPane.showMessageDialog(null, "Since you are not entering an order....\n" +
"The program will close.");
System.exit(0);
}
else if(inputStr.equals(null)){
JOptionPane.showMessageDialog(null, "You have chosen to cancel this order");
System.exit(0);
}
else if(!inputStr.equalsIgnoreCase("Y")){
JOptionPane.showMessageDialog(null, "You have entered an invalid character.\n" +
"Enter a 'Y' or 'N' only.");
continue;
}
尝试将inputStr.equalsnull更改为inputStr==null 只要您将周围的if状态更改为其下面的状态,就可以正常工作
String inputStr;
inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if(inputStr == null){
JOptionPane.showMessageDialog(null, "You have chosen to cancel this order");
System.out.println("hello");
System.exit(0);
}
else if(inputStr.equalsIgnoreCase("N")){
JOptionPane.showMessageDialog(null, "Since you are not entering an order....\n" +
"The program will close.");
System.exit(0);
}
else if(!inputStr.equalsIgnoreCase("Y")){
JOptionPane.showMessageDialog(null, "You have entered an invalid character.\n" +
"Enter a 'Y' or 'N' only.");
}
在exit0和消息之间进行延迟
改变
if(inputStr.equals(null)){
与
==将始终比较标识-即两个值是否是对同一对象的引用。这也称为引用相等。Java没有任何用户定义的运算符重载
.equals将调用由对象声明的虚拟equals方法,除非编译时类型inputStr引入了更具体的重载
当然,如果inputStr为null,那么当您尝试调用inputStr.equalsnull时,您将得到一个NullPointerException
if(inputStr.equals(null)){
对“null”对象调用“equals”方法有意义吗?因为如果inputStr为null,那么您将无法对其调用方法
正确的语法是:
if(inputStr == null){
并将此作为第一个“如果”,以保护您免受NPE的影响。我会使用“是”“否”“取消”选项:
Object[] options = {"Yes","No","Cancel"};
int n = JOptionPane.showOptionDialog(frame,
"Continue?",
"Would you like to continue?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if (n == JOptionPane.YES_OPTION) {
System.out.println("Clicked Yes");
} else if (n == JOptionPane.NO_OPTION) {
System.out.println("Clicked No");
} else if (n == JOptionPane.CANCEL_OPTION) {
System.out.println("Clicked Cancel");
} else {
System.out.println("something else (like clicked the 'x' button)");
}
这就是我要做的
String inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if (inputStr == null || inputStr.isEmpty()) {
JOptionPane.showMessageDialog(null, "You Cancelled");
} else {
if (inputStr.equalsIgnoreCase("N")) {
JOptionPane.showMessageDialog(null,
"Since you are not entering an order....\n"
+ "The program will close.");
System.exit(0);
} else if (!inputStr.equalsIgnoreCase("Y")) {
JOptionPane.showMessageDialog(null,
"You have entered an invalid character.\n"
+ "Enter a 'Y' or 'N' only.");
}
}
Goodluck此代码按预期工作。为什么不创建一个包含三个选项的选项窗格,JOptionPane.YES\u NO\u CANCEL\u选项。那么每个选项都有值。为什么?JOptionPane.showMessageDialog会阻止执行。@UdoKlimaschewski JOptionPane.showMessageDialog会阻止正确的执行,但被问到的问题说他不必这样做,所以我提到一个关于swing的意见。计时器:
String inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if (inputStr == null || inputStr.isEmpty()) {
JOptionPane.showMessageDialog(null, "You Cancelled");
} else {
if (inputStr.equalsIgnoreCase("N")) {
JOptionPane.showMessageDialog(null,
"Since you are not entering an order....\n"
+ "The program will close.");
System.exit(0);
} else if (!inputStr.equalsIgnoreCase("Y")) {
JOptionPane.showMessageDialog(null,
"You have entered an invalid character.\n"
+ "Enter a 'Y' or 'N' only.");
}
}