【问题标题】:GridBagLayout doesn't set JPanel position as expectedGridBagLayout 未按预期设置 JPanel 位置
【发布时间】:2018-02-24 19:55:22
【问题描述】:

所以我最初在主屏幕上使用 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' 变量仍位于上一个边框布局的底部边框上。不知道为什么。

我不完全确定这些信息是否足够,我很乐意发布所需的任何信息。

编辑:尝试添加 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.add(panel, layoutConstraints); 创建新的 GridBagConstraints layoutConstraints而不是在那里放置相同的参考?
  • 我做到了。我即将编辑帖子以提供更多信息。
  • 好的,在这里docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 最好不要使用相同的参考。
  • but once the user hits start, it converts to a GridBagLayout - 为什么?继续使用 BorderLayout。将您的“游戏面板”添加到 CENTER,将“菜单面板”添加到 LINE_END。添加面板后,您需要重新验证()内容窗格。
  • 当你改变添加组件的顺序时,正确的仍然占80%吗?

标签: java swing layout layout-manager gridbaglayout


【解决方案1】:

如果有人好奇,我想通了。我是通过这种方式做到的...

    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();

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多