Java:处理来自不同 JFrame 的 ActionEvent

发布于 2025-01-08 13:50:36 字数 309 浏览 2 评论 0原文

我希望一个 JFrame 具有这样的方法:

private void someEvent(java.awt.event.ActionEvent evt){

    //initialize another JFrame
    //set the new JFrame to be visible
    //set this JFrame to be disabled

}

这是可能的,但我也希望主 JFrame 在新创建的 JFrame 被释放时执行某些操作。但是,我不想将主 JFrame 传递给新的 JFrame。这可能吗?

I want one JFrame to have a method like this:

private void someEvent(java.awt.event.ActionEvent evt){

    //initialize another JFrame
    //set the new JFrame to be visible
    //set this JFrame to be disabled

}

Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以为你会在 2025-01-15 13:50:36

Instead, use CardLayout to switch between the two desired content panes. There's an example here.

少跟Wǒ拽 2025-01-15 13:50:36

不要让一个 JFrame 创建并显示另一个 JFrame。相反,第二个窗口应该是 JDialog,如果您希望第一个窗口冻结直到处理第二个窗口,则可以是模态窗口,否则可以是非模态窗口。如果是模态的,那么一旦 JDialog 被释放,第一个窗口的代码将恢复,并且代码流将在对话框上的 setVisible(true) 调用之后立即开始。如果是非模态的,您可能需要向对话框添加 WindowListener。

例如,请在此处查看我的代码,此处,以及此处

Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.

For example, please check out my code here, here, and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文