简体   繁体   中英

How can I display a message dialog in JOptionPane, when user doesn't give input?

So I've made a program, which displays an input dialog and asks for the username, and the password. If the right combinations, it says that you're in the system. If you give a wrong combination, obviously it will say that you typed in the wrong combination.

But, I'm kinda stuck here. I want to do a new method, which says "You didn't give enough information" IF you clicked on the cancel button, either at the username dialog or at the password one.

When I did this new IF it said "Obsolete method" What can be the problem?

import javax.swing.JOptionPane;

public class LoginProgram {

    public static void main(String[] args) {
        String username = JOptionPane.showInputDialog("Username: ");
        String password = JOptionPane.showInputDialog("Password: ");

        if (username != null && password != null && username.contentEquals("pass") && password.contentEquals("fish") || username.contentEquals("italy") && password.contentEquals("shield") || username.contentEquals("PASS") && password.contentEquals("FISH") || username.contentEquals("ITALY") && password.contentEquals("SHIELD")) 
        {
            JOptionPane.showMessageDialog(null, "You're in our system.");
        } else {
            JOptionPane.showMessageDialog(null, "You're suspicious.");
        }

        if (username == null || password == null) {
            JOptionPane.showMessageDialog(null, "Not enough information.");
        }
    }

}

You can do it as follows:

import javax.swing.JOptionPane;

public class LoginProgram {

    public static void main(String[] args) {
        String username = JOptionPane.showInputDialog("Username: ");
        String password = JOptionPane.showInputDialog("Password: ");

        if (username == null || password == null) {
            JOptionPane.showMessageDialog(null, "Not enough information.");
        } else if (username != null && password != null && username.contentEquals("pass")
                && password.contentEquals("fish") || username.contentEquals("italy") && password.contentEquals("shield")
                || username.contentEquals("PASS") && password.contentEquals("FISH")
                || username.contentEquals("ITALY") && password.contentEquals("SHIELD")) {
            JOptionPane.showMessageDialog(null, "You're in our system.");
        } else {
            JOptionPane.showMessageDialog(null, "You're suspicious.");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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