我试图创建游戏Stratego的副本,并且我在标签中设置文本时遇到了一些麻烦。代码可能会更好地解释它。被称为“。”在坐标“3C”中,它对应于BoardSquares [2] [2],但如果我打印BoardSquares [2] [2] .getText(),它将在控制台中打印出“S”,但是不是在JLabel.Any ideia为什么? 有一个na imagem的链接,用于显示带有JLabel的gridlayout。 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);