如何在 Composite JButton class 的 ChangeListener 中访问我的 toString() 方法?
How would I access my toString() method in a ChangeListener in my Composite JButton class?
我有一个 JButton 组合 class 并且我有一个要发送到另一个 class 中的 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()
.
我有一个 JButton 组合 class 并且我有一个要发送到另一个 class 中的 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()
.