简体   繁体   English

JFrame不处理

[英]JFrame Not Disposing

I am attempting to dispose my JFrame on button click. 我试图按下按钮处理我的JFrame。 However, when I click the button everything executes except the JFrame doesn't close! 但是,当我单击按钮时,一切都会执行,但JFrame不会关闭! Why is this? 为什么是这样? Is it a problem with another JFrame opening directly after it? 在它之后直接打开另一个JFrame是一个问题吗?

public static void login() {
    JFrame loginFrame = new JFrame();
    JPanel loginPanel = new JPanel();
    JButton loginButton = new JButton("Login");
    JLabel usernameText = new JLabel("Username:");
    JLabel passwordText = new JLabel("Password:");
    final JTextField usernameField = new JTextField(15);
    final JTextField passwordField = new JTextField(15);
    status.setForeground(Color.RED);

    new LoginBackground();

    loginPanel.add(usernameText);
    loginPanel.add(usernameField);
    loginPanel.add(passwordText);
    loginPanel.add(passwordField);
    loginPanel.add(loginButton, BorderLayout.CENTER);
    loginPanel.add(status);
    loginFrame.add(loginPanel);

    loginPanel.add(loginBackgroundLabel);
    loginFrame.add(loginPanel);

    loginFrame.repaint();
    loginFrame.setVisible(true);
    loginFrame.setSize(1200, 800);

    loginPanel.setLayout(null);

    usernameText.setSize(100, 100);
    passwordText.setSize(100, 100);

    usernameText.setFont(new Font("Serif", Font.PLAIN, 20));
    usernameText.setForeground(Color.white);

    passwordText.setFont(new Font("Serif", Font.PLAIN, 20));
    passwordText.setForeground(Color.white);

    loginBackgroundLabel.setLocation(0, 0);
    usernameText.setLocation(555, 230);
    passwordText.setLocation(555, 290);
    usernameField.setLocation(500, 300);
    passwordField.setLocation(500, 350);
    loginButton.setLocation(550, 400);
    status.setLocation(500, 450);

    loginPanel.revalidate();
    loginFrame.repaint();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e2) {
        e2.printStackTrace();
    }

    new Sound();

    login = false;

    loginButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                Frame.loginFrame.setVisible(false);
                Frame.loginFrame.dispose();
                username = usernameField.getText();
                password = passwordField.getText();
                Connect.checkLogin();
                Sound.clip.stop();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    });
}

关闭时窗口操作设置为关闭

setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM