关闭主 JFrame
我有一个主 jFrame,借助它我按下按钮并打开新的 JFrame,但问题是,当我打开其他 JFrame 时,以前的 JFrame 仍然保留在那里,因为我想要的是,当我按下下一个按钮时,我会继续前进新的 JFrame(前一个 JFrame 不应该在那里),当我按上一个按钮时,我会移回到前一个 JFrame。
我知道有 dispose 函数,它们做得很好,就像 jframe1.dispose() 但我不知道如何隐藏第一个 JFrame,其主代码中的代码是这样写的,
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new GraphicalInterface().setVisible(true);
}
});
}
我如何将其设置为 .setVisible(false)按钮代码?
I have a main jFrame with the help of which i press button and open new JFrames but the problem is that when i open other JFrame the previous ones still remains there where as what i want is that when i press next button then i move forward to the new JFrame (the previous one should not be there) and when i press previous button i move back to the previous JFrame.
I know there are functions of dispose,they do well like jframe1.dispose() but i dont get it how to hide the very first JFrame whose code in the main is written like this
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new GraphicalInterface().setVisible(true);
}
});
}
how do i set this as .setVisible(false) in the button code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要保留对
JFrame
的引用,以便稍后设置它的可见性。请注意,
JFrame
是一个顶级容器,每个应用程序实际上只应该拥有一个。如果不使用多个JFrame
,而只使用一个,然后交换到不同的JPanel
,可能会更好。You need to retain a reference to the
JFrame
, so you can set it's visibility later.Note that a
JFrame
is a top-level container, you are only really supposed to have one per application. It may be better if instead of using multipleJFrame
s you use just one, and swap in variousJPanel
s instead.如果您只保留一帧并设置一个新面板作为内容,这将保护资源。
您的问题是如何处理对框架的引用?请提供创建下一帧的代码。
It would safe resources if you keep just one frame and set a new panel as content.
Your question was how to handle the reference to the frame? Please provide the code where the next frame is created.
您可以将 GUI(我想是扩展 JFrame)分配给一个变量并在该对象上调用 .setVisible(false) 。由于上面代码中的对象或多或少是匿名的,因此您将无权访问它。
您还可以检查这个。
You could assign your GUI (extends JFrame I suppose) to a variable and call .setVisible(false) on that object. Since your object from the code above is more or less anonymous, you won't have access on that.
You could also check this.