GridLayout在JFrame / JPanel中一起消失

问题描述 投票:0回答:1

我正在尝试创建一个TicTacToe游戏'。用户只能使用一块电路板,当用户点击电池时,电池将充满X / O,并且相同的电池(在第二块电路板的随机点上)将充满X / O.用户可以在任何一块板上获胜。当试图构建电路板并让它们在各自的JPanel中显示时会出现问题。我理想的是希望JFrame全屏,所以我设置了SetSize(2000,2000)。当我运行该程序时,JFrame会弹出,并且两个TicTacToe网格都被挤压在一起。应该是游戏状态更新的底部(X轮到,X赢了游戏等)也似乎被压扁了:

Squished grids screenshot

所以我决定尝试使用pack()而不是setSize()。好消息是游戏板显示不完整,但JFrame非常小:

Small JFrame pack screenshot(JFrame is top left corner)

理想情况下,这就是我希望JFrame看起来像:

Ideal JFrame

理想情况下,我希望JFrame是全屏的,但是当我使用pack()而不是setSize()时,我可以使用一个使JFrame更大的解决方案。

如何在全屏JFrame中取消网格或者如何使pack()更大?

我构建JFrame的方法代码:

public Attempt(){
  JFrame frame = new JFrame("Shadow Tic Tac Toe Game");
  frame.setSize(2000,2000);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();

  // Adds panel1 which will hold the first TicTacToe game.
  panel1.setLayout(new GridLayout(3,3,0,0));
  for(int d = 0; d < 3; d++){
     for(int c = 0; c < 3; c++){
        panel1.add(cell[d][c] = new Cell());
     }
  }
  // Builds Panel2 which will hold the second TicTacToe game
  panel2.setLayout(new GridLayout(3,3,0,0));
  row = rand.nextInt(3);
  column = rand.nextInt(3);
  for(int n = 0; n < 3; n++){
     panel2.add(cell[row][column] = new Cell());
  }
  // Adds Panel1 to the JFrame
  frame.add(panel1, BorderLayout.EAST);
  // Adds Panel2 to the JFrame
  frame.add(panel2, BorderLayout.WEST);
  // Updates the status of the game here (win/lose/draw/whose turn it is)
  frame.add(jlblStatus, BorderLayout.SOUTH);
  // Calls method Chose() which allows the player to chose which token they will play as
  Chose();
  frame.setVisible(true);

}

java swing jframe jpanel grid-layout
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.