在行中设置 JLabels 并在其旁边放置等效的 JTextField

Setting JLabels in rows and placing the equivalent JTextField right next to it

我正在尝试为基于 Swing 的应用程序创建 4 JLabel 个组件 4 JTextfield 个组件。我在应用程序的众多面板之一中添加这些标签和文本字段。

本面板的class如下:

public class CustomerInfoPanel extends JPanel implements ActionListener {

        private Car[] carList;
        private CarSalesSystem carSystem;
        private int currentIndex = 0;
        private JLabel headingLabel = new JLabel("Enter your details and let us find a car for you");
        private JLabel ageLabel = new JLabel("Age");
        private JLabel genderLabel = new JLabel("Gender");
        private JLabel incomeLabel = new JLabel("Income");
        private JLabel interestLabel = new JLabel("Age");

        private JButton searchButton = new JButton("Search");
        private JButton resetButton = new JButton("Reset");
        private JButton previousButton = new JButton("Previous");
        private JButton nextButton = new JButton("Next");

        //text fields
        private JTextField ageTextField = new JTextField();
        private JTextField genderTextField = new JTextField();
        private JTextField incomeTextField = new JTextField();
        private JTextField interestTextField = new JTextField();

        private JPanel topPanel = new JPanel();
        private JPanel agePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        private JPanel searchButtonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        private JPanel navigateButtonsPanel = new JPanel();
        private CarDetailsComponents carComponents = new CarDetailsComponents();

        /**
         * @param carSys links to a CarSalesSystem object
         * @param dest where the panel will be displayed on the main frame
         */
        public CustomerInfoPanel(CarSalesSystem carSys)
        {
            carSystem = carSys;
            Insets currentInsets;
            GridBagConstraints gridBagConstraints;
            setLayout(new BorderLayout(0, 20));
            topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));

            previousButton.addActionListener(this);
            nextButton.addActionListener(this);
            resetButton.addActionListener(this);
            searchButton.addActionListener(this);

            String currentFont = ageLabel.getFont().getName();
            currentInsets =  new Insets(0, 10, 0, 30);

            ageLabel.setFont(new Font(currentFont, Font.BOLD, 12));
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.insets = currentInsets;
            agePanel.add(ageLabel, gridBagConstraints);

            genderLabel.setFont(new Font(currentFont, Font.BOLD, 12));
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.insets = currentInsets;
            agePanel.add(genderLabel, gridBagConstraints);

            incomeLabel.setFont(new Font(currentFont, Font.BOLD, 12));
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.insets = currentInsets;
            agePanel.add(incomeLabel, gridBagConstraints);

            interestLabel.setFont(new Font(currentFont, Font.BOLD, 12));
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.insets = currentInsets;
            agePanel.add(interestLabel, gridBagConstraints);

            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.weightx = 1.0;
            agePanel.add(ageTextField, gridBagConstraints);

            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.weightx = 1.0;
            agePanel.add(genderTextField, gridBagConstraints);

            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.weightx = 1.0;
            agePanel.add(incomeTextField, gridBagConstraints);

            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
            gridBagConstraints.anchor = GridBagConstraints.WEST;
            gridBagConstraints.weightx = 1.0;
            agePanel.add(interestTextField, gridBagConstraints);


            searchButtonsPanel.add(searchButton);
            searchButtonsPanel.add(resetButton);
            navigateButtonsPanel.add(previousButton);
            navigateButtonsPanel.add(nextButton);
            agePanel.setBorder(new javax.swing.border.EmptyBorder(new Insets(0, 5, 0, 0)));
            searchButtonsPanel.setBorder(new javax.swing.border.EmptyBorder(new Insets(0, 5, 0, 0)));

            headingLabel.setAlignmentX(0.5f);

            topPanel.add(Box.createVerticalStrut(10));
            topPanel.add(headingLabel);
            topPanel.add(Box.createVerticalStrut(10));
            topPanel.add(agePanel);
            topPanel.add(searchButtonsPanel);
            topPanel.add(Box.createVerticalStrut(15));
            carComponents.add(navigateButtonsPanel, "Center");
            carComponents.setVisible(false);

            add(topPanel, "North");
            add(carComponents, "Center");
        }

但是当我执行上面的代码时,我最终得到以下结果

从图像中可以清楚地看到,文本字段和标签的位置不正确。我在这里做错了什么?

agePanel 使用的是 FlowLayout,但您在添加组件时向其提供了 GridBagConstraints。您需要决定要使用哪个布局管理器。

首先将 agePanel 的布局管理器更改为 GridBagLayout

private JPanel agePanel = new JPanel(new GridBagLayout());

然后提供一个列提示 JTextField

private JTextField ageTextField = new JTextField(10);
private JTextField genderTextField = new JTextField(10);
private JTextField incomeTextField = new JTextField(10);
private JTextField interestTextField = new JTextField(10);

仔细看看Laying Out Components Within a Container并关注不同布局管理器之间的差异

如果你想开发自定义的用户界面,你必须使用下面的方法并在任何你想要的地方自由绑定控件 setLayout(null) 并使用 setBounds( x, y, width, height) 方法绑定控件而不使用任何布局管理器。