frame
是JFrame
我的 Swing 应用程序中唯一的。由于JFrame
扩展Window
,我从描述和方法名称中相信代码应该返回框架本身。
SwingUtilities.windowForComponent(frame)
public static Window windowForComponent(组件aComponent)
返回组件的窗口
但它返回null
,因为实现是这样的
public static Window windowForComponent(Component c) {
return getWindowAncestor(c);
}
public static Window getWindowAncestor(Component c) {
for(Container p = c.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
return (Window)p;
}
}
return null;
}
您是否同意方法实现不精确?
UPD:我的意思是 JFrame 被传递给方法windowForComponent
,JFrame
扩展Window
所以应该有额外的检查,比如
if (c instanceof Window) return (Window)c; //in windowForComponent
UPD2:所以我必须实施
public static Window windowForComponent (Component c) {
if (c instanceof Window)
return (Window)c;
return SwingUtilities.windowForComponent(c);
}