简体   繁体   English

Java-SWT:如何更新标签?

[英]Java-SWT: How to update Label?

I'm new at Java SWT. 我是Java SWT的新手。 I created the game"2048" ( by MVP model), and I try to update the Score label in the Gui - I don't know why but the score doesn't updated. 我创建了游戏“ 2048”(按MVP模型),我尝试更新Gui中的“分数”标签-我不知道为什么,但是分数没有更新。 I'm not sure how to use all the Listners and which one will be good for this action, can you help me? 我不确定如何使用所有侦听器,哪一个适合进行此操作,您能帮我吗?

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


        }

To update the label you just need to call label.setText , as you do. 要更新标签,您只需调用label.setText If you want to do it when the score is changed, the simplest approach is just to call label.setText in displayScore or in SetScore (the normal Java name would be setScore ). 如果要在更改分数时执行此操作,最简单的方法是在displayScorelabel.setText中调用SetScore (正常的Java名称为setScore )。 This doesn't fit into MVP pattern, but I don't see it in your code anyway. 这不适合MVP模式,但无论如何我都看不到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM