gpt4 book ai didi

Java-SWT : How to update Label?

转载 作者:行者123 更新时间:2023-12-01 13:01:56 27 4
gpt4 key购买 nike

我是 Java SWT 新手。我创建了游戏“2048”(通过 MVP 模型),我尝试更新 Gui 中的分数标签 - 我不知道为什么,但分数没有更新。我不确定如何使用所有监听器以及哪一个最适合此操作,您能帮助我吗?

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);


}

最佳答案

要更新标签,您只需调用 label.setText,就像您所做的那样。如果你想在分数改变时执行此操作,最简单的方法就是在 displayScoreSetScore 中调用 label.setText (正常的 Java 名称是 setScore)。这不符合 MVP 模式,但我在你的代码中没有看到它。

关于Java-SWT : How to update Label?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23454221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com