Visual Programming - Grid Bag Layout
Thanks to my friend for the frame.pack (); it's very useful when calculating the size was a problem......
package latihan5;
import javax.swing.*;
import java.awt.*;
public class Latihan5 {
public Latihan5 ()
{
JFrame frame = new JFrame ("Contoh Strust, Grid Area, Glue");
frame.setSize (600,600);
frame.setLocation(1, 1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setPreferredSize(new Dimension (600,600));
JLabel labelNama = new JLabel ("Nama :");
JLabel labelPhone = new JLabel ("Phone :");
JLabel labelAddress = new JLabel ("Address :");
JRadioButton button1 = new JRadioButton ("Small");
JRadioButton button2 = new JRadioButton ("Meidum");
JRadioButton button3 = new JRadioButton ("Large");
ButtonGroup grup1 = new ButtonGroup ();
grup1.add(button1);
grup1.add(button2);
grup1.add(button3);
Box box1 = Box.createVerticalBox();
box1.setBorder (BorderFactory.createTitledBorder("Size"));
box1.add(button1);
box1.add(button2);
box1.add(button3);
JRadioButton button4 = new JRadioButton ("Thin");
JRadioButton button5 = new JRadioButton ("Thick");
ButtonGroup grup2 = new ButtonGroup ();
grup2.add(button4);
grup2.add(button5);
Box box2 = Box.createVerticalBox();
box2.setBorder (BorderFactory.createTitledBorder ("Style"));
box2.add(button4);
box2.add(button5);
JCheckBox check1 = new JCheckBox ("Tuna Melt");
JCheckBox check2 = new JCheckBox ("Meat Lover");
JCheckBox check3 = new JCheckBox ("Idaho");
Box box3 = Box.createVerticalBox();
box3.setBorder (BorderFactory.createTitledBorder ("Topping"));
box3.add(check1);
box3.add(check2);
box3.add(check3);
JButton button6 = new JButton ("OK");
JButton button7 = new JButton ("Cancel");
Box box4 = Box.createVerticalBox();
box4.add(button6);
box4.add(button7);
box4.add(Box.createHorizontalGlue());
JTextField textNama = new JTextField(28);
JTextField textPhone = new JTextField(16);
JTextField textAddress = new JTextField(28);
addItem(panel, labelNama, 0, 0, 1, 1, GridBagConstraints.EAST);
addItem(panel, labelPhone, 0, 1, 1, 1, GridBagConstraints.EAST);
addItem(panel, labelAddress, 0, 2, 1, 1, GridBagConstraints.EAST);
addItem(panel, textNama, 1, 0, 2, 1, GridBagConstraints.WEST);
addItem(panel, textPhone, 1, 1, 1, 1, GridBagConstraints.WEST);
addItem(panel, textAddress, 1, 2, 2, 1, GridBagConstraints.WEST);
addItem(panel, box1, 0, 3, 1, 1, GridBagConstraints.WEST);
addItem(panel, box2, 1, 3, 1, 1, GridBagConstraints.CENTER);
addItem(panel, box3, 2, 3, 1, 1, GridBagConstraints.EAST);
addItem(panel, box4, 2, 4, 1, 1, GridBagConstraints.EAST);
frame.add(panel);
frame.setVisible(true);
//frame.pack ();
}
private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align)
{
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
public static void main(String[] args) {
// TODO code application logic here
new Latihan5();
}
}
Labels:
Coding