gpt4 book ai didi

java - 如果窗口之前没有经历过 Java 的登录阶段,如何防止窗口加载?

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:11 25 4
gpt4 key购买 nike

我用Java编写了一个程序,它有两个窗口(JFrame):

a) 登录窗口,

b) 主窗口。

运行程序的正确方法是首先运行“登录窗口”,如果用户名和密码正确,则该窗口会将您重定向到“主窗口”,从而使“登录窗口”处置。

但是

如果有人直接运行“主窗口”(忽略登录阶段),那么该窗口将无需任何身份验证即可正确运行...

所以

如果有人直接运行“主窗口”(例如>> java Main_Window),如何防止其加载和运行?我希望我的程序按以下顺序运行:(1)“登录窗口”,然后(2)“主窗口”。

编辑

Main_Window 类只有一个 main 方法,代码如下:

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
public void run() {
try {

final Main_Window frame = new Main_Window();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
toRunOnClose();
frame.dispose();
}

});

} catch (Exception e) {
e.printStackTrace();
}

}

});

}

最佳答案

建议:

  • 您的登录窗口应该是一个模态对话框,例如 JDialog,而不是 JFrame。
  • 您的 GUI 应该只有一个 主要方法,该方法首先调用您的登录模式对话框,然后如果成功,则在此之后打开主 JFrame。

主要的 main 方法可能类似于:

   private static void createAndShowGui() {
LoginDialog loginDialog = new LoginDialog();
loginDialog.setVisible(true);

// however you verify that the user is authentic
if (loginDialog.verify()) {
MainGui mainGui = new MainGui();
mainGui.setVisible(true);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}

在上面的示例中,由于 LoginDialog 是模态的,因此在对话框窗口不再可见之前,loginDialog.setVisible(true) 下面的代码不会运行。

<小时/>

关于您的问题:

1) Run some code at the loading time of the main window,

在单个主方法中运行它。

2) How can i set the look and feel (later) without a main method

您将有一个主要方法,但只有一个主要方法。

3) How can I run some code when the frame closes (as i already doing now)?

做同样的事情,但是在一个主方法中。

关于java - 如果窗口之前没有经历过 Java 的登录阶段,如何防止窗口加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27171802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com