我在处理 JApplet 中的容器和组件时遇到问题。 我没有放置我的代码,因为我认为没有必要。
主类拥有 getContentPane()。
另一个类(我们称之为 class1)拥有 JLayeredPane() 和添加到其中的组件。
另一个类(我们称之为 class2)仅具有要显示的组件。
在init()中,主类添加了class1的JLayeredPane()。
稍后,class1 将其组件添加到其 JLayeredPane() 中,并从 class2 创建一个新对象。
Class2 应该生成可变数量的组件(组件的数量及其属性随时间变化),但无法将它们添加到 class1 的 JLayeredPane() 中。
如何显示 class2 组件?
感谢您的阅读。
最佳答案
Three ways 1. Pass JLayeredPane() to the class2's constructor or a method 2. Class2 has methods that return the components that Class1 can add to JLayeredPane() 3. Pass Class1's object to class2 which will call Class1's method through a known interface that Class1 implements (a callback)
关于java - 容器类所有者的子对象必须将组件添加到其父级容器中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1690998/