GridBagLayout 未按预期设置 JPanel 位置
GridBagLayout doesn't set JPanel position as expected
所以我最初在主屏幕上使用 BorderLayout,但是一旦用户点击开始,它就会转换为 GridBagLayout,目的是将 80% 的游戏屏幕放在左侧,20% 的菜单、得分等右边。
| --- 游戏画面 --- |分数 |
但是我很难得到这个,因为我无法调整乐谱屏幕的大小,或者它根本无法正确打印出来。这是我目前使用的代码。
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方法的ACtionListenerMethod
else if (e.getSource() == startButton) {
cp.remove(panel);
panel.setSize(1200, getHeight());
state = STATE.RUNNING;
updateState();
updateGui();
createGameLoop();
}
这是我更改布局并添加 JPanel 的方法。
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);
}
它以 'house' JPanel 为 80% 结束,但 'panel' 变量仍位于前一个边框布局的底部边框上。不知道为什么。
我不完全确定这些信息是否足够,我很乐意 post 需要的任何信息。
编辑:尝试添加 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();
所以我最初在主屏幕上使用 BorderLayout,但是一旦用户点击开始,它就会转换为 GridBagLayout,目的是将 80% 的游戏屏幕放在左侧,20% 的菜单、得分等右边。
| --- 游戏画面 --- |分数 |
但是我很难得到这个,因为我无法调整乐谱屏幕的大小,或者它根本无法正确打印出来。这是我目前使用的代码。
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方法的ACtionListenerMethod
else if (e.getSource() == startButton) {
cp.remove(panel);
panel.setSize(1200, getHeight());
state = STATE.RUNNING;
updateState();
updateGui();
createGameLoop();
}
这是我更改布局并添加 JPanel 的方法。
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);
}
它以 'house' JPanel 为 80% 结束,但 'panel' 变量仍位于前一个边框布局的底部边框上。不知道为什么。
我不完全确定这些信息是否足够,我很乐意 post 需要的任何信息。
编辑:尝试添加 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();