有人可以帮我正确显示我的GUI吗?在我创建一个新实例时,GUI显示为一个小框,根本不适合内容。
我创建界面的方法是调用makeFrame方法:
private void makeFrame()
{
frame = new JFrame("game");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(menubar, BorderLayout.NORTH);
JPanel gamePanel1 = new JPanel();
buttons = new JButton[boardsize][boardsize];
mineBoard = new int[9][9];
for (int a = 0; a < boardsize; a++)
for (int b = 0; b < boardsize; b++) {
buttons[a][b] = new JButton("");
contentPane.add(buttons[a][b]);
buttons[a][b].setBounds(30+gridsize*a,30+gridsize*b,gridsize,gridsize);
buttons[a][b].addMouseListener(new MouseListener(a,b));
setx(a);
sety(b);
settried(false);
setmine(false);
}
assignmines();
updateGamesPlayed() ;
UpdateName();
contentPane.add(gamePanel1, BorderLayout.CENTER);
Box gamePanel = Box.createVerticalBox();
add(gamePanel);
gamePanel.add(nameEnter);
gamePanel.add(saveName);
gamePanel.add(namelabel);
gamePanel.add(scorelabel);
gamePanel.add(gamesPlayed);
gamePanel.add(gamesWon);
gamePanel.add(status);
gamePanel.add(noMines);
contentPane.add(gamePanel, BorderLayout.EAST);
contentPane.add(status, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
makeMenuBar();
}
如何让Jpanels适应内容?
答案 0 :(得分:0)
尝试:
frame . setSize ( height , width ) ;
或:
frame . setPreferredSize ( dimension ) ;