0

我想要的是使第一个文本字段更靠近第一个 jlabel,以及第一个文本字段和第二个 jlabel 之间的一些空间,如下所示:

JLABEL:JTEXTFIELD ----(空格)---------JLABEL:JTEXTFIELD

但我改变的一切都没有接近我想要的,看:

在此处输入图像描述

我添加 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中的组件将不再自动调整大小=(,任何线索家伙

4

3 回答 3

3

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

我要做的就是从删除它开始......

你也可以玩弄gbc.insets影响各个组件之间的距离......

查看如何使用 GridBagLayout了解更多详情

更新示例

在此处输入图像描述

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);            
        }
    }
}
于 2013-11-01T06:09:17.100 回答
1

将您的组件添加到设置了 GridBagConstraints 的 GridBagLayout。您可以在定义 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);
于 2013-11-01T05:58:25.607 回答
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);
}

例子: 在此处输入图像描述

于 2013-11-01T06:41:39.877 回答