Source of GridBagLayoutDemo.java


  1: //GridBagLayoutDemo.java (from Lemay)
  2: import java.awt.*;
  3: import javax.swing.*;
  4: import java.awt.event.*;

  6: public class GridBagLayoutDemo extends JFrame
  7: {
  8:     //Helper function for setting constraints
  9:     void buildConstraints(GridBagConstraints gbc,
 10:                           int gx, int gy,
 11:                           int gw, int gh,
 12:                           int wx, int wy)
 13:     {
 14:         gbc.gridx      = gx;
 15:         gbc.gridy      = gy;
 16:         gbc.gridwidth  = gw;
 17:         gbc.gridheight = gh;
 18:         gbc.weightx    = wx;
 19:         gbc.weighty    = wy;
 20:     }

 22:     public GridBagLayoutDemo()
 23:     {
 24:         super("Username and Password");
 25:         setSize(290, 110);
 26:         GridBagLayout gridbag = new GridBagLayout();
 27:         GridBagConstraints constraints = new GridBagConstraints();
 28:         JPanel pane = new JPanel();
 29:         pane.setLayout(gridbag);

 31:         //Name label
 32:         buildConstraints(constraints, 0, 0, 1, 1, 10, 40);
 33:         constraints.fill = GridBagConstraints.NONE;
 34:         constraints.anchor = GridBagConstraints.EAST;
 35:         JLabel nameLabel = new JLabel("Username: ", JLabel.LEFT);
 36:         gridbag.setConstraints(nameLabel, constraints);
 37:         pane.add(nameLabel);

 39:         //Name text field
 40:         buildConstraints(constraints, 1, 0, 1, 1, 90, 0);
 41:         constraints.fill = GridBagConstraints.HORIZONTAL;
 42:         JTextField nameField = new JTextField();
 43:         gridbag.setConstraints(nameField, constraints);
 44:         pane.add(nameField);

 46:         //Password label
 47:         buildConstraints(constraints, 0, 1, 1, 1, 0, 40);
 48:         constraints.fill = GridBagConstraints.NONE;
 49:         constraints.anchor = GridBagConstraints.EAST;
 50:         JLabel passwordLabel = new JLabel("Password: ", JLabel.LEFT);
 51:         gridbag.setConstraints(passwordLabel, constraints);
 52:         pane.add(passwordLabel);
 53: 
 54:         //Password text field
 55:         buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
 56:         constraints.fill = GridBagConstraints.HORIZONTAL;
 57:         JPasswordField passwordField = new JPasswordField();
 58:         passwordField.setEchoChar('*');
 59:         gridbag.setConstraints(passwordField, constraints);
 60:         pane.add(passwordField);

 62:         //OK Button
 63:         buildConstraints(constraints, 0, 2, 2, 1, 0, 20);
 64:         constraints.fill = GridBagConstraints.NONE;
 65:         constraints.anchor = GridBagConstraints.CENTER;
 66:         JButton okButton = new JButton("OK");
 67:         okButton.addActionListener(
 68:             new ActionListener()
 69:             {
 70:                 public void actionPerformed(ActionEvent e)
 71:                 {
 72:                     JOptionPane.showMessageDialog(GridBagLayoutDemo.this,
 73:                         "Thanks for logging in (or not).\n" +
 74:                         "But we are now going to quit anyway!",
 75:                         "Saying Goodbye", JOptionPane.INFORMATION_MESSAGE);
 76:                     System.exit(0);
 77:                 }
 78:             });
 79:         gridbag.setConstraints(okButton, constraints);
 80:         pane.add(okButton);

 82:         //Content Pane
 83:         setContentPane(pane);
 84:     }

 86:     public static void main(String[] arguments)
 87:     {
 88:         GridBagLayoutDemo frame = new GridBagLayoutDemo();
 89:         ExitWindow exit = new ExitWindow();
 90:         frame.addWindowListener(exit);
 91:         frame.show();
 92:     }
 93: }

 95: class ExitWindow extends WindowAdapter
 96: {
 97:     public void windowClosing(WindowEvent e)
 98:     {
 99:         System.exit(0);
100:     }
101: }