0
我在製作掃雷程序,我需要實現一個功能,用戶可以選擇調整地雷的大小。我已經完成了這個,但是當我製作一個更大的電路板時,JFrame沒有變大。JFrame沒有調整JPanel的大小
這是當用戶想要調整板
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();
}
這是任何幫助,將理解
,其將所述面板到一個JFramepublic 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);
}
}
類發生這種情況的方法
當框架在一個單獨的類中時,我該怎麼做? – gvanderclay
你的'frame'是'static',這對我來說不是一個好主意,或者你可以使用['SwingUtilities.getWindowAncestor'](http://docs.oracle.com/javase/7/docs/api/ javax/swing/SwingUtilities.html#getWindowAncestor(java.awt.Component)) – MadProgrammer
哦,那是我的不好,我正在嘗試其他一些沒有用的東西,忘記改變它。這有什麼區別嗎?感謝你的幫助 – gvanderclay