如何使用GridBagLayout使JLabel 1列宽,然后使JTextField 2列宽(总宽度为3列)

时间:2014-05-29 14:07:58

标签: java swing layout-manager

我已阅读GridBagLayout的文档,但我无法理解它。我基本上想要完成这样的事情:

enter image description here

我制作了一些示例代码来帮助我解决这个问题。如何修改此代码才能实现此目的?

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


public class Main {
    public static void main(String[] args) {

        JLabel label = new JLabel("label");
        JTextField field = new JTextField();

        JLabel label2 = new JLabel("label2");
        JTextField field2 = new JTextField();

        JPanel jp = new JPanel();
        jp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        //gbc.weightx = ??
        jp.add(label, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 2;
        //gbc.weightx = ??
        jp.add(field, gbc);

        JPanel jp2 = new JPanel();
        jp2.setLayout(new GridBagLayout());
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.fill = GridBagConstraints.BOTH;
        gbc2.gridx = 0;
        gbc2.gridy = 0;
        gbc2.gridwidth = 1;
        //gbc2.weightx = ??
        jp2.add(label2, gbc2);
        gbc2.gridx = 1;
        gbc2.gridwidth = 2;
        //gbc2.weightx = ??
        jp2.add(field2, gbc2);

        JPanel jpContainer = new JPanel();
        jpContainer.setLayout(new BoxLayout(jpContainer, BoxLayout.Y_AXIS));
        jpContainer.add(jp);
        jpContainer.add(jp2);

        JFrame f = new JFrame();
        f.setSize(300, 100);
        f.setContentPane(jpContainer);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}

编辑:将JTextArea更改为JTextField

2 个答案:

答案 0 :(得分:1)

每行只有两个组件,因此您只能有两列。

如果您希望JTextArea占用更多空间,请定义JTextArea,如:

JTextArea textArea = new JTextArea(3, 30);

通过指定文本区域的行/列来控制文本区域的大小。

我不确定你为什么要使用JTextArea。看起来像JTextField会更合适。您还可以在创建JTextField时指定列。查看JTextField API。

答案 1 :(得分:1)

使用GridBagLayout columnWidths可以手动调整宽度,然后将GridBagConstraints填充设置为GridBagConstraints.HORIZONTAL

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;


public class Example extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Example frame = new Example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Example() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] {100, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JLabel lblNewLabel = new JLabel("jlabel");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        contentPane.add(lblNewLabel, gbc_lblNewLabel);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("jlabel2");
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
        gbc_lblNewLabel_1.gridx = 0;
        gbc_lblNewLabel_1.gridy = 1;
        contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);

        textField_1 = new JTextField();
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.gridx = 1;
        gbc_textField_1.gridy = 1;
        contentPane.add(textField_1, gbc_textField_1);
        textField_1.setColumns(10);
    }

}

当然,如果你想维护1/3 Label和2/3 JTextField,你可以考虑使用MigLayout:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MigLayoutExample extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MigLayoutExample frame = new MigLayoutExample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MigLayoutExample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new MigLayout("", "[grow 33][grow 66]", "[][]"));

        JLabel lblNewLabel = new JLabel("New label");
        contentPane.add(lblNewLabel, "cell 0 0");

        textField = new JTextField();
        contentPane.add(textField, "cell 1 0,growx");
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("New label");
        contentPane.add(lblNewLabel_1, "cell 0 1");

        textField_1 = new JTextField();
        contentPane.add(textField_1, "cell 1 1,growx");
        textField_1.setColumns(10);
    }

}