|
Hi, I am learning how to use group layout in Swing. For example I have these labels and textFileds.
JLabel nameLabel = new JLabel("Name");
JLabel streetLabel = new JLabel("Street");
JLabel suburbLabel = new JLabel("Suburb");
JLabel phoneLabel = new JLabel("Phone");
JLabel emailLabel = new JLabel("Email");
name = new JTextField(20);
street = new JTextField(40);
suburb = new JTextField(20);
phone = new JTextField(20);
email = new JTextField(20);
setFieldsEditable(false);
Now to arrange it using GroupLayout.SequentialGroup class as following part of code, the question is that I don't understand the concept to add the components horizontally and vertically. Could anybody give me some suggestions?
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
// The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
hGroup.addGroup(layout.createParallelGroup().addComponent(nameLabel)
.addComponent(streetLabel).addComponent(suburbLabel).addComponent(
phoneLabel).addComponent(emailLabel));
hGroup.addGroup(layout.createParallelGroup().addComponent(name)
.addComponent(street).addComponent(suburb).addComponent(phone)
.addComponent(email));
layout.setHorizontalGroup(hGroup);
// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(nameLabel).addComponent(name));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(streetLabel).addComponent(street));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(suburbLabel).addComponent(suburb));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(phoneLabel).addComponent(phone));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(emailLabel).addComponent(email));
layout.setVerticalGroup(vGroup);
|