Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton无法打开新表单_Java_Swing_Jframe - Fatal编程技术网

Java JButton无法打开新表单

Java JButton无法打开新表单,java,swing,jframe,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 {

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


    }


}

这是你的第二张窗口表格吗?如果是,则用new register.setVisibletrue替换新寄存器。类名应以大写字符开头。JDK中的所有类都遵循这个标准。不要制定自己的惯例。此外,变量名不应以大写字符开头。大多数变量都是正确的,但有些变量不正确。保持一致!!!注册是做什么的?你可能还想看看。一般来说,您的登录类不应该做出导航决策,而应该简单地将用户意图报告给另一个某种控制器,然后该控制器会自行做出决策。顺便说一句,当前行为由frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE;&frame.dispose。。但是使用@MadProgrammer建议的对话框