gridbag layout
I was recently studying GridBagLayout. As I have insuffecient material
ab GridBagLayout, I tried to learn from experiments.
Here is a code I wrote,
import javax.swing.*;
import java.awt.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frm = new JFrame("Login");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel loginHeading = new JLabel("Login Window");
JLabel userId = new JLabel("User Id : ");
JLabel passwd = new JLabel("Password : ");
JButton login = new JButton("Login");
JTextField userIdField = new JTextField(10);
JTextField passwdField = new JTextField(10);
GridBagLayout gbg = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
frm.setLayout(gbg);
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(25,25,0,0);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbg.setConstraints(loginHeading, gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbg.setConstraints(userId, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbg.setConstraints(userIdField, gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbg.setConstraints(passwd, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbg.setConstraints(passwdField, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbg.setConstraints(login, gbc);
frm.add(loginHeading);
frm.add(userId);
frm.add(userIdField);
frm.add(passwd);
frm.add(passwdField);
frm.add(login);
//frm.pack();
frm.setSize(400,500);
frm.setVisible(true);
}
}
It works fine. But how can I add spacing between the heading label and
the rest of the labels ???