[英]JFrame not resizing with JPanel
I am making a minesweeper program, and I need to implement a feature where the user can choose to resize the grid that the mines are on. 我正在制作一个扫雷程序,我需要实现一个功能,用户可以选择调整地雷的网格大小。 I've finished this, but when I make a larger board the JFrame doesn't get any larger, with it.
我已经完成了这个,但是当我制作一个更大的电路板时,JFrame不会变得更大。
This is the method that happens when the user wants to resize the board 这是当用户想要调整板的大小时发生的方法
public void mouseClicked(MouseEvent e) {
if(e.getSource() == quitButton && e.getButton() == 1){
System.exit(0);
}
// if I hit the reset size button this happens
if(e.getSource() == setDimension){
for (int r = 0; r < size; r++)
for (int c = 0; c < size; c++){
gameBoard.remove(board[r][c]); // removes the buttons from the board
}
inputSizeAndMines(); // lets the user choose the size and amount of mines
game = new MineSweeperGame(size, mines); // remakes the game
setBoard(); // adds new buttons based on what the user entered
displayBoard(); // makes visuals for the board
revalidate();
repaint();
}
This is the class that adds the panel to a JFrame 这是将面板添加到JFrame的类
public class MineSweeper {
/**********************************
* Main class that does everything
**********************************/
public static void main(String[] args) {
JFrame frame = new JFrame("MineSweeper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MineSweeperPanel panel = new MineSweeperPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
Any help would be appreciated 任何帮助,将不胜感激
重新计算面板的首选大小后,再次在框架上调用pack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.