简体   繁体   中英

GridLayout Squished Together in JFrame/JPanel

I am trying to create a TicTacToe Game'. Only one board is playable by the user and when the user clicks on a cell the cell will be filled with an X/O and the same cell(on a randomized spot of the second board) will be filled with an X/O. The user can win on either board. The problem arises when trying to build the boards and getting them to display in their respective JPanels. I ideally want the JFrame to be full screen so I setSize(2000,2000). When I run the program though the JFrame pops up and both TicTacToe grids are squished together. The bottom part where there are supposed to be status updates of the game (X's turn, X won the game, etc) also appears to be squished:

压缩的网格截图

So I decided to try pack() instead of setSize(). Good news is that the game boards showed up unsquished but the JFrame was insanely small:

JFrame小包截图(JFrame在左上角)

Ideally this is what I want the JFrame to look like:

理想的JFrame

Ideally I want the JFrame to be full screen but I would be fine with a solution that makes the JFrame bigger when I use pack() instead of setSize().

How do I either unsquish the grids in full screen JFrame or how do I make pack() bigger?

Code of method in which I build the 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);

}

How do I either unsquish the grids in full screen JFrame or how do I make pack() bigger?

You need to implement the getPreferredSize() method of your Cell component to return the size of the cell. Then when you pack the frame the Cells will be displayed at their preferred size.

ideally want the JFrame to be full screen so I setSize(2000,2000).

A size of of (2000, 2000) makes no sense. I don't know of any square screens. In any case, don't use setSize() for this. Instead when you want a full screen frame you use:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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