0

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")));
}
}
NumaNuma
  • 39
  • 4
  • There are many things wrong with your code: conventions say variable names should start with a lower-case (i.e. `PwdField` instead of `PwdField`). Use `pwdField.getText()` to get the *content* of the textfield. Your code will not compile as-is. Any reason why you test 3 times each field on the same constant? – Matthieu Nov 29 '16 at 04:30
  • The idea of using the if / else property was to begin a form of lightweight encryption by outputing two separate files, one for input1(user1), and one for input2(password1) which would merge after, and form a backend verification. – NumaNuma Nov 29 '16 at 04:32
  • I forgot to mention; I'm new to Java. – NumaNuma Nov 29 '16 at 04:34
  • 1
    @numanuma we guessed – Scary Wombat Nov 29 '16 at 04:34
  • I'll update this code when I get it working, granted it doesn't get taken down or deleted. It's part of the beginning of a login system that I'm going to make. Thanks for any future/past input, folks. – NumaNuma Nov 29 '16 at 04:36

1 Answers1

1

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);
    }
}
Community
  • 1
  • 1
Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • @ScaryWombat of course it should. And the JPanel should be embedded in a JFrame to be shown on the screen. – Matthieu Nov 29 '16 at 04:36
  • Well, I haven't built much structure around it because I'm just trying to get it to get the basic output right now. – NumaNuma Nov 29 '16 at 04:38
  • @NumaNuma see my edit about some hints you should do and where to look for more info. – Matthieu Nov 29 '16 at 04:42
  • Thank you Matthieu. Will do. I'll also read into my Java 5th edition book, as well later. I've been working at this project all day outside of my web design project. – NumaNuma Nov 29 '16 at 04:49
  • 1
    @NumaNuma I added an MVCE for you to grab on. But the Swing tutorial really is the place to start. – Matthieu Nov 29 '16 at 05:22
  • 1
    Thanks, I really appreciate the help. I'm slowly learning. As much as the book helps, I really do just enjoy tinkering with the code and get carried away with just that a lot of the time. Your example I will add into my index of programs. – NumaNuma Nov 29 '16 at 08:45
  • You are brilliant, Matthieu. It runs great, I'm just going through it and I'll probably retype it 20 times or so. – NumaNuma Nov 29 '16 at 08:54
  • @NumaNuma your're more than welcome, code is what this site is about :) – Matthieu Nov 29 '16 at 10:23