使用 GridBagLayout

Using the GridBagLayout

我以前从未使用过布局,据我所知,GridBagLayout 可能不是最容易上手的布局,但无论如何我都在尝试使用它。我正在尝试将 GridBagLayout 用于我的 JPanel,这样当我重新调整它的大小时,组件就会增长。我只需要它来保持组件的自然高度,但我正在尝试使用 GridBagConstraints.HORIZONTAL 来自动调整组件的宽度。我有代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.io.*;

public class NumberConverterDesignTest
{
    JFrame numberConversionWindow = new JFrame("Number Conversion");

    JPanel  numberPanel = new JPanel();

    ////////COMPONENTS////////
    JLabel lblTitle;
    JLabel lblBase2;
    JLabel lblBase8;
    JLabel lblBase10;
    JLabel lblBase16;
    JTextField txtBase2;
    JTextField txtBase8;
    JTextField txtBase10;
    JTextField txtBase16;

    ///////////Font///////////
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String []fontFamilies = ge.getAvailableFontFamilyNames();
    Font FontT5 = new Font("SansSerif", Font.BOLD, 45);

    public void numberConvertGUI()
    {
        numberConversionWindow.setBounds(10, 10, 420, 300);

        numberConversionWindow.setMinimumSize(new Dimension(420, 300));

        numberConversionWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        numberConversionWindow.setLayout(new GridLayout(1,1));

        createNumberConversionPanel();

        numberConversionWindow.getContentPane().add(numberPanel);

        numberConversionWindow.setVisible(true);
    }

    public void createNumberConversionPanel()
    {
        numberPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        lblTitle = new JLabel();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        lblTitle.setText("Number Converter");
        lblTitle.setHorizontalAlignment(JLabel.CENTER);
        lblTitle.setFont(FontT5);
        numberPanel.add(lblTitle, c);

        lblBase2 = new JLabel();
        c.weightx = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        lblBase2.setText("Binary: ");
        lblBase2.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase2);

        lblBase8 = new JLabel();
        //c.weightx = 0.5;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        lblBase8.setText("Octal: ");
        lblBase8.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase8);

        lblBase10 = new JLabel();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        lblBase10.setText("Decimal: ");
        lblBase10.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase10);

        lblBase16 = new JLabel();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        lblBase16.setText("Hexadecimal: ");
        lblBase16.setHorizontalAlignment(JLabel.RIGHT);
        numberPanel.add(lblBase16);

        txtBase2 = new JTextField();
        c.weightx = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        txtBase2.setText("");
        numberPanel.add(txtBase2);

        txtBase8 = new JTextField();
        //c.weightx = 0.5;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        txtBase8.setText("");
        numberPanel.add(txtBase8);

        txtBase10 = new JTextField();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        txtBase10.setText("");
        numberPanel.add(txtBase10);

        txtBase16 = new JTextField();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 4;
        txtBase16.setText("");
        numberPanel.add(txtBase16);
    }

    public static void main(String[] args)
    {
        NumberConverterDesignTest nC = new NumberConverterDesignTest();
        nC.numberConvertGUI();
    }
}

编译后我得到

我的目标是争取

我认为问题出在我已将 weightx 设置为什么,但是我不确定将其设置为什么才能获得我想要的结果。任何帮助将不胜感激

每次调用numberPanel.add方法时都需要传递更新后的约束。