简体   繁体   中英

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.

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

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

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