使用GridBagLayout设置文本字段的默认大小

时间:2016-05-24 00:49:21

标签: java eclipse user-interface layout

我在调整文本字段时遇到问题。我一直在尝试使用gridbaglayout,但我正在努力让文本域的大小相同。是否可以使用此布局管理器设置默认文本字段大小?

private void createWidgets() {
         GridBagLayout gridbag = new GridBagLayout();
         GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;

        JPanel leftSidePanel = new BlueJPanel(new GridLayout(4, 1));
        JPanel fNameEntry = new BlueJPanel(gridbag);
        JPanel sNameEntry = new BlueJPanel(gridbag);
        JPanel phoneNoEntry = new BlueJPanel(gridbag);
        JPanel NINumberEntry = new BlueJPanel(gridbag);

        fNameEntry.add(new JLabel("First Name"), c);

        JTextField fNameField = new JTextField();
        fNameField.setColumns(10);
        fNameEntry.add(fNameField, c);

        sNameEntry.add(new JLabel("Surname"), c);
        JTextField sNameField = new JTextField();
        sNameField.setColumns(10);
        sNameEntry.add(sNameField, c);

        phoneNoEntry.add(new JLabel("Phone Number"), c);
        JTextField phoneNoField = new JTextField();
        phoneNoField.setColumns(10);
        phoneNoEntry.add(phoneNoField, c);

        NINumberEntry.add(new JLabel("National Insurance Number"), c);
        JTextField NINumberField = new JTextField();
        NINumberField.setColumns(10);
        NINumberEntry.add(NINumberField, c);

        leftSidePanel.add(fNameEntry);
        leftSidePanel.add(sNameEntry);
        leftSidePanel.add(phoneNoEntry);
        leftSidePanel.add(NINumberEntry);
        this.add(leftSidePanel, BorderLayout.WEST);

不同大小的文本字段

enter image description here 在此先感谢您的帮助!

0 个答案:

没有答案