Java 应用程序重新启动或重置按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26075081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:48:24  来源:igfitidea点击:

Java Application Restart or Reset Button

javabuttonjlabelresetrestart

提问by MattS

I have a very simple, choose your path, Java game that I am working on in the NetBeans IDE. Basically what happens is the user will click one of the three buttons, and pre-made JLabel's which are set to ""(nothing) will be reset to whatever text I decide that label should then use. I do this by adding the code below.

我有一个非常简单的选择你的路径,我正在 NetBeans IDE 中开发的 Java 游戏。基本上发生的事情是用户将单击三个按钮之一,并且JLabel设置为""(无)的预制将被重置为我决定该标签应该使用的任何文本。我通过添加下面的代码来做到这一点。

private void option1ActionPerformed(java.awt.event.ActionEvent evt) {
    jLabel6.setText("You go down the dark tunnel...");
}

Now all this works fine but I'd like a way to restart/reset my application by clicking a button labelled "restart". I don't mind if it has to close the application and then open it again or if it will simply reset all the JLabel's back to "". I just don't want to have to type out the code below for every single JLabel.

现在所有这些工作正常,但我想要一种通过单击标记为“重新启动”的按钮来重新启动/重置我的应用程序的方法。我不介意它是否必须关闭应用程序然后再次打开它,或者它是否只是将所有 JLabel 重置为“”。我只是不想为每个 JLabel 输入下面的代码。

jLabel1.setText("");
jLabel2.setText("");
jLabel3.setText("");

I have done some research on this site and none of the code that people provide seems to work for my situation, either because it simply doesn't work or because I am doing something wrong. Any help would be greatly appreciated and please try and be specific because I am fairly new to writing Java and don't understand everything.

我在这个网站上做了一些研究,人们提供的代码似乎都不适用于我的情况,要么是因为它根本不起作用,要么是因为我做错了什么。任何帮助将不胜感激,请尝试具体说明,因为我对编写 Java 还很陌生并且不了解所有内容。

It would also work for me if someone could provide a way for me to close 1 window in an application instead of the whole thing, like when

如果有人可以为我提供一种方法来关闭应用程序中的 1 个窗口而不是整个窗口,那么它也对我有用,例如

System.exit(0);

is used.

用来。

采纳答案by MattS

Found the answer to my own question:

找到了我自己问题的答案:

Thanks to Holger for suggesting "dispose". This is what I found worked.

感谢 Holger 建议“处理”。这就是我发现的工作。

private void restartButtonActionPerformed(ActionEvent evt)  {
    if(evt.getSource() == restartButton)
    {
        dispose();
        MyGUI game = new MyGUI();
        game.setVisible(true);
    }
}

回答by jangler

First, I recommend importing the JLabel class specifically so you can write JLabelinstead of javax.swing.JLabel:

首先,我建议专门导入 JLabel 类,以便您可以编写JLabel而不是javax.swing.JLabel

import javax.swing.JLabel;

Instead of declaring each JLabel individually, create an Array of JLabels:

不是单独声明每个 JLabel,而是创建一个 JLabel 数组:

JLabel[] jLabels = new JLabel[N]; // where N is the number of JLabels

Whenever you need to access a JLabel, use:

每当您需要访问 JLabel 时,请使用:

jLabels[6].setText("You go down the dark tunnel...");

When you want to reset all the JLabels, use:

当您想重置所有 JLabels 时,请使用:

for (JLabel jLabel : jLabels) {
    jLabel.setText("");
}

For more details on Arrays, read http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

有关数组的更多详细信息,请阅读http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

回答by Thusitha Thilina Dayaratne

If you are having your program as a executable jar file then you can use

如果你的程序是一个可执行的 jar 文件,那么你可以使用

public void restert(){
     Runtime.getRuntime().exec("java -jar yourapp.jar");
     System.exit(0);
}

If you want to know more about restarting java application you can go through this

如果您想了解有关重新启动 Java 应用程序的更多信息,可以通过