起初我是Java的初学者。我在线程标题中提到的checkstyle错误有问题。ActionListener中的Checkystyle问题:引用实例变量'x'需要'this'。在Java中
考虑让similiar代码:
public class myClass {
JButton[] buttons;
public myClass() {
this.buttons = new JButton[2];
//constructor code....
this.buttons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstMethod(0, 1);
secondMethod(5, 10);
}
});
}
public void firstMethod(int x, int y) {
// do something
}
public void secondMethod(int x, int y) {
// do something
}
}
在constructior我从属性buttons
,在点击按钮时,将执行方法firstMethod(int, int)
和secondMethod(int, int)
创建onclick事件的按钮,当然一切工作,但checkstyle会引发错误。 由于某些原因,我不能只使用this.firstMethod()
,因为我在另一个对象(ActionListener)内。
任何想法如何将myClass引用放入actionListener?
可能,但checkstyle错误未在您发布的帖子中提及,因此我没有找到解决方案。 – t4dohx