【发布时间】:2020-01-08 20:14:59
【问题描述】:
标题很混乱,但我想要实现的是调用我类中所有(摆动)按钮/标签的方法。 (使它们看起来都相似)
有点像这样:
for(Button btn: components)
btn.setThisTheme();
其中components[] 是一个JComponent-s 数组。
到目前为止,我已经尝试过:
// at beginning of class
private LinkedList<JComponent> components = new LinkedList<>();
private Field[] fields = ToDo.class.getDeclaredFields().length;
// in constructor
for (Field field: fields) {
if(field.getType() == JComponent.class) {
components.add(field); // how to do this?
// field is a Field and i need to convert it into the variable it represents...
}
}
【问题讨论】:
标签: java class nullpointerexception field gettype