从另一个单元格GridBagLayout控制单元格间距

时间:2013-11-01 05:53:01

标签: java swing gridbaglayout

我想要的是让第一个textField更接近第一个jlabel,以及第一个textfield和第二个jlabel之间的一些空间,如下所示:

JLABEL:JTEXTFIELD ----(spaceeee)---------- JLABEL:JTEXTFIELD

但我改变的一切都没有达到我想要的水平,看:

enter image description here

我添加TFields和Jlabels的代码:

    JLabel lblProtocolo = new JLabel("Protocolo:");
    JLabel lblDataEntrada = new JLabel("Data Entrada:");
    JTextField tfProtocolo = new JTextField(10);
    JTextField tfDataEntrada = new JTextField(10);
    JTextField soteste = new JTextField(10);
    JLabel soocupa = new JLabel();
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(10, 10, 10, 10);
    gbc.anchor = GridBagConstraints.NORTHWEST;
    panel2DistribuidorDistribuir.add(lblProtocolo,gbc);
gbc.gridx = 1;
    panel2DistribuidorDistribuir.add(tfProtocolo,gbc);

gbc.gridx = 2;
    panel2DistribuidorDistribuir.add(lblDataEntrada,gbc);

gbc.gridx = 3;
    panel2DistribuidorDistribuir.add(tfDataEntrada,gbc);

我知道如果在gridx = 4中添加一些不可见的jtextfield;然后将这个新jtextfield的权重设置为1,其他为0,它将“工作”但是gridx = 0,1,2,3中的组件将不再自动调整大小=(,任何线索的人)

3 个答案:

答案 0 :(得分:3)

gbc.weightx = 1;基本上是说该组件想要占用100%的可用空间。

我要做的就是先删除它......

您还可以使用gbc.insets来影响各个组件之间的距离......

请查看How to Use GridBagLayout了解详情

使用示例更新

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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 GridBagLayoutTest100 {

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            JLabel lblProtocolo = new JLabel("Protocolo:");
            JLabel lblDataEntrada = new JLabel("Data Entrada:");
            JTextField tfProtocolo = new JTextField(10);
            JTextField tfDataEntrada = new JTextField(10);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.WEST;

            add(lblProtocolo, gbc);
            gbc.gridx++;
            gbc.weightx = 1;
            add(tfProtocolo, gbc);

            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridx++;
            add(lblDataEntrada, gbc);
            gbc.gridx++;
            gbc.weightx = 0;
            add(tfDataEntrada, gbc);            
        }
    }
}

答案 1 :(得分:1)

将组件添加到GridBagLayout并设置了GridBagConstraints。您可以在定义GridBagConstraints时定义insets。示例代码如下所示

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0, 155, 0, 0};
        gridBagLayout.rowHeights = new int[]{94, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
        setLayout(gridBagLayout);



        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        GridBagConstraints gbc_textArea = new GridBagConstraints();
        gbc_textArea.insets = new Insets(0, 0, 5, 15);
        gbc_textArea.fill = GridBagConstraints.BOTH;
        gbc_textArea.gridx = 2;
        gbc_textArea.gridy = 0;
        add(textArea, gbc_textArea);

答案 2 :(得分:0)

现在已经很晚了但我发布了这个技巧,它的工作方式与你想要的一样:

public static void main(String[] args) {
    JLabel lblProtocolo = new JLabel("Protocolo:");
    JLabel lblDataEntrada = new JLabel("Data Entrada:");
    JTextField tfProtocolo = new JTextField(10);
    JTextField tfDataEntrada = new JTextField(10);
    JTextField soteste = new JTextField(10);

    JFrame f = new JFrame();

    f.getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 5, 5);
    c.anchor = GridBagConstraints.WEST;
    f.getContentPane().add(lblProtocolo,c);

    c.gridx = 1;
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    f.getContentPane().add(tfProtocolo,c);

    c.anchor = GridBagConstraints.EAST;
    c.gridx = 4;
    f.getContentPane().add(tfDataEntrada,c);

    c.gridx = 2;
    c.gridwidth = 2;
    c.weightx = 1;
    lblDataEntrada.setHorizontalAlignment(JLabel.RIGHT);
    f.getContentPane().add(lblDataEntrada,c);


    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

实施例: enter image description here