【问题标题】:Java Swing - Why is my first window not disposing/closing?Java Swing - 为什么我的第一个窗口没有处理/关闭?
【发布时间】:2017-06-04 04:35:53
【问题描述】:

所以我正在创建一个应用程序。我希望 main 初始化一个登录窗口,我已经完成了。现在,我希望在单击按钮时关闭登录窗口,当它关闭时,会打开一个新窗口(称为 MainWindow),其中包含不同的按钮/信息/文本。

所有这些类都是独立的,但在同一个包中。

问题是:当我点击登录时主窗口打开,但是登录窗口没有终止/关闭。

我的主要方法,我从中调用登录窗口:

import javax.swing.*;

public class Main {

    // Display Login Window
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginWindow();
            LoginWindow.createAndShowLoginGUI();
        });
    }
}

我的登录窗口类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginWindow extends JFrame implements ActionListener {

    public void prepareLoginGUI() {

        // Frame with GridBagLayout
        JFrame loginFrame = new JFrame("Login Window");
        loginFrame.setSize(1200, 800);
        loginFrame.setLayout(new GridBagLayout());


        // Button for logging in, with respective GridBagConstraint
        // setFocusable is set to false to take out the border around the text
        JButton loginButton = new JButton("Login");
        loginButton.addActionListener(this::actionPerformed);
        loginButton.setActionCommand("Open");
        GridBagConstraints lButtonC = new GridBagConstraints();
        loginButton.setFocusable(false);


        // Username text-field and JLabel with respective GridBagConstraints
        JTextField tfUsername = new JTextField(15);
        GridBagConstraints tfUserC = new GridBagConstraints();
        JLabel txtUser = new JLabel("Username: ");
        GridBagConstraints txtUserC = new GridBagConstraints();


        // Password text-field and JLabel with respective GridBagConstraints
        JPasswordField tfPassword = new JPasswordField(15);
        GridBagConstraints tfPassC = new GridBagConstraints();
        JLabel txtPassword = new JLabel("Password: ");
        GridBagConstraints txtPassC = new GridBagConstraints();


        // Add all components to the JFrame
        // Making sure to add the text before the text-fields
        loginFrame.add(txtUser, txtUserC);
        loginFrame.add(tfUsername, tfUserC);
        loginFrame.add(txtPassword, txtPassC);
        loginFrame.add(tfPassword, tfPassC);
        loginFrame.add(loginButton, lButtonC);

        // Show and set close parameters
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.setVisible(true);
    }

    // Instructions for when the login button is clicked
    // Close this window, if button == open, which it does
    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals("Open")) {
            this.dispose();
            this.setVisible(false);
            new MainWindow();
            MainWindow.createAndShowMainWGUI();
        }
    }

    // Callable from Main class
    public static void createAndShowLoginGUI() {
        LoginWindow loginW = new LoginWindow();
        loginW.prepareLoginGUI();

    }

}

我的主窗口类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainWindow extends JFrame implements ActionListener {

    public void prepareMainWGUI(){
        // Frame with GridBagLayout
        JFrame loginFrame = new JFrame("Main Window");
        loginFrame.setSize(1200, 800);
        loginFrame.setLayout(new GridBagLayout());


        // Username text-field and JLabel with respective GridBagConstraints
        JLabel txtUser = new JLabel("It worked!");
        GridBagConstraints txtUserC = new GridBagConstraints();

        loginFrame.add(txtUser, txtUserC);

        loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        loginFrame.setVisible(true);
    }

    // Callable from Main class
    public static void createAndShowMainWGUI() {
        MainWindow mainWind = new MainWindow();
        mainWind.prepareMainWGUI();
    }

}

如果你使用这个代码,当主窗口打开时,登录窗口就在它的后面。

【问题讨论】:

标签: java swing


【解决方案1】:

按下按钮即可使用

System.exit(0);

这将终止您的应用程序。关闭一个窗口使用(如果您打开了多个窗口)而不影响其他窗口,

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

然后,打开一个新的,

new frameName().setvisible(true);

例子:

JButton closeButton = new JButton("Close");

closeButton .addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        this.dispose();//current(close only this) frame
        new frameName().setvisible(true);
    }
});

看看这个question

更新:

在下面的行中,您有extends JFrame,但您没有使用它。

public class LoginWindow extends JFrame implements ActionListener {

而不是你从JFrame创建新对象

JFrame loginFrame = new JFrame("Login Window");

您要添加到loginFrame 的所有组件。但是当actionPerformed 尝试this.dispose() 时,因为您没有使用扩展JFrame,所以什么都不会发生。

解决方案:

将您的JFrame 声明为实例变量:

public class LoginWindow implements ActionListener {
    JFrame loginFrame;
    public void prepareLoginGUI() {

然后改变,

this.dispose();

收件人:

loginFrame.dispose();

在此this.dispose(); 对扩展JFrame 的调用中,对您的JFrame 对象(loginForm) 无效。

最后一件事,确保删除extends JFrame。因为您没有对扩展的JFrame 做任何事情。阅读此post about extended JFrame

【讨论】:

  • 嗯,奇怪的是我做了同样的事情。但是当我在 prepareMainWGUI 方法中执行它而不是覆盖它时,它可以工作。这是为什么呢?
  • 几乎没有什么需要指出的。等一下,我会更新答案
  • @Theo 看看 UPDATE 部分
  • @Theo 阅读了这篇文章 - stackoverflow.com/questions/15867148/…
猜你喜欢
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2011-11-18
  • 1970-01-01
  • 2020-05-17
  • 2011-09-26
相关资源
最近更新 更多