今天突然想到这样一个问题 class MyButton extends JButton{
boolean clicked;
public void buttonClicked(MyButton target){
target.clicked =! clicked;
}
public MyButton(){
clicked = false;
addMouseListener(new MouseAdapter(){
buttonClicked(this);
}
}
}这里最后一行的this 用法是错的,因为目前this指向的是那个MouseAdapter的一个对象,我的问题是,如何在this这个地方做手脚,可以指向我这个MyButton对象呢?
boolean clicked;
public void buttonClicked(MyButton target){
target.clicked =! clicked;
}
public MyButton(){
clicked = false;
addMouseListener(new MouseAdapter(){
buttonClicked(this);
}
}
}这里最后一行的this 用法是错的,因为目前this指向的是那个MouseAdapter的一个对象,我的问题是,如何在this这个地方做手脚,可以指向我这个MyButton对象呢?
修改为
buttonClicked(MyButton.this);
MyButton button = this;