因此我最初使用BorderLayout作为主屏幕,但是一旦用户点击开始,它将转换为GridBagLayout,目的是在左侧为80%的游戏画面,在右侧为20%的菜单,得分等。
| --- GameScreen --- |得分|
但是我很难得到这个,因为我无法调整分数屏幕的大小,或者它根本无法打印出来。这是我目前正在使用的代码。
public Launcher(String title) {
super(title);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
cp = getContentPane();
// cp.setSize((int)(1920*0.5), (int)(1080*0.5));
cp.setLayout(new BorderLayout());
panel = new JPanel();
panel.setBackground(Color.YELLOW);
// panel.setSize(this.getWidth()/4, this.getHeight());
panel.add(startButton);
panel.add(pauseButton);
panel.add(quitButton);
setSize(1920, 1100);
cp.add(panel, BorderLayout.SOUTH);
this.getContentPane().setBackground(Color.GREEN);
//ActionListenerStuff for buttons
.
.
.
这是调用updateGui方法的ACtionListener方法。
else if (e.getSource() == startButton) {
cp.remove(panel);
panel.setSize(1200, getHeight());
state = STATE.RUNNING;
updateState();
updateGui();
createGameLoop();
}
这是我更改布局和添加JPanels的方法。
private void updateGui() {
cp.setLayout(new GridBagLayout());
GridBagConstraints layoutConstraints = new GridBagConstraints();
layoutConstraints.fill = GridBagConstraints.VERTICAL;
layoutConstraints.weighty = 1;
layoutConstraints.weightx = 0.8;
cp.add(house, layoutConstraints);
layoutConstraints.weighty = 1;
layoutConstraints.weightx = 0.2;
cp.add(panel, layoutConstraints);
}
结果是“房子”JPanel为80%,但“面板”变量仍然位于前一个边界布局的底部边界。不知道为什么。
我不完全确定这些信息是否足够,并且很乐意发布所需的任何信息。
编辑:尝试添加layoutConstraints.gridy = 1;但那也不起作用。
private void updateGui() {
cp.setLayout(new GridBagLayout());
GridBagConstraints layoutConstraints = new GridBagConstraints();
layoutConstraints.fill = GridBagConstraints.VERTICAL;
layoutConstraints.weighty = 1;
layoutConstraints.weightx = 0.8;
house.setMinimumSize(new Dimension(1600, 1080));
cp.add(house, layoutConstraints);
GridBagConstraints layoutConstraints2 = new GridBagConstraints();
layoutConstraints2.fill = GridBagConstraints.VERTICAL;
layoutConstraints2.weighty = 1;
layoutConstraints2.weightx = 0.2;
layoutConstraints.gridy = 1;
cp.add(panel, layoutConstraints2);
}
private void updateState() {
if (roomGenerator == null) {
roomGenerator = new RoomGenerator();
}
roomGenerator.updateRoom();
Room room = roomGenerator.getRoom();
house = (House) roomGenerator.getRoom();
house.setSize(300, getHeight());
this.getContentPane().add(house, BorderLayout.CENTER);
startButton.setVisible(false);
// this plugin puts the buttons in the House JPanel
// house.add(pauseButton);
// house.add(quitButton);
}
这就是我现在拥有的。这就是显示的内容。 The drawing should be the "80%" portion and the yellow should be the 20%.
编辑2:使用Camickr的更改我最终使用this。
这是朝着正确方向迈出的一步,但据我所知,它没有正确设置房屋JPanel的大小。
private void updateGui() {
cp.add(panel, BorderLayout.LINE_END);
panel.setVisible(true);
cp.revalidate();
}
private void updateState() {
if (roomGenerator == null) {
roomGenerator = new RoomGenerator();
}
roomGenerator.updateRoom();
Room room = roomGenerator.getRoom();
house = (House) roomGenerator.getRoom();
house.setSize(1500, getHeight());
this.getContentPane().add(house, BorderLayout.CENTER);
startButton.setVisible(false);
}
如果有人好奇我就明白了。我是通过这种方式做到的......
cp.setLayout(new GridBagLayout());
GridBagConstraints layoutConstraints = new GridBagConstraints();
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.gridx = 0;
layoutConstraints.gridy = 0;
layoutConstraints.weighty = 1;
layoutConstraints.weightx = 0.8;
house.setMinimumSize(new Dimension(1500, 1080));
cp.add(house, layoutConstraints);
GridBagConstraints layoutConstraints2 = new GridBagConstraints();
layoutConstraints2.fill = GridBagConstraints.BOTH;
layoutConstraints2.weighty = 1;
layoutConstraints2.weightx = 0.2;
layoutConstraints2.gridx = 1;
layoutConstraints2.gridy = 0;
cp.add(dataPanel, layoutConstraints2);
dataPanel.setVisible(true);
cp.revalidate();