Gridbag Layout

//<applet code=GridBagLayoutDemo1 height=400 width=500></applet>
// example of GridBagLayout & GridBagConstraints
import java.awt.*;
import javax.swing.*;

public class GridBagLayoutDemo1 extends JApplet {
	JPanel p1;
	JButton b1, b2, b3, b4, b5;
	JTextArea area;
	JTextField t1;
	GridBagLayout gbl;
	GridBagConstraints gbc;

	@Override
	public void init() {
		p1 = new JPanel();
		b1 = new JButton("button1");
		b2 = new JButton("button2");
		b3 = new JButton("button3");
		b4 = new JButton("button4");
		b5 = new JButton("button5");
		t1 = new JTextField(7);

		area = new JTextArea(10, 5);
		gbl = new GridBagLayout();
		gbc = new GridBagConstraints();

		p1.setLayout(gbl);

		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = 10;
		gbc.gridy = 10;
		gbl.setConstraints(b1, gbc);
		p1.add(b1);
		p1.add(area);
		p1.add(t1);

		gbc.gridx = 50;
		gbc.gridy = 10;
		gbl.setConstraints(b2, gbc);
		p1.add(b2);

		p1.add(area);
		// p1.add(t1);
		gbc.gridx = 10;
		gbc.gridy = 40;
		gbl.setConstraints(b3, gbc);
		p1.add(b3);
		p1.add(area);
		// p1.add(t1);

		gbc.gridx = 50;
		gbc.gridy = 40;
		gbl.setConstraints(b4, gbc);
		p1.add(b4);

		getContentPane().add(p1);
	}
}

Download

Layout Manager in Java

Border Layout
Box Layout
Card Layout
Flow Layout
Gridbag Layout
Gridbag Layout
Grid Layout
Design GUI contianer without layout


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code