问题描述:

I have a little problem with (what I suppose) layout manager.

I'm using GridBagLayout to arrange my components but after remove whole set it gets messed up.

Here's code:

 public TabBody()

{

setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.anchor = GridBagConstraints.WEST;

c.gridx = 0;

c.gridy = 0;

c.insets = new Insets(5, 5, 5, 5);

nameField = new JTextField(20);

closeButton = new JButton("x");

closeButton.setMargin(new Insets(0, 0, 0, 0));

closeButton.setPreferredSize(new Dimension(18, 18));

closeButton.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

removeAll();

}

});

amountField = new JTextField(20)

{

@Override

public void processKeyEvent(KeyEvent ev)

{

char c = ev.getKeyChar();

if (c > 48 && c < 57 || c == 8 || ev.getKeyCode() == KeyEvent.VK_LEFT || ev.getKeyCode() == KeyEvent.VK_RIGHT)

{

super.processKeyEvent(ev);

}

}

};

unitsBox = new JComboBox(units);

unitsBox.setSelectedIndex(3);

c.gridx++;

add(new JLabel("Product name"), c);

c.gridx++;

add(new JLabel("Amount"), c);

c.gridx++;

add(new JLabel("Unit"), c);

c.gridy++;

c.gridx++;

add(new JLabel("="), c);

c.gridx++;

add(new JLabel("KCal"), c);

c.gridx = 0;

add(closeButton, c);

c.gridx++;

add(nameField, c);

c.gridx++;

add(amountField, c);

c.gridx++;

add(unitsBox, c);

}

Here's how it looks when I add a few sets of components: http://i.imgur.com/ReQumkr.jpg

And here's how it looks when I remove some of sets: http://i.imgur.com/3cWCYX0.jpg

As You see some of them are not even at X axis.

Thanks from advance.

相关阅读:
Top