所以我正在创建一个应用程序。我希望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();
}
}
如果您使用此代码,当主窗口打开时,登录窗口将紧随其后。
答案 0 :(得分: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
。