[英]Java Swing - Why is my first window not disposing/closing?
So I'm creating an application. 所以我正在创建一个应用程序。 I want main to initialize a login window, which I've done.
我希望main初始化一个登录窗口,我已经完成了。 And for now I want the login window to close whenever I click on the button, and when it closes, a new window (called MainWindow) opens with different buttons/info/text.
现在,我希望每次单击按钮时都关闭登录窗口,并且在关闭时,会打开一个包含不同按钮/信息/文本的新窗口(称为MainWindow)。
All these classes are separate, but in the same package. 所有这些类都是单独的,但是在同一包中。
The problem is: The main window opens when I click login, however the login window doesn't terminate/close. 问题是:当我单击登录时,主窗口会打开,但是登录窗口不会终止/关闭。
My main method, from which I call the login window: 我的主要方法,从其中调用登录窗口:
import javax.swing.*;
public class Main {
// Display Login Window
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new LoginWindow();
LoginWindow.createAndShowLoginGUI();
});
}
}
My login window class: 我的登录窗口类:
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();
}
}
My Main Window Class: 我的主窗口类:
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();
}
}
If you use this code, when the main window opens, the login window will be right behind it. 如果使用此代码,则在打开主窗口时,登录窗口将紧随其后。
You can use when pressing button 您可以在按下按钮时使用
System.exit(0);
This will terminate your application. 这将终止您的应用程序。 To close one window use(if you have open multiple windows) without affecting to others,
要关闭一个窗口(如果您打开了多个窗口),而又不影响其他窗口,
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
Then, to open new one, 然后,打开一个新的
new frameName().setvisible(true);
Example: 例:
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);
}
});
Take a look at this question . 看一下这个问题 。
UPDATE: 更新:
In below line you have extends JFrame
but you didnot use it. 在下面的行中,您
extends JFrame
但是您没有使用它。
public class LoginWindow extends JFrame implements ActionListener {
Rather than that you have create new object from JFrame
而不是从
JFrame
创建新对象
JFrame loginFrame = new JFrame("Login Window");
All the component you are adding to the loginFrame
. 您要添加到
loginFrame
所有组件。 But when actionPerformed
trying to this.dispose()
, since you are not using extended JFrame
nothing will happen. 但是当
actionPerformed
尝试到this.dispose()
,由于您没有使用扩展的JFrame
不会发生任何事情。
Solution: 解:
Declare your JFrame
as a instance variable: 声明您的
JFrame
作为实例变量:
public class LoginWindow implements ActionListener {
JFrame loginFrame;
public void prepareLoginGUI() {
Then change, 然后改变
this.dispose();
To: 至:
loginFrame.dispose();
In this this.dispose();
在此
this.dispose();
call to the extended JFrame
, no effect for your JFrame
object( loginForm
). 调用扩展的
JFrame
,对您的JFrame
对象( loginForm
)无效。
And one last thing, make sure to remove extends JFrame
. 最后一件事,请确保删除
extends JFrame
。 Because you are not doing anything with extended JFrame
. 因为您没有对扩展
JFrame
做任何事情。 Read this post about extended JFrame
. 阅读有关扩展
JFrame
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.