简体   繁体   中英

placing components using GridBagLayout() is not giving desired result

I want to create a data entry tab as follows along with few buttons:

在此处输入图片说明

where width of all textfields is 3, heights of 'Name' and 'Amount paid' are 1 whereas height of address is 3. There is a vertical gap of 1 line between them.

For this purpose I wrote the code as follows: (data_panel is seperated out from button_panel so as to get better component placements)

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

This code gave me the output as below:

在此处输入图片说明

looking closely:

在此处输入图片说明

I tried various changes including those of using gbc.fill and different dimensions in JTextField() , gbc.gridx and gbc.gridy , etc. But I'm unable to get desired result.

Can anybody please help me to sort out the problem?

Maybe this will help you out. I know its not exactly what your looking for, but it demonstrates how to use the GB layout fairly well I guess:

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

}

The problem is, you forgot to pass the GridBagConstraints when you added label_amount_paid and 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

Remember, by default, the x/y positions are 0 based.

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

支付额

I'd also be very careful with gridWidth and gridHeight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM