GridBagLayout没有按预期设置JPanel位置

时间:2017-09-15 15:09:50

标签: java swing layout layout-manager gridbaglayout

所以我最初使用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方法的ACtionListenerMethod。

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

它最终会成为' house' JPanel是80%但是'小组'变量仍然位于前一个borderlayout底部的边框上。不知道为什么会这样。

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

编辑:尝试添加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);

}

1 个答案:

答案 0 :(得分:0)

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

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