我是Java SWT的新手。我创建了游戏“2048”(通过MVP模型),我尝试更新Gui中的Score标签 - 我不知道为什么但是得分没有更新。我不确定如何使用所有Listners以及哪一个对这个动作有好处,你能帮助我吗?
public void buttons() {
Group group = new Group(this.shell, SWT.SHADOW_OUT);
final Label label2 = new Label(group, SWT.BORDER);
Button UndoMove = new Button(group, SWT.PUSH);
Button RestartGame = new Button(group, SWT.PUSH);
Button LoadGame = new Button(group, SWT.PUSH);
Button SaveGame = new Button(group, SWT.PUSH);
group.setLayout(new GridLayout(1, false));
**int i = board.getScore();
label2.setText("Your Score is: " + i);
label2.update();**
public class GameBoard extends Canvas {
protected int N;
protected Tile[][] tile;
int[][] boardData;
protected int Score = 0;
protected int MaxScore = 0;
public GameBoard(Composite arg0, int arg1, int n) {
super(arg0, arg1);
N = n;
boardData = new int[N][N];
setLayout(new GridLayout(N, true));
tile = new Tile[N][N];
}
public void initilizeValueInTile() {
if (boardData != null) {
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
if (tile[i][j] == null) {
tile[i][j] = new Tile(this, SWT.BORDER);
tile[i][j].setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
}
tile[i][j].setValue(boardData[i][j]);
}
}
}
public void setBoard(int[][] data) {
boardData = data;
initilizeValueInTile();
}
**
public void SetScore(int s) {
Score = s;
}
public int getScore() {
return Score;**
}
public class GameView extends Observable implements View
@Override
public void displayScore(int score) {
System.out.println(score);
board.SetScore(score);
}
答案 0 :(得分:1)
要更新标签,您只需像以前一样致电label.setText
。如果您想在更改分数时执行此操作,最简单的方法就是在label.setText
或displayScore
中调用SetScore
(正常的Java名称为setScore
) 。这并不适合MVP模式,但我还是无法在你的代码中看到它。