如何用GridBagLayout填充BOTH和CENTER面板?

时间:2016-06-13 09:13:20

标签: java gridbaglayout

家伙!如何正确放置椅子?在下面图片的第二帧中,我发布了我已经绘制了我要放置这些对象的圆圈。 enter image description here

我使用了4个BorderLayouts来放置标签和图像,以免使gridbaglayout太复杂,然后我将它们添加到gridbaglayout中,如下所示:

    c.fill = GridBagConstraints.BOTH/*PAGE_START*/;
    c.weightx = 10; 
    c.weighty = 10; 
    c.gridx = 1;
    c.gridy = 0;
    panel.add(topPlayerPanel, c);

    c.gridx = 0;
    c.gridy = 1;
    panel.add(leftPlayerPanel, c);

    c.gridx = 1;
    c.gridy = 1;
    panel.add(t, c);

    c.gridx = 2;
    c.gridy = 1;
    panel.add(rightPlayerPanel, c);

    c.gridx = 1;
    c.gridy = 2;
    panel.add(bottomPlayerPanel, c); 

如果我使用PAGE_START,LINE_START,LINE_END,PAGE_END放置它们,则展示位置正确无误。但它并没有向我展示整个图像:(

2 个答案:

答案 0 :(得分:0)

您可以使用GridBagLayout和"推送"占用多余空间的隐形组件周围的其他组件。我使用Box.Filler将这些计算机图标推送到表格面板。

table with chairs

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class GBLTableWithChairs extends JFrame {

    public GBLTableWithChairs() {
        setTitle("Table with chairs");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc;
        Box.Filler filler;
        JLabel label;

        JPanel table = new JPanel(new BorderLayout());
        table.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY));
        table.add(new JLabel("<html>TABLE&nbsp;TABLE<br/>TABLE&nbsp;TABLE<br/>TABLE&nbsp;TABLE"));
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        add(table, gbc); // to frame

        JPanel leftChair = new JPanel(new GridBagLayout());
        filler = createHorizontalFiller();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0d;
        leftChair.add(filler, gbc); // to leftChair
        label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        leftChair.add(label, gbc); // to leftChair
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(leftChair, gbc); // to frame

        JPanel topChair = new JPanel(new GridBagLayout());
        filler = createVerticalFiller();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.VERTICAL;
        gbc.weighty = 1.0d;
        topChair.add(filler, gbc); // to topChair
        label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        topChair.add(label, gbc); // to topChair
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(topChair, gbc); // to frame

        JPanel rightChair = new JPanel(new GridBagLayout());
        filler = createHorizontalFiller();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0d;
        rightChair.add(filler, gbc); // to rightChair
        label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        rightChair.add(label, gbc); // to rightChair
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 1;
        add(rightChair, gbc); // to frame

        JPanel bottomChair = new JPanel(new GridBagLayout());
        filler = createVerticalFiller();
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.VERTICAL;
        gbc.weighty = 1.0d;
        bottomChair.add(filler, gbc); // to bottomChair
        label = new JLabel(UIManager.getIcon("FileView.computerIcon"));
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        bottomChair.add(label, gbc); // to bottomChair
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        add(bottomChair, gbc); // to frame

        setSize(200, 200);
        setLocationRelativeTo(null);
    }

    private Box.Filler createHorizontalFiller() {
        return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(32767, 0));
    }

    private Box.Filler createVerticalFiller() {
        return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, 32767));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GBLTableWithChairs().setVisible(true);
            }
        });
    }

}

这应该让你开始。

答案 1 :(得分:0)

非常感谢!我使用填充程序重写了我的代码,但我遇到了这个问题:

例如对于左椅子,我放置填充物然后放置leftChairPanel(它在BorderLayout.NORTH中带有JLabel的BorderLayout和BorderLayout.CENTER中的图像),但我得到了这个......也许我确实使用了错误的GridBagConstraints填充?

JPanel table = new JPanel(new BorderLayout());
    table.add(t);
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;
    panel.add(table, gbc);

    JPanel leftChair = new JPanel(new GridBagLayout());
    filler = createHorizontalFiller();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.weightx = 1.0d;
    leftChair.add(filler, gbc); //alla sedia di sinistra
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    leftChair.add(leftPlayerPanel, gbc);
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 1;
    panel.add(leftChair, gbc); //al mainPanel

    JPanel topChair = new JPanel(new GridBagLayout());
    filler = createVerticalFiller();
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.weighty = 1.0d;
    topChair.add(filler, gbc); //alla sedia in alto
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 1;
    topChair.add(topPlayerPanel, gbc);
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    panel.add(topChair, gbc); //al panel

    JPanel rightChair = new JPanel(new GridBagLayout());
    filler = createHorizontalFiller();
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1.0d;
    rightChair.add(filler, gbc); //alla sedia di destra
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    rightChair.add(rightPlayerPanel, gbc);
    gbc = new GridBagConstraints();
    gbc.gridx = 2;
    gbc.gridy = 1;
    panel.add(rightChair, gbc); //al panel

    JPanel bottomChair = new JPanel(new GridBagLayout());
    filler = createVerticalFiller();
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.weighty = 1.0d;
    bottomChair.add(filler, gbc); //alla sedia in basso
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    bottomChair.add(bottomPlayerPanel, gbc);
    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 2;
    panel.add(bottomChair, gbc); //al panel