Search code examples
javalogin-script

How to make my user/pwd verification with output work


So, I'm lost as to what to do here. I need to make the input1 and input2 two separate fields so it doesn't mix them up. Any ideas would be appreciated. It doesn't have to be input1/input2, but it would be nice if it was kept that way.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.*;
import static jdk.nashorn.tools.ShellFunctions.input;

public class JOptionPaneMultiInput {

public static void main(String[] args) throws FileNotFoundException {

    Scanner keyboard = new Scanner(System.in);
    String input1;
    String input2;

    JTextField UsernameField = new JTextField(10);
    JTextField PwdField = new JTextField(10);

    JPanel myPanel = new JPanel();
    myPanel.add(new JLabel("Username: "));
    myPanel.add(UsernameField);
    myPanel.add(Box.createVerticalStrut(20)); // a spacer
    myPanel.add(new JLabel("Password: "));
    myPanel.add(PwdField);

    input1 = (UsernameField); //<<<--- Error.
    input2 = (PwdField);      //<<<--- Error.

    if (usernamefield(input1)) {

        System.out.println("Username verified.");

    } else if (pwdfield(input2)) {

        System.out.println("Password verified.");

    } else {
        System.out.println("Error: Wrong username/password.");
    }
}

private static boolean usernamefield(String user1) {

    return ((user1.length() == 6)
            && (user1.matches("user"))
            && (user1.matches("user"))
            && (user1.matches("user")));
}

private static boolean pwdfield(String password1) {

    return ((password1.length() == 6)
            && (password1.matches("pwd"))
            && (password1.matches("pwd"))
            && (password1.matches("pwd")));
}
}

Solution

  • Read the fine manual:

    input1 = UsernameField.getText();
    input2 = PwdField.getText();
    

    Also, your JPanel should be added to a JFrame, which should in turn be set visible:

    JFrame frm = new JFrame();
    frm.getContentPane().add(myPanel);
    frm.pack();
    frm.setVisible(true);
    

    Then you should also add a JButton to trigger the login action instead of putting it in the main().

    I strongly suggest you read the Java tutorial on Swing.

    EDIT: Here is an MCVE you can use for future references:

    public class Test extends JFrame {
        private static final long serialVersionUID = 1L;
    
        private JTextField tfUsername;
        private JPasswordField tfPwd;
    
        public Test() {
            super();
            JPanel myPanel = new JPanel();
            getContentPane().add(myPanel);
            myPanel.add(new JLabel("Username: "));
            tfUsername = new JTextField(10);
            myPanel.add(tfUsername);
            myPanel.add(Box.createHorizontalStrut(20)); // a horizontal spacer
            myPanel.add(new JLabel("Password: "));
            tfPwd = new JPasswordField(10);
            myPanel.add(tfPwd);
            JButton btnLogin = new JButton("Login");
            myPanel.add(btnLogin);
            btnLogin.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    String user = tfUsername.getText();
                    if (usernamefield(user)) {
                        String pwd = tfPwd.getText(); // Deprecated (use "getPassword()" for better security, see https://stackoverflow.com/q/8881291/1098603)
                        if (pwdfield(pwd))
                            JOptionPane.showMessageDialog(Test.this, "Password verified.");
                        else
                            JOptionPane.showMessageDialog(Test.this, "Error: Wrong username/password.", "Login error", JOptionPane.ERROR_MESSAGE);
                    } else
                        JOptionPane.showMessageDialog(Test.this, "Error: Unknown username.", "Login error", JOptionPane.WARNING_MESSAGE);
                }
            });
        }
    
        public static void main(String[] args) {
            // You can do that outside of EDT (see https://stackoverflow.com/q/491323/1098603)
            Test frm = new Test();
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Outside of Test class so close operation is defined by the user and not the class
            frm.pack();
            frm.setVisible(true);
        }
    
        private static boolean usernamefield(String user) {
            return "user".equals(user);
        }
    
        private static boolean pwdfield(String password) { // TODO: Change String to char[] to improve security
            return "pwd".equals(password);
        }
    }