0

有什么方法可以知道类是否扩展JWindow?例如:

class DialogWindow extends JWindow {
}

如何检查DialogWindow类是否扩展JWindow类?我需要知道某些组件的父Window级,这些组件可能放置在某些组件上,而这些组件可能会JPanel再次放置在某些组件上JPanel,依此类推DialogWindow。当然,我可以将父实例参数传递给某个组件,但也许有更好的方法来做到这一点?

4

3 回答 3

2

尝试像这样使用 instanceof :

if(DialogWindow instanceof JWindow){//must return true in your case
...
}
于 2013-08-28T17:24:31.963 回答
1

正确的做法是(当然感谢@Ben :)):

Container window = getParent();

    while(!(window instanceof JWindow)){
        window = window.getParent();
    }

JWindow parent = (JWindow) window;

System.out.println(parent.getClass());

输出是:class ...DialogWindow 超级!

于 2013-08-28T17:36:35.270 回答
0

You can try getClass().getSuperClass() . A similar question was asked here How to get the parent base class object super.getClass()

于 2013-08-28T17:23:39.420 回答