JLabel.setText работает странным образом - PullRequest
0 голосов
/ 11 июня 2018

Я пытаюсь создать копию игры Stratego, и у меня возникли проблемы с установкой текста на этикетке.Код может объяснить это лучше. Предложенный "."находится в корникатах "3C", что соответствует BoardSquares [2] [2], но если я сделаю печать BoardSquares [2] [2] .getText (), она выведет "S" в консоли, ноне в JLabel. Любая идея почему?Существует ссылка на изображение, чтобы показать расположение сетки с JLabels.https://i.stack.imgur.com/eGcDZ.png

Основной:

public static void main(String[] args) {
    int totalP = 40;
    Board cb = new Board();
    JFrame f = new JFrame("Stratego");
    Runnable r = new Runnable() {

        @Override
        public void run() {

            f.add(cb.getGui());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            // ensures the frame is the minimum size it needs to be
            // in order display the components within it
            f.pack();
            // ensures the minimum size is enforced.
            f.setMinimumSize(f.getSize());
            f.setVisible(true);

        }
    };
    SwingUtilities.invokeLater(r);
    do {
        System.out.println("peça a mover");
        System.out.println("insira a linha da peça a mover ");
        int a = reader.nextInt();
        System.out.println("insira a coluna da peça a mover");
        int b = reader.nextInt();
        System.out.println("insira a linha para onde a quer mover ");
        int c = reader.nextInt();
        System.out.println("insira a coluna para onde a quer mover ");
        int d = reader.nextInt();
        System.out.println(cb.canMoveTo(a, b, c, d));
        System.out.println(BoardSquares[b - 1][a - 1].getText());
        System.out.println(BoardSquares[d - 1][c - 1].getText());
        if (cb.canMoveTo(a, b, c, d)) {

            BoardSquares[d - 1][c - 1].setText(BoardSquares[b - 1][a - 1].getText());
            // setting the text with an "S" that was on the BoardSquare[b-1][a-1]
            // the BoardSquares[d-1][c-1].getText() will give the correct value
            // but in the gridlayout with the JLabels it will have a "." instead of and "S"
            BoardSquares[b - 1][a - 1].setText("altered");
            f.add(cb.getGui());

        }

    } while (totalP > 0);
...