Java JButton无法打开新表单

时间:2015-11-04 16:15:14

标签: java swing jframe

我是Java的新手,我遇到了一个问题。一旦我点击按钮,它就不会向我显示另一种形式,只是消失了。你可以看到,我设置了按钮ActionListener,但它只运行第二行(关闭当前表单)。

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 ){


    }


}

0 个答案:

没有答案