我一直在尝试将 jpanel 的高度设置为窗口高度的 80%。我所做的每一种方式都会创建一个小矩形,或者填充整个窗口。目前,这是我的代码;
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
window.setSize(xSize,ySize);
JPanel p = new JPanel();
p.setBackground(Color.PINK);
p.setLayout(new BorderLayout());
int gameHeight = (int)(Math.round(ySize * 100.0/window.getHeight()));
int gameWidth = (int)(Math.round(xSize * 100.0/window.getWidth()));
p.setPreferredSize(new Dimension(gameHeight, gameWidth));
p.add(new JLabel(" "));
window.add(p, BorderLayout.SOUTH);