如何在Composite JButton类的ChangeListener中访问我的toString()方法?

时间:2015-05-04 02:19:02

标签: java swing constructor jbutton

我有一个JButton组合类,我有一个toString,我发送到另一个类的JLabel。当我调用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;

  }
}

1 个答案:

答案 0 :(得分:2)

使用EmptySpace.this.toString()