Java-SWT : How to update Label?

标签 java user-interface swt label project

我是 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/

相关文章:

java - 单击按钮后打开一个新 Activity

Java:如何返回一个数组?

javascript - 为什么我无法禁用按钮?

c++ - 虚拟列表控件 (MFC)

java - SWT 在组中创建滚动组合

java - 添加动态 TextView

iPhone 4 UI 设计对比 iPhone 5

java - SWT:在 Linux 上使用 setEnabled() 后单击按钮出现问题

java - 在 swt/jface 应用程序中使用的属性编辑器 Ui

Java 异步套接字 IO