2011-03-02 84 views
0

对于我以前的线程Accessing container fields part 1之一,我想问下:访问容器字段部分2

我使用NetBeans和目前有3个组件设计的应用程序:

1)我的主类(mainApp)延伸一个JFrame
2)两个定制类这

前些日子都扩展一个JPanel,我遇到了一些问题,指的我的主类从一个对我JPanels 但希望感谢“气垫船内充满鳗鱼s“的用户我用了以下方法:

app = (mainApp) SwingUtilities.getWindowAncestor(this);

并处理它。

现在我需要从其中一个JPanel中为其他JPanel获取参考。虽然有些奇怪的原因,但似乎我不能这样做..!有任何想法吗?

回答

0

没关系! 使用以下代码解决:
//myFrame is my main class myFrame = (mainApp) SwingUtilities.getWindowAncestor(this);
Component[] comps = myApp.getContentPane().getComponents();

int i =-1;
do{ i++;
}
//canvasPan is one of my custom-made JPanels
while(!(comps[i] instanceof canvasPan));
myCanvas = (canvasPan)comps[i];