我有一个组件,我在独立的 Java 应用程序和 Java 小程序中都使用它。如何从组件中确定我的组件是否在小程序中?
另外,一旦我发现我在 Applet 中运行,我如何才能访问 Applet?
我认为您应该能够通过反复调用Component.getParent()
直到到达容器树的顶部,然后检查该容器是否为instanceof Applet
.
下面的代码完全未经测试:
boolean isInAnApplet(Component c)
{
Component p = c.getParent();
if (p != null) {
return isInAnApplet(p);
} else {
return (c instanceof Applet);
}
}