Window 总是打开空白,即使写了代码

Window always opens blank even though code is written

我正在制作一个程序,当在文本字段密码字段中输入正确的登录信息并按下名为 "login" 的按钮时,它将打开一个名为 "Menu" 的 class GUI。问题是菜单 window 将打开空白,不显示我在菜单中编程的内容。

这是我的登录代码 class,

package ems;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JFrame implements ActionListener{

    private JFrame loginFrame;
    private JTextField usernameField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    window.loginFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Login() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        loginFrame = new JFrame();
        loginFrame.setTitle("Login");
        loginFrame.setBounds(100, 100, 450, 300);
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.getContentPane().setLayout(null);

        JLabel lblUsername = new JLabel("Username");
        lblUsername.setBounds(112, 116, 74, 16);
        loginFrame.getContentPane().add(lblUsername);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(112, 165, 74, 16);
        loginFrame.getContentPane().add(lblPassword);

        usernameField = new JTextField();
        usernameField.setBounds(198, 110, 134, 28);
        loginFrame.getContentPane().add(usernameField);
        usernameField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(198, 159, 134, 28);
        loginFrame.getContentPane().add(passwordField);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String uname = usernameField.getText();
                String pword = passwordField.getText();

                if (uname.equals("test") && pword.equals("test")){
                    JOptionPane.showMessageDialog(loginFrame, "Login successful.");
                    loginFrame.setVisible(false);
                    Menu menu = new Menu ();
                    menu.setVisible (true);

                }else{
                    JOptionPane.showMessageDialog(loginFrame, "Login unsuccessful.");
                }
            }
        });
        btnLogin.setBounds(238, 210, 90, 30);
        loginFrame.getContentPane().add(btnLogin);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(108, 210, 90, 30);
        loginFrame.getContentPane().add(btnExit);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(192, 192, 192));
        panel.setBounds(91, 86, 258, 163);
        loginFrame.getContentPane().add(panel);

        JLabel lblLoginToThe = new JLabel("LOGIN TO THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
        lblLoginToThe.setBounds(26, 23, 418, 16);
        loginFrame.getContentPane().add(lblLoginToThe);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.setBounds(0, 0, 450, 63);
        loginFrame.getContentPane().add(panel_1);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

这是菜单的代码 class,

package ems;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import ems.Login;

public class Menu extends Login implements ActionListener{

    private JFrame menuFrame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Menu window = new Menu();
                    window.menuFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Menu() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        menuFrame = new JFrame();
        menuFrame.setTitle("Menu");
        menuFrame.setBounds(100, 100, 450, 300);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuFrame.getContentPane().setLayout(null);

        JLabel lblLoginToThe = new JLabel("THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
        lblLoginToThe.setBounds(64, 22, 331, 16);
        menuFrame.getContentPane().add(lblLoginToThe);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.setBounds(0, 0, 450, 63);
        menuFrame.getContentPane().add(panel_1);

        JButton btnLogout = new JButton("Logout");
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                menuFrame.setVisible(false);
                Login login = new Login ();
                login.setVisible(true);
            }
        });
        btnLogout.setBounds(307, 222, 117, 29);
                menuFrame.getContentPane().add(btnLogout);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(10, 222, 117, 29);
        menuFrame.getContentPane().add(btnExit);



    }
}

这两个 classes 都在一个名为 ems 的包下。 我对编程很陌生,将不胜感激。 谢谢

仍在寻找,但是:

    menuFrame.getContentPane().add(lblLoginToThe);

内容窗格不是容器,您不能向其中添加多个内容。您需要将所有内容都放在一个面板中(注意面板 != 窗格),然后将一个面板添加到内容窗格。

也不要直接调用 setBounds(),使用布局管理器。这是您可能什么都看不到的另一个原因:所有组件都在 window 或其他类似错误之外绘制。布局管理器会解决这个问题。

编辑:正如 Edwardth 所说,您习惯于将 classes 声明为一个东西(如 JFrame),然后在initialize 方法。不要那样做。

