【发布时间】:2012-05-09 13:46:41
【问题描述】:
我正在编写一个选项面板,为了能够在开发应用程序时更快地添加更多选项,我决定将所有输入的组件放在一个框架中,我需要从配置中加载它们的值并设置相应的文本,但似乎无法从字段中获取组件的文本。
我得到一个:
线程“AWT-EventQueue-0”中的异常 java.lang.RuntimeException:无法编译的源代码 - 错误的 sym 类型:java.awt.Component.setText
名称:服务器类:class javax.swing.JTextField
private void loadConfigs() {
List<Component> compList = getAllComponents(this);
System.out.println("Tamaño "+compList.size());
for(int i=0; i<compList.size();i++) {
if(compList.get(i).getName() != null) {
System.out.println("Nombre: "+compList.get(i).getName() +" Clase:"+ compList.get(i).getClass().toString());
if(compList.get(i).getClass().toString().matches("class javax.swing.JTextField")) {
System.out.println("Machea load " +compList.get(i).getName() + compList.get(i).toString());
compList.get(i).setText(rootFrame.config.get(compList.get(i).getName()));
}
else if(compList.get(i).getClass().toString().matches("class javax.swing.JCheckBox")) {
if (rootFrame.config.get(compList.get(i).getName()) == null) {
compList.get(i).setSelected(false);
}
else {
compList.get(i).setSelected(true);
}
}
}
}
}
public static List<Component> getAllComponents(final Container c) {
Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
compList.add(comp);
if (comp instanceof Container) {
compList.addAll(getAllComponents((Container) comp));
}
}
return compList;
}
【问题讨论】:
-
“为了能够在我开发应用程序时更快地添加更多选项,我决定将所有输入的组件放在一个框架中” “在一个大桶里”,这对我来说同样有意义。框架和快速添加东西有什么关系?
标签: java swing input components edit