Java查找JFrame属于JPanel的内容

Java查找JFrame属于JPanel的内容,java,swing,jframe,Java,Swing,Jframe,我有一个多个jpanel,它们被放在一个对话中,几个小时后,我仍然无法找到jpanel存储在其中的帧。我想知道是否有一种方法可以返回JFrame(最终目标是在JFrame上调用setDefaultCloseOperation())。我想getParent()可以做到这一点,但是无论我经历了多少层父级,我仍然无法调用setDefaultCloseOperation。有一个实用方法: 如果将JPanel添加到JFrame中,它显然是一个JFrame实例: JFrame f = (JFrame) S

我有一个多个
jpanel
,它们被放在一个对话中,几个小时后,我仍然无法找到
jpanel
存储在其中的帧。我想知道是否有一种方法可以返回
JFrame
(最终目标是在
JFrame
上调用
setDefaultCloseOperation()
)。我想
getParent()
可以做到这一点,但是无论我经历了多少层父级,我仍然无法调用
setDefaultCloseOperation

有一个实用方法:

如果将
JPanel
添加到
JFrame
中,它显然是一个
JFrame
实例:

JFrame f = (JFrame) SwingUtilities.getWindowAncestor(panel);

注意:
getWindowSenentor()
windowForComponent()
提供相同的功能。

有一个实用方法:
Window window = SwingUtilities.windowForComponent(...);

如果将
JPanel
添加到
JFrame
中,它显然是一个
JFrame
实例:

JFrame f = (JFrame) SwingUtilities.getWindowAncestor(panel);
注意:
getWindowConsentor()
windowForComponent()
提供相同的功能。

请参阅
Window window = SwingUtilities.windowForComponent(...);