插入GridBagLayout后,JComboBox会更改JTextField宽度

时间:2016-09-20 13:56:13

标签: java swing gridbaglayout

我正在尝试为一个人的出生日期添加3个组合框:一个月,一天,一年。当我添加所有3个组合框时,它会更改JTextFields的宽度。任何有关如何解决此问题的帮助将不胜感激。

还有更好的方法来添加JComboBox的月份,而不是逐个插入吗?

以下是代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Home extends JFrame {

    private JButton btnClear, btnNext;
    private JTextField txtName, txtAddress, txtCity, txtState,
            txtZipCode, txtPhoneNumber, txtEmail;
    private JComboBox txtDate, txtYear, txtMonth;
    private JLabel labelName, labelAddress, labelCity, labelState,
            labelZipCode, labelPhoneNumber, labelEmail, labelDOB;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Home().setVisible(true);
            }
        });
    }

    public Home() {
        createView();

        setTitle("Job Hiring Systems");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 500);
        setMinimumSize(new Dimension(600, 500));
        //pack();
        setLocationRelativeTo(null);
        setResizable(false);
    }

    private void createView() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        //panel.setBorder(new EmptyBorder(25,10,25,10));
        getContentPane().add(panel);

        /*
                 * North Panel
         */
        JPanel northPanel = new JPanel();
        northPanel.setBackground(Color.GRAY);
        panel.add(northPanel, BorderLayout.NORTH);

        JLabel labelWelcomeMessage = new JLabel("Welcome to the Job Hiring Systems");
        labelWelcomeMessage.setFont(new Font("Serif", Font.BOLD, 25));
        northPanel.add(labelWelcomeMessage, BorderLayout.CENTER);
        /*
                 * End of North Panel
         */

 /*
                 * Center Panel
         */
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.yellow);
        centerPanel.setSize(300, 300);
        GridBagLayout layout = new GridBagLayout();

        centerPanel.setLayout(layout);
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        labelName = new JLabel("Enter your name: ");
        labelName.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        txtName = new JTextField(25);
        txtName.setHorizontalAlignment(JTextField.CENTER);
        txtName.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtName, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        labelAddress = new JLabel("Enter your address: ");
        labelAddress.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelAddress, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        txtAddress = new JTextField(25);
        txtAddress.setHorizontalAlignment(JTextField.CENTER);
        txtAddress.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtAddress, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        labelCity = new JLabel("Enter your city: ");
        labelCity.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelCity, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        txtCity = new JTextField(25);
        txtCity.setHorizontalAlignment(JTextField.CENTER);
        txtCity.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtCity, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        labelState = new JLabel("Enter your State: ");
        labelState.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelState, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        txtState = new JTextField(25);
        txtState.setHorizontalAlignment(JTextField.CENTER);
        txtState.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtState, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 4;
        labelZipCode = new JLabel("Enter your Zip Code: ");
        labelZipCode.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelZipCode, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        txtZipCode = new JTextField(25);
        txtZipCode.setHorizontalAlignment(JTextField.CENTER);
        txtZipCode.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtZipCode, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 5;
        labelPhoneNumber = new JLabel("Enter your Phone Number: ");
        labelPhoneNumber.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        txtPhoneNumber = new JTextField(25);
        txtPhoneNumber.setHorizontalAlignment(JTextField.CENTER);
        txtPhoneNumber.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtPhoneNumber, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 6;
        labelEmail = new JLabel("Enter your Email: ");
        labelEmail.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        txtEmail = new JTextField(25);
        txtEmail.setHorizontalAlignment(JTextField.CENTER);
        txtEmail.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtEmail, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 7;
        labelDOB = new JLabel("Enter your Date of Birth: ");
        labelDOB.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelDOB, gbc);

        gbc.gridx = 1;
        txtMonth = new JComboBox();
        txtMonth.addItem("January");
        txtMonth.addItem("February");
        txtMonth.addItem("March");
        txtMonth.addItem("April");
        txtMonth.addItem("May");
        txtMonth.addItem("June");
        txtMonth.addItem("July");
        txtMonth.addItem("August");
        txtMonth.addItem("September");
        txtMonth.addItem("October");
        txtMonth.addItem("November");
        txtMonth.addItem("December");
        centerPanel.add(txtMonth, gbc);

        gbc.gridx = 2;
        txtDate = new JComboBox();
        for (int i = 0; i < 31; i++) {
            txtDate.addItem(i);
        }
        centerPanel.add(txtDate, gbc);

        gbc.gridx = 3;
        txtYear = new JComboBox();
        for (int i = 1940; i < 2017; i++) {
            txtYear.addItem(i);
        }
        centerPanel.add(txtYear, gbc);
        panel.add(centerPanel);

        /*
                 * End of Center Panel
         */

 /*
                 * South Panel
         */
        JPanel southPanel = new JPanel();
        southPanel.setBackground(Color.GREEN);
        panel.add(southPanel, BorderLayout.SOUTH);

        btnClear = new JButton("CLEAR");
        southPanel.add(btnClear, BorderLayout.CENTER);

        btnNext = new JButton("NEXT");
        southPanel.add(btnNext, BorderLayout.WEST);
        /*
                 * End of South Panel
         */
    }
}

2 个答案:

答案 0 :(得分:2)

  

当我添加所有3个组合框时,它会改变JTextField的宽度。任何有关如何解决此问题的帮助将不胜感激。

默认情况下,GridBagLayout中的每个组件占用1个单元格(垂直一个空格,水平一个)。每次添加组合框时,都会增加网格中的列数,但文本字段仍占用1个水平空间,与第一个组合框对齐。

enter image description here

您希望文本字段与最后一个组合框对齐,换句话说,要占用网格中剩余的任何空间。您可以通过指定

来实现
gbc.gridwidth = GridBagConstraints.REMAINDER;

为您的文字字段。 (在您的情况下gbc.gridwidth = 3也可以,但如果您添加更多组件,则可能需要更改它。)

请注意,由于布局管理器为组件分配空间,因此在文本字段的构造函数中指定数字不会有太大作用,

  

还有一种更好的方法来添加JComboBox的月份,而不是逐个插入。

是的,我建议你利用新的Date-Time API。有一个Enum Month让你的常数准备就绪。不要忘记为组合框指定泛型类型:

JComboBox<Month> txtMonth = new JComboBox<>();
for (Month month : Month.values())
    txtMonth.addItem(month);

我会让你自己纠正大写(提示:getDisplayName)。

答案 1 :(得分:0)

对于第二个问题,您可以使用几个月的数组直接创建JComboBox:

String[] months = {"January", ..., "December"}; // put all the months here
JComboBox txtMonth = new JComboBox(months);

编辑:

对于您的第一个问题:如果您希望每个JTextField的长度与三个JComboBox的长度相同,那么您应该在添加任何JTextField之前编写gbc.gridwidth = 3;

在此处查看更多内容(主要是中间的示例,非常清楚):https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html