简体   繁体   English

在另一个JPanel中访问一个JPanel

[英]Accessing a JPanel inside another JPanel

I have multiple panels nested inside other panels and I'd like to get a JPanel from there. 我在其他面板中嵌套了多个面板,我想从那里获得一个JPanel

I've tried: 我试过了:

Component[] components = this.getComponents();

for both JFrame and a JPanel but it returns me only javax.swing.JRootPane and not any JPanels . 对于JFrame和JPanel而言,但它仅返回javax.swing.JRootPane而不返回任何JPanels

Is there another function I am not aware of that would return all JPanels inside JFrame or JPanel? 我是否不知道还有另一个函数会返回JFrame或JPanel中的所有JPanels

You're accessing the JFrame's JRootPane, a component that holds a JLayeredPane, a component which holds the contentPane, a component which holds your JPanels: 您正在访问JFrame的JRootPane,一个包含JLayeredPane的组件,一个包含contentPane的组件,一个包含您的JPanels的组件:

在此处输入图片说明 * Reference * 参考

I suggest that you not do what you're trying to do since your method of getting references is very brittle and will break if you change the structure of your GUI. 我建议您不要做您想做的事情,因为获取引用的方法非常脆弱,并且如果您更改GUI的结构,它会中断。 Instead use fields to hold the key references that you need access to, be they JPanel fields or collection fields such as an ArrayList, or a field that references objects of your own classes. 而是使用字段来保存您需要访问的关键引用,这些字段是JPanel字段或集合字段(例如ArrayList)或引用您自己类的对象的字段。

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

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