[英]Dispose a JFrame on button click from another JFrame
As many probably know, I am a complete Java newbie.很多人可能都知道,我是一个完整的 Java 新手。 I have already attempted to research this (by reading other posts on StackOverflow, Googling and asking a friend who is less of a java newbie) but I can't figure it out.
我已经尝试过对此进行研究(通过阅读 StackOverflow 上的其他帖子、谷歌搜索并询问一个不太熟悉 Java 新手的朋友),但我无法弄清楚。 The answer is probably clear and easy but I am blind to it.
答案可能很清楚也很容易,但我对此视而不见。 I am attempting to dispose a
JFrame
from a different frame.我正在尝试从不同的框架中处理
JFrame
。
My application is supposed to work as follows:我的应用程序应该如下工作:
Getting frame Z to dispose frame Y is where most of my issues are.让框架 Z 处理框架 Y 是我的大部分问题所在。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 Ideally help will be phrased in such a way that even a baby could understand (because that is my level of Java comprehension).
理想情况下,帮助的措辞应使婴儿也能理解(因为这是我对 Java 的理解水平)。
I know many will think this is a duplicate question of either this question or this other question .我知道很多人会认为这是这个问题或另一个问题的重复问题。 I believe it is not a duplicate question because I have read both and have not understood how to resolve my own problem.
我相信这不是一个重复的问题,因为我已经阅读了这两篇文章并且不明白如何解决我自己的问题。
- Frame Z has a button, when pressed: performs method from frame Y before disposing frame Y and itself.
框架 Z 有一个按钮,按下时:在配置框架 Y 和自身之前,从框架 Y 执行方法。
Frame Y and frame Z should be modal dialogs (at least Z should be, probably also Y).框架 Y 和框架 Z 应该是模态对话框(至少 Z 应该是,也可能是 Y)。
When dialog Y goes to open dialog Z (we'll call it dialogZ
) the code should go something like this:当对话框 Y 打开对话框 Z(我们称之为
dialogZ
)时,代码应该是这样的:
DialogZ dialogZ = new DialogZ(..);
dialogZ.setVisible(true);
this.setVisible(false); // at this point, dialogZ will have been closed
See How to Use Modality in Dialogs for details and example code.有关详细信息和示例代码,请参阅如何在对话框中使用模态。
(Frame Z) … performs method from frame Y
(Frame Z) … 从帧 Y执行方法
Frame Y and Frame Z should probably not extend any class.框架 Y 和框架 Z 可能不应该扩展任何类。 Instead they should be instance variables that are used as needed.
相反,它们应该是根据需要使用的实例变量。
you could hold a reference to the other jframe in another frame.您可以在另一个框架中保存对另一个 jframe 的引用。 this class could look like this:
这个类可能是这样的:
The constructor takes a jframe, which should be controlled from this jframe.构造函数接受一个 jframe,它应该由这个 jframe 控制。
class YourFrame {
public YourFrame(JFrame controlFrame){
//build the frame and a button, which action listener calls controlFrame.setVisible(false);
JFrame f = new JFrame();
f.setSize(800, 600);
JPanel content = new JPanel();
JButton button = new JButton();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
controlFrame.setVisible(false);
}
}
content.add(button);
f.add(content);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.