在从另一个 JFrame 单击按钮时释放一个 JFrame

2022-01-24 00:00:00 java swing jframe dispose

可能很多人都知道,我是一个完整的 Java 新手.我已经尝试对此进行研究(通过阅读 StackOverflow 上的其他帖子、谷歌搜索并询问一个不太懂 java 新手的朋友),但我无法弄清楚.答案可能很清楚也很容易,但我对此视而不见.我正在尝试从不同的框架中处理 JFrame.

As many probably know, I am a complete Java newbie. 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. The answer is probably clear and easy but I am blind to it. I am attempting to dispose a JFrame from a different frame.

我的应用程序应该如下工作:

My application is supposed to work as follows:

  • 框架 X 有一个按钮,按下时:生成框架 Y
  • 框架 Y 有一个按钮,按下时:生成框架 Z
  • 框架 Z 有一个按钮,按下时:从框架 Y 执行方法,然后再处理框架 Y 及其自身.

让框架 Z 处理框​​架 Y 是我的大部分问题所在.任何帮助是极大的赞赏.理想情况下,帮助应该以即使是婴儿也能理解的方式来表达(因为这是我对 Java 的理解水平).

Getting frame Z to dispose frame Y is where most of my issues are. 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).

我知道很多人会认为这是 this question 或其他问题.我相信这不是一个重复的问题,因为我已经阅读了这两个问题并且不明白如何解决我自己的问题.

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.

推荐答案

  • 框架 Z 有一个按钮,按下时:从框架 Y 执行方法,然后再处理框架 Y 及其自身.

框架 Y 和框架 Z 应该是模态对话框(至少 Z 应该是,也可能是 Y).

Frame Y and frame Z should be modal dialogs (at least Z should be, probably also Y).

当对话框 Y 进入打开对话框 Z(我们将其称为 dialogZ)时,代码应如下所示:

When dialog Y goes to open dialog Z (we'll call it dialogZ) the code should go something like this:

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) ... 执行方法从帧 Y

(Frame Z) … performs method from frame Y

Frame Y 和 Frame Z 应该不扩展任何类.相反,它们应该是根据需要使用的实例变量.

Frame Y and Frame Z should probably not extend any class. Instead they should be instance variables that are used as needed.

相关文章