GridBagLayout - 垂直定位组件

时间:2018-03-16 23:13:17

标签: java swing layout-manager gridbaglayout

尝试将JTable旁边的表单与GridBagLayout对齐,我正在努力将我的组件放到面板的顶部。我需要物品标签和字段下面的价格标签和字段,但我不能让它从面板底部移动。

如果我使用anchor = GridConstraints.NORTHWEST,则会将商品标签和字段移至顶部,但之后我无法使用LINE_END锚定它。除非有办法做到这两点?请参阅我的图片,我试图展示我要放置表单的区域。感谢任何帮助。

enter image description here

GridBagConstraints gbc = new GridBagConstraints();

// TEST COMPONENTS
JLabel lblItem = new JLabel("Item: ");
JLabel lblPrice = new JLabel("Price: ");
JLabel lblQuantity = new JLabel ("Quantity: ");

JTextField itemField = new JTextField(15);
JTextField pricePoundsField = new JTextField(3);
JTextField pricePenceField = new JTextField(2);
JTextField quantityField = new JTextField(3);

gbc.gridx = 0;
gbc.gridy = 0;
//gbc.weightx = 1.0;
//gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.LINE_START;
panelStockTable.add(jsp, gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_END;
panelStockTable.add(lblItem, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
panelStockTable.add(lblPrice, gbc);

gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 2;
gbc.gridy = 0;
panelStockTable.add(itemField, gbc);

gbc.gridx = 2;
gbc.gridy = 1;
panelStockTable.add(pricePoundsField, gbc);

1 个答案:

答案 0 :(得分:1)

UI越复杂,您就越需要专注于将功能和布局隔离到自己的容器/类。

这就是复合布局的概念变得非常强大的地方。而不是将字段直接放在与表相同的容器上,为它们使用单​​独的容器

Compound layout

这里,右侧面板正站在桌子上,蓝色面板正在展示复合容器......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            JPanel stockTableProxy = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(200, 150);
                }
            };
            stockTableProxy.setBackground(Color.RED);

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.LINE_START;
            add(stockTableProxy, gbc);


            JPanel fieldsPanel = new JPanel(new GridBagLayout());
            fieldsPanel.setBackground(Color.BLUE);

            // TEST COMPONENTS
            JLabel lblItem = new JLabel("Item: ");
            JLabel lblPrice = new JLabel("Price: ");
            JLabel lblQuantity = new JLabel("Quantity: ");

            JTextField itemField = new JTextField(15);
            JTextField pricePoundsField = new JTextField(3);
            JTextField pricePenceField = new JTextField(2);
            JTextField quantityField = new JTextField(3);

            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_END;
            fieldsPanel.add(lblItem, gbc);

            gbc.gridx = 1;
            gbc.gridy = 1;
            fieldsPanel.add(lblPrice, gbc);

            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.gridx = 2;
            gbc.gridy = 0;
            fieldsPanel.add(itemField, gbc);

            gbc.gridx = 2;
            gbc.gridy = 1;
            fieldsPanel.add(pricePoundsField, gbc);

            gbc.gridx = 0;
            gbc.gridy = 20;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weighty = 1;
            fieldsPanel.add(new JLabel(), gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_END;
            gbc.fill = GridBagConstraints.VERTICAL;
            add(fieldsPanel, gbc);
        }

    }

}

但等等,还有更多...

gbc.gridx = 0;
gbc.gridy = 20;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
fieldsPanel.add(new JLabel(), gbc);

这是一个小技巧,可以用来强制组件移动到容器的不同边缘,在这里,我用它将所有字段推到容器的顶部。

所有这一切都是添加一个透明的组件(在这个JLabel中)并为其提供所有剩余的空间,整洁