使用GridBagLayout()放置组件并不能提供所需的结果

时间:2014-02-14 21:58:34

标签: java swing user-interface awt gridbaglayout

我想创建一个数据输入标签,如下所示:

enter image description here

其中所有文本字段的宽度为3,“名称”和“支付金额”的高度为1,而地址高度为3.它们之间存在1行的垂直间隙。

为此,我编写了如下代码:( data_panel与button_panel分开,以便获得更好的组件展示位置)

private JLabel label_name;
private JTextField text_name;

private JLabel label_address;
private JTextField text_address;

private JLabel label_amount_paid;
private JTextField text_amount_paid;

private JButton button_add_member;
private JButton button_remove_member;
private JButton button_remove_all;

public TabPanesAddRemove() {
    setLayout(new GridLayout(2, 1));

    JPanel data_panel = new JPanel(new GridBagLayout()); // data entry panel
    GridBagConstraints gbc = new GridBagConstraints();
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    label_name = new JLabel("Name: ");
    data_panel.add(label_name, gbc);

    text_name = new JTextField(20);
    text_name.setName("name");
    gbc = new GridBagConstraints();
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 2;
    gbc.gridy = 1;
    gbc.gridwidth = 3;
    data_panel.add(text_name, gbc);

    label_address = new JLabel("Address: ");
    gbc = new GridBagConstraints();
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    data_panel.add(label_address, gbc);

    text_address = new JTextField(20);
    text_address.setName("address");
    gbc = new GridBagConstraints();
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.gridwidth = 3;
    gbc.gridheight = 3;
    data_panel.add(text_address, gbc);

    label_amount_paid = new JLabel("Amount Paid: ");
    gbc = new GridBagConstraints();
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 7;
    gbc.gridwidth = 1;
    // gbc.gridheight = 1;
    data_panel.add(label_amount_paid);

    text_amount_paid = new JTextField(20);
    text_amount_paid.setName("amount");
    gbc = new GridBagConstraints();
    // gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 2;
    gbc.gridy = 7;
    gbc.gridwidth = 3;
    data_panel.add(text_amount_paid);

    add(data_panel);

    JPanel buttons_panel = new JPanel(); // button panel
    button_add_member = new JButton(BUTTON_ADD);
    button_remove_member = new JButton(BUTTON_REMOVE);
    button_remove_all = new JButton(BUTTON_REMOVE_ALL);
    buttons_panel.add(button_add_member);
    buttons_panel.add(button_remove_member);
    buttons_panel.add(button_remove_all);
    add(buttons_panel);

    Event ev = new Event();
    button_add_member.addActionListener(ev);
    button_remove_member.addActionListener(ev);
    button_remove_all.addActionListener(ev);
}

这段代码给了我如下输出:

enter image description here

仔细观察:

enter image description here

我尝试了各种更改,包括使用gbc.fillJTextField()gbc.gridxgbc.gridy等不同维度的更改。但我无法获得理想的结果。

有人可以帮我解决问题吗?

2 个答案:

答案 0 :(得分:0)

也许这会帮助你。我知道它不完全是你想要的,但它演示了如何使用GB布局我猜:

public class MainF extends JFrame {

public static void main(String[] args) {
    MainF f = new MainF();
    f.setVisible(true);

}


public MainF() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(100, 100, 600, 300);

    Dimension tfSize = new Dimension(200, 25);
    JPanel dataP = new JPanel();
    dataP.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    JLabel label = new JLabel("Name:");

    c.gridx = 1;
    c.gridy = 1;
    c.anchor = GridBagConstraints.NORTHEAST;

    dataP.add(label, c);

    JTextField tf = new JTextField(30);
    tf.setMinimumSize(tfSize);
    tf.setMaximumSize(tfSize);
    tf.setPreferredSize(tfSize);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 3;
    dataP.add(tf, c);

    label = new JLabel("Address:");
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 2;
    c.anchor = GridBagConstraints.NORTHEAST;
    dataP.add(label, c);

    tf = new JTextField(30);
    tf.setMinimumSize(tfSize);
    tf.setMaximumSize(tfSize);
    tf.setPreferredSize(tfSize);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 2;
    c.gridwidth = 3;
    dataP.add(tf, c);

    label = new JLabel("Amount Paid:");
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 3;
    c.anchor = GridBagConstraints.NORTHEAST;
    dataP.add(label, c);

    tf = new JTextField(30);
    tf.setMinimumSize(tfSize);
    tf.setMaximumSize(tfSize);
    tf.setPreferredSize(tfSize);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 3;
    dataP.add(tf, c);

    JButton button = new JButton("Add Member");
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 5;
    dataP.add(button, c);

    button = new JButton("Remove Member");
    c = new GridBagConstraints();
    c.gridx = 3;
    c.gridy = 5;
    dataP.add(button, c);

    button = new JButton("Remove All Members");
    c = new GridBagConstraints();
    c.gridx = 4;
    c.gridy = 5;
    dataP.add(button, c);
    this.add(dataP);
}

}

答案 1 :(得分:0)

问题是,当您添加GridBagConstraintslabel_amount_paid

时,您忘记传递text_amount_paid
label_amount_paid = new JLabel("Amount Paid: ");
gbc = new GridBagConstraints();
//...
data_panel.add(label_amount_paid); // Missing constraints

text_amount_paid = new JTextField(20);
text_amount_paid.setName("amount");
gbc = new GridBagConstraints();
//...
data_panel.add(text_amount_paid); // Missing constraints

请记住,默认情况下,x / y位置为0。

label_amount_paid = new JLabel("Amount Paid: ");
gbc = new GridBagConstraints();
//...
data_panel.add(label_amount_paid, gbc);

text_amount_paid = new JTextField(20);
text_amount_paid.setName("amount");
//...
gbc = new GridBagConstraints();
data_panel.add(text_amount_paid, gbc);

Payout

我对gridWidthgridHeight

也要非常小心