2014-09-26 37 views
0

我需要保持我的按鈕不變的大小,並重新調整JFrame的大小以適應按鈕的大小,但我得到的是一個恆定的大小JFrame和我的按鈕根據按鈕的數量重新調整大小。我想我的JFrame調整其尺寸,並保持按鈕的大小不變

這是我的代碼。

下面是其中i創建我的按鈕,由尺寸選擇的按鈕的數量接收

[campoMinado.getAltura()] [campoMinado.getLargura()]

 Height    Width 

botoes [] []是我的二維按鈕陣列。

for(int y = 0; y < campoMinado.getAltura(); y++){ 
    for(int x = 0; x < campoMinado.getLargura(); x++){ 
     botoes[x][y] = new BotaoCampoMinado(x, y); 
     botoes[x][y].setPreferredSize(new Dimension(40,40)); 
     botoes[x][y].setMinimumSize(new Dimension(40,40)); 
     botoes[x][y].setMaximumSize(new Dimension(40,40)); 
     botoes[x][y].addActionListener(action); 
     botoes[x][y].addMouseListener(mouseListener); 
     getContentPane().add(botoes[x][y]); 

    } 
} 
+0

您需要使用正確的佈局管理器。閱讀教程[教程:放置容器內的組件](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – DavidPostill 2014-09-26 12:38:40

+0

我正在使用網格佈局,以便按鈕顯示爲格。 – 2014-09-26 12:40:34

回答

0

創建JFrame時使用.pack();

例:

JFrame gameField = new JFrame(); 
gameField.pack(); 
gameField.setVisible(true); 
相關問題