如何重新启动启动我的应用程序特定类的Java应用程序? [英] How to restart a Java Application launching a specific class of my application?
问题描述
我的情况如下:
我有一个Java Swing 应用程序.
I have a Java Swing application.
在实现GUI的类中,我有一个名为注销的按钮,它绑定到处理click事件的事件侦听器,类似这样:
In the class that implement my GUI I have a button named Log Out tath is binding to an event listener that handle the click event, something like it:
JButton logOutButton = new JButton("LogOut");
header.add(logOutButton);
然后在实现我的GUI的同一类中,我声明了 ActionListener ,它使用内部类来处理此事件:
Then in the same class that implement my GUI I have declared the ActionListener that handle this event using an inner class:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
System.exit(0);
}
});
此刻,当我单击 logOutButton 按钮时,程序结束.我宁愿退出它,也可以通过运行一个名为 LoginForm 的特定类(实现登录表单GUI的类)来重新启动它
In this moment when I click the logOutButton button the program end. I would that instead exit it is restarted by running a specific class called LoginForm (the class that implement the login form GUI)
该怎么办?
Tnx
安德里亚
推荐答案
您根本不需要close/open window
垃圾方法.只需使用卡片布局:
You don't really need to close/open window
junky approach at all. Just use Card Layout:
-
将框架的内容窗格的布局设置为卡片布局.
set Frame's content pane's layout to card layout.
getContentPane().setLayout(new CardLayout());
将您不同的Form的内容代码放入不同的面板中,然后 使用相应的名称将它们添加到内容窗格中,例如:
Put your different Form's content code inside different panel and add them to the content pane with their corresponding name, for example:
getContetnPane().add(logInFormPanel, "logIn Form");
现在,您可以通过调用CardLayout.show(Container parent, String name)
来模拟该卡在必要时显示.例如:
Now you can simulate the card to appear whenever necessary by calling CardLayout.show(Container parent, String name)
. For example:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
CardLayout cl = (CardLayout)(getContentPane().getLayout());
cl.show(getContentPane(), "logIn Form");
}
});
查看 CardLayout
演示 从我的另一个答案中来.
Check out a CardLayout
demo from my another answer.
这篇关于如何重新启动启动我的应用程序特定类的Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!