当我调用此方法时,我试图更改 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 声明为公共(public),但 BoardWrapper 类无法解析它
public JButton namedisplay = new JButton("Unit");
最佳答案
将您的按钮声明为公共(public)静态,并通过在前面添加其类名来调用它。 因此,如果您的 Button 在 A 类中并且您想从 B 类访问它,您可以这样做:
A.buttonName.setText("abc");
记住将其声明为静态。
public static Button buttonName = new Button ("unit");
关于java - 如何使用单独类中的方法更改 JButton 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58160847/