0

I want to make one simple window with JLabel 'Password', and JPasswordField, and "Login" Button. In the future I want to make a database with Data's, but at the moment I just want to make simple 'move'. If I write in Password Field word which equals my String Field, I want to create new Window and make the old one disappear. My code:

public class Test extends JFrame implements ActionListener {

    JButton login,exit;
    JTextField tflogin;
    JPasswordField pf;
    JLabel lpf,ll;
    String llogin = "Marco";
    String password = "Result";
    JFrame nw;

    public Test()
    {
        setBounds(200, 200, 600, 400);;
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);

        nw = new JFrame("Frame");
        nw.setSize(1200,800);
        nw.setVisible(false);

        pf = new JPasswordField();
        pf.setBounds(200, 150, 150, 20);
        add(pf);

        tflogin = new JTextField();
        tflogin.setBounds(200, 120, 150, 20);
        add(tflogin);

        lpf = new JLabel("Password");
        lpf.setBounds(120, 150, 100, 20);
        add(lpf);

        ll = new JLabel("Login");
        ll.setBounds(120, 120, 100, 20);
        add(ll);

        login = new JButton("Log");
        login.setBounds(180, 180, 100, 20);
        add(login);
        login.addActionListener(this);

        exit = new JButton("Exit");
        exit.setBounds(290, 180, 100, 20);
        add(exit);
        exit.addActionListener(this);
    }
    public static void main(String[] args)
    {
        Test MyFrame = new Test();
        MyFrame.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if(source == login)
        {
            if (pf.equals(password));
            {
                nw.setVisible(true);
            }
        }
        else if (source == exit)
        {
            dispose();
        }
    }
}
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
Just4Fun
  • 81
  • 1
  • 1
  • 4
  • so.... what problem(s) are you having? – ControlAltDel Sep 22 '15 at 18:41
  • Sorry for my language , main problem -> When i click 'Log' button , program don't check equals between JPasswordField and String 'password' , if it's the same , program should launch second window , if it's not the same , it should do nothing... in my code when i click 'Log' it's always show second window..no matter what i wrote into JPasswordField.. – Just4Fun Sep 22 '15 at 19:05
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Sep 22 '15 at 21:51

3 Answers3

0

One of your problems is here:

if (pf.equals(password));

pf is a JPasswordField. Use pf.getText() and compare it with the password.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
0

Problem is your comparing JPasswordField with String:

pf.equals(password) // pf is of type JPasswordField

Try getPassword() which returns char[]:

String pswd = new String(pf.getPassword());
if(pswd.equals(password)) {
  nw.setVisible(true);
}
0

You can try this

 public void actionPerformed(ActionEvent e)
    {
        //Object source = e.getSource();
        if( e.getSource() == login)
        {

             String user_password= pf.getText();  //you can use  char [] user_password=pf.getPassword();
           if (user_password.equals(password));
            {
                 dispose();
                 nw.setVisible(true);
            }
        }
        else if (source == exit)
        {
            dispose();
        }
    }
Anptk
  • 1,125
  • 2
  • 17
  • 28