之前你跟GridBagLayout的建议,我已经试过了,但我无法得到它的工作。 我想用大小为800 x 800,并用600×600的一个中心面板的框架。 现在,当我运行它的中心面板为600x578。 谁能告诉我在哪里出了问题? 这只是22个像素。
public void createPlayground()
{
JFrame frame = new JFrame("ForFun Maze");
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3,1));
buttonPanel.setPreferredSize(new Dimension(100,600));
buttonPanel.setMaximumSize(new Dimension(100,600));
buttonPanel.setBackground(SIDEBAR);
JButton reset = new JButton();
reset.setText("Reset");
reset.addActionListener(new RestartListener());
reset.setSize(100,180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100,180);
pause.addActionListener(new PauseListener());
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100,180);
quit.addActionListener(new QuitListener());
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
if(level == 1)
{
array = glevel1;
}
CenterPanel centerPanel = new CenterPanel(array);
centerPanel.setPreferredSize(new Dimension(600,600));
centerPanel.setMinimumSize(new Dimension(600,600));
centerPanel.setBackground(BACKGROUND);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(SIDEBAR);
leftPanel.setPreferredSize(new Dimension(100,600));
JPanel northPanel = new JPanel();
northPanel.setBackground(SIDEBAR);
northPanel.setPreferredSize(new Dimension(800,100));
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(SIDEBAR);
bottomPanel.setPreferredSize(new Dimension(800,100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Size of centerpane"+centerPanel.getWidth()+"x"+centerPanel.getHeight());
}