即使编写代码,窗口也始终打开

时间:2017-06-02 16:09:33

标签: java eclipse window graphic

我正在创建一个程序,当正确的登录信息被放入文本字​​段密码字段并按下一个名为“login”的按钮时,该程序将打开一个名为“Menu”的类GUI。问题是菜单窗口将打开空白,而不是显示我已编程到菜单中的内容。

这是我的登录类代码,

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

    }
}

这是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;
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);



    }
}

这两个类都在名为ems的包中。 我非常喜欢编程,非常感谢帮助。 谢谢

2 个答案:

答案 0 :(得分:1)

仍在寻找,但是:

    menuFrame.getContentPane().add(lblLoginToThe);

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

也不要直接致电setBounds(),使用布局管理器。这是你什么都看不见的另一个原因:你所有的组件都是在窗外绘制的,或者是其他类似的错误。布局管理员将解决这个问题。

编辑:正如Edwardth所说,你习惯于声明你的类是一个东西(比如JFrame),然后在initialize方法中创建一个第二个 JFrame。别这么做。

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

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

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

这是我的Login类示例,没有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 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();
    }
}

答案 1 :(得分:1)

您正在JFrameLogin内创建另一个Menu,这是错误的,也不需要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);
    }
}