我刚刚开始学习Java,所以请温柔地对待我。我发现框架和对话框的默认关闭行为令人困惑。
我正在尝试使用Swing创建一个简单的登录对话框。我为对话框取了JDialog
。我只是希望当用户点击登录按钮后,在执行身份验证后,我希望关闭登录窗口,但要打开另一个窗口。
但是,如果用户点击关闭按钮,则应关闭登录框,但应用程序也必须结束。
我知道如何编写动作侦听器和身份验证代码。我只需要帮助了解我必须实施哪些近距离行为。
到目前为止,这是我的代码(仅限相关代码段)。更多具体问题出现在动作听众附近的评论中。
package practice.bookyard.client;
import javax.swing.SwingUtilities;
public class Program {
public static void main(String[] args) {
SwingUtilities.invokeLater(new LoginDialogEventLoop());
}
}
public class LoginDialogEventLoop implements Runnable {
@Override
public void run() {
// Create and set title
JDialog dialog = new LoginDialog("Bookyard Login");
// Set default close behavior
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// Set size
dialog.setSize(400, 300);
// dialog.setResizable(false);
// Set position on the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
dialog.setLocation((int)screenSize.getWidth() / 2, (int)screenSize.getHeight() / 2);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}
public class LoginDialog extends JDialog {
private static final long serialVersionUID = 1L;
protected JLabel lblTopSpace = null;
protected JPanel loginPanel = null;
protected JPanel statusPanel = null;
public LoginDialog(String title) {
super((Dialog)null);
this.setTitle(title);
Initialize();
}
protected void Initialize() {
// DONE: add a panel and add controls to the panel
...
}
}
public class LoginPanel extends JPanel {
private static final long serialVersionUID = 1L;
protected JLabel lblUserName;
protected JLabel lblPassword;
protected JTextField txtUserName;
protected JPasswordField txtPassword;
protected JButton btnLogin;
protected JButton btnCancel;
public LoginPanel() {
super();
this.Initialize();
}
protected void Initialize() {
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
// Create controls
lblUserName = new JLabel("User Name: ");
lblPassword = new JLabel("Password: ");
txtUserName = new JTextField(15);
txtPassword = new JPasswordField(15);
txtPassword.setEchoChar('*');
btnLogin = new JButton("Login");
btnCancel = new JButton("Cancel");
// Set them on the panel using the constraints
// 3 rows, 3 columns
// DONE ALREADY
...
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// authenticate user: I know how to do this
// how do I close the login JDialog
// of course, I will need a reference to the dialog here
// which is not a big deal. I can have that be passed
// in via a constructor.
// So, supposing this 'dialog' variable represented the
// login dialog
JDialog dialog = new JDialog((Dialog)null);
// What's the method to close this window?
// I believe it is dispose()
// But will calling the dispose method also
// exit the main thread since this is the
// only window in the application?
// Also, should I be launching the new window from here?
// or from within the main() of the application?
// Is there a way this dialog can communicate to its
// creator what the dialog result was -- Login or Cancel?
dialog.dispose();
JFrame frame = new JFrame("New Window");
frame.setSize(400, 400);
frame.setVisible(true);
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// So, supposing this 'dialog' variable represented the
// login dialog
JDialog dialog = new JDialog((Dialog)null);
// This one is a bit easier
// But since this is a JDialog and not a JFrame
// I cannot set the default close behavior
// to EXIT_ON_DISPOSE. I can only set it to:
// HIDE ON DISPOSE
// CLOSE ON DISPOSE
// DO NOTHING ON DISPOSE
// And since I have set it to CLOSE ON DISPOSE in Program.java
// just calling dispose() on this will keep the application
// still alive, right?
dialog.dispose();
}
});
}
}