I feel hard to understand how the LayoutManager work even the GUI is quite simple.
I trying to make the button place below the Tel No JTextField, but it placed beside Address JTextField , not below TelNo JTextField.
package gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
public class OrderPage extends JPanel {
public static void main(String[] args) {
OrderPage order = new OrderPage();
order.createAndShowGUI();
JFrame frame = new JFrame();
frame = new JFrame();
frame.setTitle("Details");
frame.getContentPane().add(order);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void createAndShowGUI() {
String[] labels = { "Name: ", "Address: ", "Tel No: " };
int numPairs = labels.length;
// Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
JPanel p1 = new JPanel(new BorderLayout());
JButton button = new JButton("Confirm");
p1.add(button, BorderLayout.SOUTH);
for (int i = 0; i < numPairs; i++) {
JLabel l = new JLabel(labels[i], JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(25);
l.setLabelFor(textField);
p.add(textField);
}
add(p);
add(p1);
// Lay out the panel.
SpringUtilities.makeCompactGrid(p, numPairs, 2, // rows, cols
6, 6, // initX, initY
6, 6); // xPad, yPad
}
}