在行中设置JLabel并将等效的JTextField放在其旁边 [英] Setting JLabels in rows and placing the equivalent JTextField right next to it

查看:121
本文介绍了在行中设置JLabel并将等效的JTextField放在其旁边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create 4 JLabel components 4 JTextfield components for my Swing based application. I am adding these lables and text fields in one of the many panels of the application.

此面板的类如下:

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");
        }

但是,当我执行上述代码时,最终得到以下

But when I execute the above code, I end up getting the following

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

As you can clearly see from the image that the text field and labels are not positioned properly. What am I doing wrong here?

推荐答案

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

The agePanel is using a FlowLayout, but you're supplying GridBagConstraints to it when you're adding your components. You need to make up you mind over which layout manager you want to use.

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

Start by changing the layout manager for agePanel to a 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);

仔细研究布置容器内的组件并关注不同布局管理器之间的差异

Take a closer look at Laying Out Components Within a Container and focus on the differences between the different layout managers

这篇关于在行中设置JLabel并将等效的JTextField放在其旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