public class Login extends JFrame implements ActionListener{
       ...
    private void initialize() {
        loginFrame = new JFrame();  // BAD!

当您声明 Login extends JFrame 您不需要第二帧时,JFrame 已经完成。

Edwardth 在我之前得到了答案,所以请继续研究他的示例,然后将他的答案标记为正确。如果您还有其他问题,请在下面的评论中提问。

这是我的登录示例 class,没有 setBounds()

class Login extends JFrame implements ActionListener{

    private JTextField usernameField;
    private JPasswordField passwordField;

    /**
     * Create the application.
     */
    public Login() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setTitle("Login");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblUsername = new JLabel("Username");
        JLabel lblPassword = new JLabel("Password");
        usernameField = new JTextField();
        usernameField.setColumns(10);
        passwordField = new JPasswordField();

        JPanel loginPanel = new JPanel( new GridLayout( 0,2 ) );
        loginPanel.add( lblUsername );
        loginPanel.add( usernameField );
        loginPanel.add( lblPassword );
        loginPanel.add( passwordField );

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String uname = usernameField.getText();
                String pword = passwordField.getText();

                if (uname.equals("test") && pword.equals("test")){
                    JOptionPane.showMessageDialog( Login.this, "Login successful.");
                    setVisible(false);
                    Menu menu = new Menu ();
                    menu.setVisible (true);

                }else{
                    JOptionPane.showMessageDialog( Login.this, "Login unsuccessful.");
                }
            }
        });


        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        JPanel panel = new JPanel();
        panel.setBackground(new Color(192, 192, 192));
        panel.add( btnLogin );
        panel.add( btnExit );

        JLabel lblLoginToThe = new JLabel("LOGIN TO THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));


        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.add( lblLoginToThe );

        Box topBox = Box.createVerticalBox();

        topBox.add( panel_1 );
        topBox.add( loginPanel );
        topBox.add( panel );

        add( topBox );
        pack();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

}

和主要 class:

class Menu extends JFrame {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Menu window = new Menu();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Menu() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setTitle("Menu");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Box topBox = Box.createVerticalBox();

        JLabel lblLoginToThe = new JLabel("THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));

        topBox.add( lblLoginToThe ); // **********

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));

        topBox.add( panel_1 );  // *************

        JButton btnLogout = new JButton("Logout");
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Menu.this.setVisible(false);
                Login login = new Login ();
                login.setVisible(true);
            }
        });

        topBox.add( btnLogout ); //****************

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        topBox.add( btnExit );

        add( topBox );
        pack();
    }
}

您正在 LoginMenu 中创建另一个 JFrame,这是错误的,也没有必要 Menu 扩展 Login

这是 Login 的固定版本:

package ems;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JFrame {
    private static final long serialVersionUID = 1L;

    private JTextField usernameField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Login() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        this.setTitle("Login");
        this.setBounds(100, 100, 450, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);

        JLabel lblUsername = new JLabel("Username");
        lblUsername.setBounds(112, 116, 74, 16);
        this.getContentPane().add(lblUsername);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(112, 165, 74, 16);
        this.getContentPane().add(lblPassword);

        usernameField = new JTextField();
        usernameField.setBounds(198, 110, 134, 28);
        this.getContentPane().add(usernameField);
        usernameField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(198, 159, 134, 28);
        this.getContentPane().add(passwordField);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String uname = usernameField.getText();
                String pword = passwordField.getText();

                if (uname.equals("test") && pword.equals("test")){
                    JOptionPane.showMessageDialog(Login.this, "Login successful.");
                    Login.this.setVisible(false);
                    Menu menu = new Menu ();
                    menu.setVisible (true);

                }else{
                    JOptionPane.showMessageDialog(Login.this, "Login unsuccessful.");
                }
            }
        });
        btnLogin.setBounds(238, 210, 90, 30);
        this.getContentPane().add(btnLogin);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(108, 210, 90, 30);
        this.getContentPane().add(btnExit);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(192, 192, 192));
        panel.setBounds(91, 86, 258, 163);
        this.getContentPane().add(panel);

        JLabel lblLoginToThe = new JLabel("LOGIN TO THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
        lblLoginToThe.setBounds(26, 23, 418, 16);
        this.getContentPane().add(lblLoginToThe);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.setBounds(0, 0, 450, 63);
        this.getContentPane().add(panel_1);
    }
}

Menu

package ems;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Menu extends JFrame{
    private static final long serialVersionUID = 1L;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Menu window = new Menu();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Menu() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        this.setTitle("Menu");
        this.setBounds(100, 100, 450, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);

        JLabel lblLoginToThe = new JLabel("THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
        lblLoginToThe.setBounds(64, 22, 331, 16);
        this.getContentPane().add(lblLoginToThe);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.setBounds(0, 0, 450, 63);
        this.getContentPane().add(panel_1);

        JButton btnLogout = new JButton("Logout");
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Menu.this.setVisible(false);
                Login login = new Login ();
                login.setVisible(true);
            }
        });
        btnLogout.setBounds(307, 222, 117, 29);
        this.getContentPane().add(btnLogout);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(10, 222, 117, 29);
        this.getContentPane().add(btnExit);
    }
}