当我调用此方法时,我试图更改JButton上的文本
public Piece select() {
if (this.unit_here != null) {
namedisplay.setText(this.unit_here.name);
}
}
这是方法所在的完整类
import java.util.ArrayList;
public class BoardWrapper {
private Piece unit_here;
private String terrain_here;
private int terrain_duration;
private int x;
private int y;
public BoardWrapper(int x2, int y2) {
x = x2;
y = y2;
}
public Piece select() {
if (this.unit_here != null) {
namedisplay.setText(this.unit_here.name);
}
}
}
我已将JButton声明为公共,但BoardWrapper类无法解析它
public JButton namedisplay = new JButton("Unit");
声明您的按钮public static,并通过在其前面添加其类名来对其进行调用。因此,如果您的Button在A类内部,并且您想从B类访问它,则可以这样操作:
A.buttonName.setText("abc");
记住要声明为静态。
public static Button buttonName = new Button ("unit");