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(...);