Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout-垂直定位组件_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

Java GridBagLayout-垂直定位组件

Java GridBagLayout-垂直定位组件,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,试图将我的JTable旁边的表单与GridBagLayout对齐,我正在努力将组件放到面板顶部。我需要的价格标签和项目标签和字段正下方的字段,但我不能让它从面板底部移动 如果我使用anchor=GridConstraints.NORTHWEST,这会将项目标签和字段移到顶部,但随后我将无法使用LINE\u END对其进行锚定。除非有办法两者兼得?请查看我试图展示我想要放置表格的区域的图片。谢谢你的帮助 UI越复杂,您就越希望将功能和布局隔离到它们自己的容器/类中 这就是复合布局的概念变得非常强

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

如果我使用
anchor=GridConstraints.NORTHWEST
,这会将项目标签和字段移到顶部,但随后我将无法使用
LINE\u END
对其进行锚定。除非有办法两者兼得?请查看我试图展示我想要放置表格的区域的图片。谢谢你的帮助


UI越复杂,您就越希望将功能和布局隔离到它们自己的容器/类中

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

在这里,右边的面板代表桌子,蓝色的面板展示混合容器

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);
这是一个小技巧,您可以使用它强制组件移动到容器的不同边缘,在这里,我使用它将所有字段推到容器的顶部


它所做的只是添加一个透明组件(在本例中为a
JLabel
)并为其提供所有剩余空间,而neat

似乎没有使用
gbc.gridWidth
gbc.gridHeight
指定每个组件应使用多少网格单元。如果您的表具有
gridy=0
,则将价格字段设置为
gridy=1
将其放置在表下方的区域中。相反,让表使用多个行和列,以允许在表的右侧空间中使用更多的网格单元。我不是GridBagLayout的高手,有一段时间没有完全使用Swing,但我相信这是问题的一部分。
gbc.gridx = 0;
gbc.gridy = 20;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
fieldsPanel.add(new JLabel(), gbc);