我有一个 JButton 组合类,并且有一个要发送到不同类中的 JLabel 的 toString。当我调用 toString 时,我得到类似以下内容: javax.swing.DefaultButtonModel@f730d2f
public class EmptySpace{
private JButton button;
protected int x;
protected int y;
protected String name;
public EmptySpace(String text, int x, int y){
this.name = text;
this.x = x;
this.y = y;
button = new JButton(text);
button.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isRollover()) {
Board.toStringText.setText(e.getSource().toString()); //Where toString is called
} else if (model.isPressed()) {
}
}
});
}
public String toString(){ //toString I want to access
return "Name: " + name + " Xcoords: " + x + " Ycoords: " + y;
}
public JButton getButton(){
return button;
}
}
最佳答案
使用EmptySpace.this.toString()
。
关于java - 如何在 Composite JButton 类的 ChangeListener 中访问 toString() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30021677/