I use SpringLayout on my form, But as you see, its look isn't good (large and bad size)!
public class t8 extends JFrame {
JButton okButton, cancellButton;
JTextField idTF, nameTf;
JLabel idlbl, namelbl;
public t8() {
add(createPanel(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setLocation(400, 100);
setVisible(true);
}
public static void main(String[] args) {
new t8();
}
public JPanel createPanel() {
JPanel panel = new JPanel();
okButton = new JButton("Ok");
cancellButton = new JButton("Cancel");
idTF = new JTextField(10);
nameTf = new JTextField(10);
idlbl = new JLabel("ID");
namelbl = new JLabel("Name");
panel.add(idlbl);
panel.add(idTF);
panel.add(namelbl);
panel.add(nameTf);
panel.add(okButton);
panel.add(cancellButton);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 3, 2, 20, 50, 50, 100);
return panel;
}
}
I change makeCompactGrid
numbers, But was not success!
(The width of JTextFields
are large, and my button's size are different)
If you don't care about the layout manager, but only care about the layout, then you should use a
GridLayout
, or if you don't want all component to be the same size, aGridBagLayout
. Here's how with a grid layout (only the modified method is shown):And with a
GridBagLayout
:I believe that the extra
GridBagLayout
complexity just might be worth it.