2

让我先解释一下情况,我有一个名为 panelclass 的 JPanel 类。它的方法 getPanel() 返回 JPanel。

在名为 frameclass 的 JFrame 类中,我创建了一个新的 panelclass 对象,获取它的面板并将其添加到框架窗格中。

我想要实现的是,当单击窗格类中的按钮时,它应该关闭这个 JFrame ie.frameclass。我不知道面板类如何与框架类通信以关闭。我尝试了 this.dispose() 和 super.dispose() 但即使在扩展 JFrame 之后也没有成功

有没有更简单的方法?请帮忙。

4

2 回答 2

3

有几个是实现这一点,但最简单的可能是通过使用SwingUtilities.getWindowAncestor(Component)

这将返回Window组件被添加到的那个或者null它没有父窗口。从那里您可以简单地调用Window#dispose以关闭框架。

于 2013-07-30T05:39:02.217 回答
1

when a button in paneclass is clicked, It should close this JFrame

See Closing an Application. I prefer using something like the `ExitAction' described there. The reason is that your application will behave just like the user clicked on the close button of the frame which means that if you have any WindowListeners added to the window they will be invoked.

于 2013-07-30T14:51:28.883 回答