簡體   English   中英

JFrame沒有使用JPanel調整大小

[英]JFrame not resizing with JPanel

我正在制作一個掃雷程序,我需要實現一個功能,用戶可以選擇調整地雷的網格大小。 我已經完成了這個,但是當我制作一個更大的電路板時,JFrame不會變得更大。

這是當用戶想要調整板的大小時發生的方法

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();
    }

這是將面板添加到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);
    }

}

任何幫助,將不勝感激

重新計算面板的首選大小后,再次在框架上調用pack

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM