Hello,
So I'm trying to make a simple register form in Java. I'm trying to code by myself to learn. I followed this tutorial:
http://www.newthinktank.com/2012/03/java-video-tutorial-30/
My code seems to be fine, but when run the program, it doesn't look the way I wanted. Right now, this is how it looks:
http://i.imgur.com/PgcK1.png
I was trying to get the first label (Navn, it means name) on the same row, but on the left side. Like where "Konsert" is. But I don't understand why it won't go there. I put the x position on my code = 0.
This is my code:
public class Layout extens JPanel
// methods
public Layout() {
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
nameLabel = new JLabel("Name: ");
addComp(thePanel, nameLabel, 0, 0, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
nameField = new JTextField(20);
addComp(thePanel, nameField, 1, 0, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
phoneLabel = new JLabel("Phone nr: ");
addComp(thePanel, phoneLabel, 0, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
phoneField = new JTextField(20);
addComp(thePanel, phoneField, 1, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
// FLOWLAYOUT
JPanel flow = new JPanel();
flow.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
movieLabel= new JLabel("Show: ");
flow.add(movieLabel);
movieCombo = new JComboBox(movies);
flow.add(movieCombo);
placesLabel = new JLabel("Places left: ");
flow.add(placesLabel);
placesField = new JTextField(10);
placesField.setEditable(false);
flow.add(placesField);
addComp(thePanel, flow, 0, 2, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
// END FLOWLAYOUT
amountLabel = new JLabel("Amount: ");
addComp(thePanel, amountLabel, 0, 3, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
model = new SpinnerNumberModel(1, 1, 1, 1);
spinner = new JSpinner(model);
addComp(thePanel, spinner, 1, 3, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
priceLabel = new JLabel("Price: ");
addComp(thePanel, priceLabel, 0, 4, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
priceField = new JTextField(20);
priceField.setEditable(false);
addComp(thePanel, priceField, 1, 4, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
buy = new JButton("Pay");
addComp(thePanel, buy, 0, 5, 1, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
this.add(thePanel);
}
private void addComp(JPanel thePanel, JComponent comp, int xPos, int yPos, int compWidth,
int compHeight, int place, int stretch) {
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 100;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(10,10,10,10);
gridConstraints.anchor = place;
gridConstraints.fill = stretch;
thePanel.add(comp, gridConstraints);
}
What have I done wrong?