【发布时间】:2014-05-09 06:10:20
【问题描述】:
我有一个 Swing GUI 应用程序,其中包含 JTabbedPane 和多个面板。 它有大约 9 个 Jpanel,在第一个 JPanel 中有四个 Jpanel,这些 Jpanel 包含一些摆动组件。 我已经设置了这些面板名称。
我的问题是:我可以在第一个选项卡面板中读取这些组件,但问题是无法获取面板的名称并继续进行。
代码如下:
1.,示例类:
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class Sample {
public Sample() {}
public List<Component> getComponents(int id , Object obj) {
List<Component> result = new ArrayList<Component>();
if (id == 1 && obj instanceof ExampleTab1) {
Component[] component =((ExampleTab1)obj).getContentPanel().getComponents();
for (Component comp : component) {
if (comp instanceof JPanel) {
String compName = ((JPanel)comp).getName().toString();
if (compName.equals("panelResult")) {
//do the stuff
}
}
}
}
return result;
}
}
2.、ExampleTab1 类:
import javax.swing.JPanel;
public class ExampleTab1 {
public ExampleTab1() { }
public JPanel getContentPanel() {
JPanel contentPane = new JPanel();
//all the components added to the panel
return contentPane;
}
}
【问题讨论】:
-
当你运行代码时会发生什么?
-
为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。
-
结果没有添加到 panelResult,但是当我列出组件时,我会得到所有组件
标签: java swing jpanel jtabbedpane