0

I am writing a small login form at Java using AWT. Faced a problem while checking user input for login: I need, when user inputed "admin" as login and "password" as password, program outputting "Succes!", but anyway I get "Wrong!" message, even if inputed data meet the condition.

enter image description here

So, what should I do so that the program checks the condition correctly?

Code:

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

public class Login extends JFrame {
    private JButton button = new JButton("Confirm");
    private JLabel label1 = new JLabel("Login:");
    private JTextField login = new JTextField("", 8);
    private JLabel label2 = new JLabel("Password:");
    private JTextField pswrd = new JTextField("", 8);

    public Login() {
        super("Please log in");
        this.setBounds(100, 100, 250, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(new GridLayout(3,2,2,2));
        container.add(label1);
        container.add(label2);
        container.add(login);
        container.add(pswrd);
        button.addActionListener(new ButtonEvent());
        container.add(button);
    }

    class ButtonEvent implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (login.getText() == "admin" && pswrd.getText() == "password")
            {
                JOptionPane.showMessageDialog(null, "Succes!", "Output", JOptionPane.PLAIN_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Wrong!", "Output", JOptionPane.PLAIN_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        Login app = new Login();
        app.setVisible(true);
    }
}
h4cktivist
  • 73
  • 3
  • 13

1 Answers1

2

Just try .equals() instead of ==

zabaykal
  • 1,134
  • 1
  • 8
  • 21