如何在Java中的system.exit之前显示消息

时间:2014-03-24 18:55:35

标签: java

我试图在使用JOptionPane.showInputDialog框时覆盖可能的选项。用户必须输入" Y"继续运行代码," N"将取消该程序并单击取消按钮应与键入" N"相同。但是,当用户点击取消时,我想显示一条消息,例如"您已选择取消订单"在System.exit(0)运行之前。我无法显示该消息。以下是我到目前为止的代码:

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

5 个答案:

答案 0 :(得分:0)

尝试将inputStr.equals(null)更改为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.");
    }

答案 1 :(得分:0)

exit(0)和留言javax.swing.Timer

之间延迟

并改变

if(inputStr.equals(null)){

if(inputStr == null){

==将始终比较标识 - 即两个值是否是对同一对象的引用。这也称为引用相等。 Java没有任何用户定义的运算符重载。

.equals()将调用Object声明的virtual equals方法,除非编译时类型inputStr引入了更具体的重载。

当然,如果inputStr为空,那么当您尝试拨打NullPointerException时,您将获得inputStr.equals(null)

答案 2 :(得分:0)

想想

if(inputStr.equals(null)){

调用' equals()'是否有意义? a' null'的方法宾语?因为如果inputStr为null,那么你将无法在其上调用方法。

正确的语法是:

if(inputStr == null){

并将此作为第一个' if',以保护您免受NPE的侵害。

答案 3 :(得分:0)

我会使用YES_NO_CANCEL_OPTION:

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

答案 4 :(得分:0)

这就是我要做的事情

 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