我正在编写一个选项面板,为了在开发应用程序时能够更快地添加更多选项,我决定将所有输入的组件放在一个框架中,我需要从配置中加载它们的值并设置相应的text 但似乎无法从字段中获取组件的文本。我得到一个:
线程“AWT-EventQueue-0”中的异常 java.lang.RuntimeException:无法编译的源代码 - 错误的符号类型:java.awt.Component.setText
名称:服务器类:类 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;
}