0

I am a new to Java, I met a problem. Once I click the Button, it never shows me another form, just disappear. You can see, I set the button ActionListener, but it only run the second line (close the current form).

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

public class login implements ActionListener {

    public login(){

        JFrame frame = new JFrame("Login");
        frame.setLayout(new GridLayout(5,1));
        JLabel label1 = new JLabel("User Name:");
        JPanel panel1 = new JPanel();
        frame.add(new JPanel());
        frame.add(panel1);
        panel1.add(label1);

        JLabel label2 = new JLabel("Password:");
        JPanel panel2 = new JPanel();
        frame.add(panel2);
        panel2.add(label2);

        JPanel panel3 = new JPanel();
        JButton button1 = new JButton("Register");
        //button1.addActionListener(this);
        JButton button2 = new JButton("Login");
        //button2.addActionListener(this);
        JButton button3 = new JButton("Cancel");
        //button3.addActionListener(this);
        panel3.add(button1);
        panel3.add(button2);
        panel3.add(button3);
        frame.add(panel3);

        JTextField JTF = new JTextField(12);
        JPasswordField JPF = new JPasswordField(12);
        panel1.add(JTF);
        panel2.add(JPF);

        frame.setResizable(false);
        frame.setVisible(true);
        frame.setSize(300, 200);
        frame.setLocation(300,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                  new register();

                frame.dispose();





            }
        });



    }

    public static void main(String[] args){
        new login();

    }
    public void actionPerformed(ActionEvent e ){


    }


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Li JiaXin
  • 11
  • 2
  • Is `register` your second window form ? if yes, replace `new register()` with `new register().setVisible(true)`. – ccc Nov 04 '15 at 16:30
  • 2
    Class names SHOULD start with an upper case character. All the classes in the JDK follow this standard. Don't make up your own conventions. Also, variable names should NOT start with an upper case character. Most of your variables are correct but some are not. Be consistent!!! – camickr Nov 04 '15 at 17:08
  • 1
    What does `register` do? You may also want to have a look at [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). Generally speaking your `login` class shouldn't be make navigation decisions, but simply reporting user intentions back to another controller of some kind, which would then make the decisions itself – MadProgrammer Nov 04 '15 at 21:43
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) BTW - the current behavior is determined by a combination of `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` & `frame.dispose();` .. But use a dialog as suggested by @MadProgrammer – Andrew Thompson Nov 04 '15 at 21:53

0 Answers0