3
\$\begingroup\$

It is a simple program which allows you to input a username an password. If the username/password is equal to the String it launches a JOptionPane that says "Correct". If it doesn't it launches a JOptionPane that says "Incorrect".

 import java.awt.event.*;
    import javax.swing.*;

public class Main {

    //The Strings for the program

    static String username = "Username";
    static String password = "Password";
    static int lockout = 0;
    static int lockout1 = 0;

    //Main statement, that runs the program

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

        //Makes the program look like regular windows program not a java one

        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        //The JPanel and it's contents
        JPanel p = new JPanel();
        JTextField first = new JTextField(8);
        first.setToolTipText("Enter Your Username");
        JLabel labelUser = new JLabel("Username:");
        JPasswordField second = new JPasswordField(8);
        //Tooltips when you hover over the item
        second.setToolTipText("Enter Your Password");
        JLabel labelPass = new JLabel("Password:");
        JRadioButton view = new JRadioButton();
        view.setToolTipText("To View Your Password");
        //Causes the JPassword field to output * as the text.
        second.setEchoChar('*');

        //Adds the different items to the JPanel.
        p.add(labelUser);
        p.add(first);
        p.add(labelPass);
        p.add(second);
        p.add(view);
        //Adds an action listener to the JRadioButton
         ActionListener viewActionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {
            if(view.isSelected()){
                    //Makes the Text in the JPasswordField visible.
                    second.setEchoChar((char)0);
                }else{
                    //Sets it back to the default "**"
                    second.setEchoChar('*');
                }
              }
            };

            //Adds the action listener to the JRadioButton
            view.addActionListener(viewActionListener);

        //Specifies what buttons are on the JOptionPane
        String[] buttons = {"OK", "Cancel"};
        //Code for the JOptionPane
        int a = JOptionPane.showOptionDialog(null, p, "Authentication", JOptionPane.WARNING_MESSAGE, 1, null, buttons, buttons);
        //If the first button is clicked
        if (a == 0) {
            //Checks if there is any text in the TextFields.
            if (first.getText().equals("") || second.getText().equals("")) {
                //Adds 1 to the int lockout 1 and if it reaches 4 it closes the app
                lockout1 += 1;
                if (lockout1 == 4) {
                    JOptionPane.showMessageDialog(null, "Please try again later", "Try Again Later", 0);
                    System.exit(0);
                }
                //Displays the message
                JOptionPane.showMessageDialog(null, "Please enter all of your credentials before continuing", "Try Again", 2);
                main(args);
                //If the username and the password fields are equal to the Strings, it outputs the message correct
            } else if (first.getText().equals(username) && second.getText().equals(password)) {
                JOptionPane.showMessageDialog(null, "Correct", "Correct", 1);
            } else {
                //If not it adds 1 to lockout, if lockout reaches 4 the program closes
                lockout += 1;
                if (lockout == 4) {
                    JOptionPane.showMessageDialog(null, "Please try again later", "Try Again Later", 0);
                    System.exit(0);
                }
                //Outputs the message incorrect
                JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", 0);
                //reopens the program
                main(args);
            }
        }
        //If you click the cancel button, it closes the application
        if (a == 1) {
            System.exit(0);
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Hi! Welcome to Code Review. This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question \$\endgroup\$ – TheCoffeeCup Dec 11 '15 at 23:55
  • \$\begingroup\$ I guess i did my best \$\endgroup\$ – Bill Nye Dec 12 '15 at 0:17
  • \$\begingroup\$ I meant a description of the program at the beginning of your question... \$\endgroup\$ – TheCoffeeCup Dec 12 '15 at 0:29
2
\$\begingroup\$

Here are some first (more or less grouped and complete) thoughts. I'm not an expert regarding awt or Swing, so I can't really comment on that.

  1. Don't put everything in your main method. Instead try to extract some useful (and potentially reusable) methods. One good starting point might be your duplicate code:

    lockout1 += 1;
    if (lockout1 == 4) {
        JOptionPane.showMessageDialog(null, "Please try again later", "Try Again Later", 0);
        System.exit(0);
    }
    
  2. Don't suppress all deprecation warnings. Instead do not use deprecated classes or methods.

  3. Make your fields private.

  4. username and password seem to be constants. If so, conventions advise to make them private static final and write them upper case:

    private static final String USERNAME = "Username";
    private static final String PASSWORD = "Password";
    
  5. Avoid System.exit. Your application will close automatically as soon as the main method returns. Just open another dialog.

  6. Format your source code. Some lines have different indentation and your are inconsistent with your spaces between if( or }else{.

  7. Some of your comments don't add much value for the reader. I usually recommend to not comment what you are doing, but instead why you are doing something.

  8. buttons should probably be a constant. Then again, there is an optionType for exactly your combination.

  9. Not sure on that one, but don't view and second have to be final to be used in the ActionListener?

  10. I think your parameters of showOptionDialog are incorrect. JOptionPane.WARNING_MESSAGE should actually be the optionType and 1 would be your messageType. Granted, this is a bit difficult, because both are int.

  11. Use constants from JOptionPane instead of "magic" numbers (e.g. the 1 above or when checking the return value of showOptionDialog).

  12. The dialog can actually return -1 as well if the user does not click any of the buttons, cf. the javadoc:

    /** Return value from class method if user closes window without selecting
     * anything, more than likely this should be treated as either a
     * <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
    public static final int         CLOSED_OPTION = -1;
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.