简体   繁体   English

如何知道某个类是否扩展了JWindow类?

[英]How to know if some class extends JWindow class?

Is there some method how to know if class extends JWindow ? 有一些方法如何知道类是否扩展了JWindow For example: 例如:

class DialogWindow extends JWindow {
}

How to check if DialogWindow class extends JWindow class? 如何检查DialogWindow类是否扩展了JWindow类? I need to know the parent Window of some component which may be placed on some JPanel which could be placed again on some JPanel , and so on on top of DialogWindow . 我需要知道某些组件的父Window ,该组件可以放在某些JPanel上,而这些JPanel可以再次放在某些JPanel ,依此DialogWindow Of course, I can pass parent instance argument to some component, but maybe there is some better way to do it? 当然,我可以将父实例参数传递给某些组件,但是也许有更好的方法吗?

Try using instanceof like this : 尝试像这样使用instanceof:

if(DialogWindow instanceof JWindow){//must return true in your case
...
}

The right way to do it is (thanks to @Ben of course :) ): 正确的方法是(当然感谢@Ben :)):

Container window = getParent();

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

JWindow parent = (JWindow) window;

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

And the output is: class ...DialogWindow Super! 输出为: class ...DialogWindow Super!

You can try getClass().getSuperClass() . 您可以尝试getClass().getSuperClass() A similar question was asked here How to get the parent base class object super.getClass() 在这里提出了类似的问题, 如何获得父基类对象super.getClass()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM