有什么方法可以知道类是否扩展JWindow
?例如:
class DialogWindow extends JWindow {
}
如何检查DialogWindow
类是否扩展JWindow
类?我需要知道某些组件的父Window
级,这些组件可能放置在某些组件上,而这些组件可能会JPanel
再次放置在某些组件上JPanel
,依此类推DialogWindow
。当然,我可以将父实例参数传递给某个组件,但也许有更好的方法来做到这一点?
尝试像这样使用 instanceof :
if(DialogWindow instanceof JWindow){//must return true in your case
...
}
正确的做法是(当然感谢@Ben :)):
Container window = getParent();
while(!(window instanceof JWindow)){
window = window.getParent();
}
JWindow parent = (JWindow) window;
System.out.println(parent.getClass());
输出是:class ...DialogWindow
超级!
You can try getClass().getSuperClass()
. A similar question was asked here How to get the parent base class object super.getClass()