Java 单击 JButton 后关闭 jFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18931261/
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
Closing jFrame after clicking JButton
提问by Ankush Pruthi
I have designed two JFrames in NetBeans.
我在 NetBeans 中设计了两个 JFrame。
When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want). In the second JFrame there is a "close" button. But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2.
当我单击“规则”按钮(即放置在 JFrame1 上)时,它会打开第二个 JFrame(但 JFrame2 在 JFrame1 的窗口上打开,这是我不想要的)。在第二个 JFrame 中有一个“关闭”按钮。但是当我单击此按钮时,我希望 JFrame1 被打开并且它也可以工作,但是 JFrame2 实际上没有关闭并且 JFrame1 出现在 JFrame2 上。
In short the main form is JFrame1. When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (i.e JFrame1) is lauched but it is launched over JFrame2.
总之主要形式是JFrame1。当我单击 JFrame1 中的“规则”按钮时,它会在 JFrame1 上打开 JFrame2,而在 JFrame2 中,单击主窗体(即 JFrame1)时会出现一个“关闭”按钮,但它是通过 JFrame2 启动的。
The scenerio is JFframe1 -> JFrame2 -> JFrame1
场景是 JFframe1 -> JFrame2 -> JFrame1
Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa.
现在我的问题是单击“规则”按钮后,JFrame1 应该关闭,JFrame2 显示在屏幕上,反之亦然。
采纳答案by Levenal
Assuming your button has an actionListener, after clicking the "rules button" put in:
假设你的按钮有一个 actionListener,点击“规则按钮”后输入:
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
And then reverese them for the opposite reaction
然后颠倒它们以进行相反的反应
回答by Aleksey Dz
Somethig like this should be on the constructor or method which create JFrame2:
像这样的事情应该在创建 JFrame2 的构造函数或方法上:
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//call another method in the same class which will close this Jframe
CloseFrame();
}
});
It's method which should close JFrame2
这是应该关闭 JFrame2 的方法
public void CloseFrame(){
super.dispose();
}
回答by Delta3
I'm not an expert by any means, however, I ran into this problem as well. If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame.
我无论如何都不是专家,但是,我也遇到了这个问题。如果您将第二个 JFrame 设置为隐藏,当您点击“取消”时,它将关闭第二个 JFrame。
//this is the code for the "cancel" button action listener
public void actionPerformed(ActionEvent e) {
setVisible(false);//hides the second JFrame and returns to the primary
回答by Gunshield_XD
this worked for me (Frame1
Called RegScreen
and Frame2
Called MainScreen
):
这对我Frame1
有用(调用RegScreen
和Frame2
调用MainScreen
):
RegScreen.this.setVisible(false);
new MainScreen().setVisible(true);
Hope that this helps :) Regscreen
was the original frame open at startup.
希望这会Regscreen
有所帮助:)是在启动时打开的原始框架。
回答by Guermou Di Omar
If this doesn't work, try this
如果这不起作用,试试这个
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
回答by Guermou Di Omar
- Have a MainClass with a main() method.
- Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
- After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods.
- 有一个带有 main() 方法的 MainClass。
- 让具有 main() 方法的 MainClass 封装您的 JFrame1 和 JFrame2 引用变量。除非有特定原因,否则不要让 JFrame1 或 JFrame2 包含 main()。
- 在 JFrame 对象之一中单击某些内容后,实例化/使另一个 JFrame 对象可见,并通过 MainProgram.JFrame 对象方法对其进行处理。
Example:
例子:
//btn event inside 1st JFrame/window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
MainProgram.openResultsForm(); //MainProgram opens 2nd window
MainProgram.queryEntryForm.dispose(); //MainProgam closes this,
//the 1st window
}