Swings and Layout Managers
Swings and Layout Managers
SWINGS
When awt components are created, internally native methods (c language functions) are
executed which create peer component (equivalent component). awt components are heavy weight
components. The look and feel of awt component change depending upon the operating system. JFC
represents a class library that is extended from awt. JFC stands for Java Foundation Classes. JFC is an
extension of the original awt, it does not replace awt. JFC components are light weight components,
they take less system resources.
Creating a Frame:
import javax.swing.*;
class MyFrame
{
public static void main (String agrs[])
{
JFrame jf = new JFrame ("My Swing Frame...");
jf.setSize (400,200);
jf.setVisible (true);
jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
1
Compilation & Execution:
javac MyFrame.java
java MyFrame
Output:
Note: To close the frame, we can take the help of getDefaultCloseOperation () method of JFrame
class, as shown here:
getDefaultCloseOperation (constant);
where the constant can be any one of the following:
JFrame.EXIT_ON_CLOSE: This closes the application upon clicking on close button.
JFrame.DISPOSE_ON_CLOSE: This disposes the present frame which is visible on the screen.
The JVM may also terminate.
JFrame.DO_NOTHING_ON_CLOSE: This will not perform any operation upon clicking on
close button.
JFrame.HIDE_ON_CLOSE: This hides the frame upon clicking on close button.
Window Panes:
In swings the components are attached to the window panes only. A window pane represents a free
area of a window where some text or components can be displayed. For example, we can create a
frame using JFrame class in javax.swing which contains a free area inside it, this free area is called
'window pane'.
Four types of window panes are available in javax.swing package.
2
Glass Pane: This is the first pane and is very close to the monitors screen. Any components
to be displayed in the foreground are attached to this glass pane. To reach this glass pane,
we use getGlassPane () method of JFrame class.
Root Pane: This pane is below the glass pane. Any components to be displayed in the
background are displayed in this pane. Root pane and glass pane are used in animations
also. For example, suppose we want to display a flying aeroplane in the sky. The aeroplane
can be displayed as a .gif or .jpg file in the glass pane where as the blue sky can be displayed
in the root pane in the background. To reach this root pane, we use getRootPane () method
of JFrame class.
Layered Pane: This pane lies below the root pane. When we want to take several
components as a group, we attach them in the layered pane. We can reach this pane by
calling getLayeredPane () method of JFrame class.
Content Pane: This is the bottom most pane of all. Individual components are attached to
this pane. To reach this pane, we can call getContentPane () method of JFrame class.
import javax.swing.*;
import java.awt.*;
class MyPanel extends JPanel
{
public void paintComponent (Graphics g)
{
super.paintComponent (g); //call JPanel’s method
setBackground (Color.red);
g.setColor (Color.white);
g.setFont (new Font("Courier New",Font.BOLD,30));
g.drawString ("Hello Readers!", 50, 100);
}
}
class FrameDemo extends JFrame
{
FrameDemo ()
{
Container c = getContentPane ();
MyPanel mp = new MyPanel ();
c.add (mp);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
FrameDemo ob = new FrameDemo ();
ob.setSize (600, 200);
ob.setVisible (true);
}
3
}
Output:
The following methods of JComponent class are very useful while handling the components:
To set some background color to the component, we can use setBackground () method, as:
component.setBackground (Color.yellow);
To set the foreground color to the component, we can use setForeground () method, as:
component.setForeground (Color.red);
To set some font for the text displayed on the component, we can use setFont () method.
We should pass Font class object to this method, as:
component.setFont (Font obj);
where, Font class object can be created as:
Font obj = new Font (“fontname”, style, size);
To set the tool tip text, we can use setToolTipText () method, as:
component.setToolTipText (“This is for help”);
To set a mnemonic, we can use setMnemonic () method, as:
4
component.setMnemonic (‘c’);
To enable or disable a component, we can use setEnabled () method, as:
component.setEnabled (true);
To make a component to be visible or invisible, we can use setVisible () method, as:
component.setVisible (true);
To know the current height of the component, use getHeight () method, as:
component.getHeight ();
To know the current width of the component, use getWidth () method, as:
component.getWidth ();
To know the current x coordinate of the component
component.getX ()
To know the current y coordinate of the component
component.getY ()
To set the location of the component in the frame, we can use setBounds () method, as:
component.setBounds (x, y, width, height);
PushButton:
To create a JButton with text: JButton b = new JButton (“OK”);
To create a JButton with image: JButton b = new JButton (ImageIcon ii);
To create a JButton with text & image: JButton b = new JButton (“OK”, ImageIcon ii);
It is possible to create components in swing with images on it. The image is specified by ImageIcon
class object.
Ex: Write a program to create a JButton component, set the button fore ground and background
color.
import javax.swing.*;
import java.awt.*;
class JButton1 extends JFrame
{
JButton b;
ImageIcon ii;
JButton1 ()
{
Container c = getContentPane ();
c.setLayout (null);
b = new JButton ("Click ME");
b.setBounds(100,20,150,150);
b.setBackground (Color.yellow);
b.setForeground (Color.red);
c.add (b);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
JButton1 ob = new JButton1 ();
ob.setTitle ("Swing Frame example");
ob.setSize (600,250);
ob.setVisible (true);
}
5
}
Compilation & Execution:
javac JButton1.java
java JButton1
Output:
Ex: Write a program to create a JButton component, set image icon on the button
import javax.swing.*;
import java.awt.*;
class JButton1 extends JFrame
{
JButton b;
ImageIcon ii;
JButton1 ()
{
Container c = getContentPane ();
c.setLayout (null);
ii = new BackgroundImageIcon("abc.jpg");
b = new JButton (ii);
b.setBounds(100,20,150,150);
b.setBackground (Color.yellow);
c.add (b);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
JButton1 ob = new JButton1 ();
ob.setTitle ("Swing Frame example");
ob.setSize (600,250);
ob.setVisible (true);
}
}
Compilation & Execution:
javac JButton1.java
java JButton1
Output:
6
Label:
To create a JLabel component: JLabel lbl = new JLabel ();
To set text to JLabel component: lbl.setText ("Hello");
To create a JLabel with text: JLabel lbl = new JLabel ("text",
JLabel.RIGHT);
To create a JLabel with image: JLabel lbl = new JLabel (ImageIcon ii);
7
Creating Components in Swing:
Ex:Write a program to create check boxes, radio buttons and text area components
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class CheckRadio extends JFrame implements ActionListener
{
JCheckBox cb1,cb2;
JRadioButton rb1,rb2;
ButtonGroup bg;
JTextArea ta;
String msg = "";
CheckRadio ()
{
Container c = getContentPane ();
c.setLayout (new FlowLayout ());
cb1 = new JCheckBox ("J2SE", true);
cb2 = new JCheckBox ("J2EE");
rb1 = new JRadioButton ("Male", true);
rb2 = new JRadioButton ("Female");
bg = new ButtonGroup();
8
bg.add (rb1);
bg.add (rb2);
ta = new JTextArea (5, 20);
c.add (cb1);
c.add (cb2);
c.add (rb1);
c.add (rb2);
c.add (ta);
cb1.addActionListener (this);
cb2.addActionListener (this);
rb1.addActionListener (this);
rb2.addActionListener (this);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed (ActionEvent ae)
{
if (cb1.getModel ().isSelected ()) //to know the user selection
msg += "J2SE";
if (cb2.getModel ().isSelected ())
msg += "J2EE";
if (rb1.getModel ().isSelected ())
msg += "Male";
if (rb2.getModel ().isSelected () )
msg += "Female";
ta.setText (msg);
msg ="";
}
public static void main(String args[])
{
CheckRadio ob = new CheckRadio ();
ob.setTitle ("Swing Frame example");
ob.setSize (600,250);
ob.setVisible (true);
}
}
9
JComboBox class:
JComboBox allows us to create a combo box, with a group of items which are displayed as a drop
down list. The user can select a single item only.
To create an empty combo box: JComboBox box = new JComboBox ();
To create a combo box with array of elements: JComboBox box = new JComboBox (Object arr[]);
To create a combo box with vector of elements: JComboBox box = new JComboBox (Vector v);
To add the items to the combo box: box.addItem (“ India”);
To retrieve the selected item from the combo box: Object obj = box.getSelectedItem ();
To retrieve the selected items index: int i = box.getSelectedIndex ();
To get the item of combo box by giving index: Object obj = box.getItemAt (int index);
To get number of items in the combo box: int n = box.getItemCount ();
To remove an item obj from the combo box: box.removeItem (Object obj);
To remove an item by giving index: box.removeItemAt (int index);
To remove all items: box.removeAllItems ();
Ex: Write a program to create a combo box with names of some countries. //JComboBox Demo
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JComboBoxDemo extends JFrame implements ItemListener
{
JComboBox box;
JLabel lbl;
JComboBoxDemo ()
{
Container c = getContentPane();
c.setLayout (null);
box = new JComboBox ();
box.addItem ("India");
box.addItem ("Pakistan");
box.addItem ("Afghanistan");
box.addItem ("China");
box.addItem ("Sri Lanka");
box.addItem ("Bangladesh");
box.setBounds (100, 50, 100, 30);
c.add (box);
box.addItemListener (this);
lbl = new JLabel ();
lbl.setBounds (100, 120,200,40);
c.add(lbl);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged (ItemEvent ie)
{
String str = (String) box.getSelectedItem ();
lbl.setText ("You Selected : " + str);
10
}
public static void main(String args[])
{
JComboBoxDemo ob = new JComboBoxDemo ();
ob.setSize (500,250);
ob.setTitle ("My Combo Box Demo");
ob.setVisible (true);
}
}
Output:
JList Class:
JList class is useful to create a list which displays a list of items and allows the user to select one or
more items.
To create an empty list: JList lst = new JList();
lst.addItem(“india”);
To create a list with the array of elements: JList lst = new JList (Object arr []);
To create a list with vector of elements: JList lst = new JList (Vector v);
To know which item is selected in the list: Object item = lst.getSelectedValue ();
To know the selected items index: int index = lst.getSelectedIndex ();
To get all the selected items into an array: Object arr [] = lst.getSelectedValues ();
To get the indexes of all selected items: int arr[] = lst.getSelectedIndices ();
Ex: Write a program to create a list box with names of some countries such that user can select
one or more items. //JList Demo
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class JListDemo extends JFrame implements ListSelectionListener
{
JList lst;
JLabel lbl;
Object arr [];
String msg="";
JListDemo ()
{
Container c = getContentPane();
c.setLayout (null);
String items [ ] = {"India","America","Germany","Japan","France"};
lst = new JList (items);
lst.setBounds(100,20,100,100);
11
c.add (lst);
lst.addListSelectionListener (this);
lbl = new JLabel ();
lbl.setBounds (100, 150,200,40);
c.add(lbl);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void valueChanged (ListSelectionEvent le)
{
arr= lst.getSelectedValues ();
for ( int i=0;i<arr.length;i++)
msg += (String) arr [i];
lbl.setText ("You Selected : " + msg);
msg = "";
}
public static void main(String args[])
{
JListDemo ob = new JListDemo ();
ob.setSize (500,250);
ob.setTitle ("JListDemo");
ob.setVisible (true);
}
}
Output:
JTabbedPane:
JTabbedPane is a container to add multiple components on every tab. The user can choose a
component from a tab.
To create a JTabbedPane: JTabbedPane jtp = new JTabbedPane ();
To add tabs: jtp.addTab ("title", object);
To create a Panel containing some components: class MyPanel extends JPanel
Now pass 'MyPanel' class object to addTab ()
To remove a tab (and its components) from the tabbedpane: jtp.removeTabAt (int index);
To remove all the tabs and their corresponding components: jtp.removeAll ();
Ex: Write a program to create a tabbed pane with two tab sheets. //JTabbedPane demo
import java.awt.*;
import javax.swing.*;
class JTabbedPaneDemo extends JFrame
{
JTabbedPaneDemo ()
12
{
JTabbedPane jtp = new JTabbedPane ();
jtp.add ("Countries", new CountriesPanel ());
jtp.add ("Capitals", new CapitalsPanel ());
Container c = getContentPane ();
c.add (jtp);
}
public static void main(String args[])
{
JTabbedPaneDemo demo = new JTabbedPaneDemo ();
demo.setSize (600,450);
demo.setVisible (true);
demo.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
class CountriesPanel extends JPanel
{
CountriesPanel ()
{
JButton b1, b2, b3;
b1 = new JButton ("America");
b2 = new JButton ("India");
b3 = new JButton ("Japan");
add (b1); add (b2); add (b3);
}
}
class CapitalsPanel extends JPanel
{
CapitalsPanel ()
{
JCheckBox cb1 = new JCheckBox ("Washington");
JCheckBox cb2 = new JCheckBox ("New Delhi");
JCheckBox cb3 = new JCheckBox ("Tokyo");
add (cb1); add (cb2); add (cb3);
}
}
Output:
13
JTable:
JTable represents data in the form of a table. The table can have rows of data, and column headings.
To create a JTable: JTable tab = new JTable (data, column_names); Here, data and
column_names can be a 2D array or both can be vector of vectors.
To create a row using a vector: Vector row = new Vector();
row.add(object); //here object represents a column
row.add (object);
row.add (object);
To create a table heading, we use getTableHeader () method of JTable class.
JTableHeader head = tab.getTableHeader ();
Ex: Write a program that creates a table with some rows and columns. //creating a table
import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
class JTableDemo extends JFrame
{
JTableDemo ()
{
Vector <Vector> data = new Vector <Vector> ();
//create first row
Vector <String> row = new Vector <String> ();
row.add ("Dimple");
row.add ("emp1);
row.add ("50000");
data.add (row);
row = new Vector <String>(); //create second row
row.add ("Mahananda");
row.add ("emp2");
row.add ("50000");
data.add (row);
row = new Vector<String> (); //create third row
row.add ("pallavi");
row.add ("emp3");
row.add ("50000");
data.add (row);
//create a row with column names
Vector <String> cols = new Vector<String> ();
cols.add ("Employee Name");
cols.add ("Designation");
cols.add ("Salary");
//create the table
JTable tab = new JTable (data, cols);
//create table header object
JTableHeader head = tab.getTableHeader ();
Container c = getContentPane ();
14
c.setLayout (new BorderLayout ());
c.add ("North", head);
c.add ("Center", tab);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
JTableDemo demo = new JTableDemo ();
demo.setSize (600, 250);
demo.setVisible (true);
}
}
Output:
JMenu Class:
A menu represents a group of items or options for the user to select. To create a menu, the
following steps should be used:
Create a menu bar using JMenuBar class object: JMenuBar mb = new JMenuBar ();
Attach this member to the container. c.add (mb);
Create separate menu to attach to the menubar. JMenu file = new JMenu ("File");
Note: Here, "File" is title for the menu which appear in the menubar.
Attach this menu to the menubar. mb.add (file);
Create menuitems using JMenuItem or JCheckBoxMenuItem or JRadioButtonMenuItem
classes. JMenuItem op = new JMenuITem ("Open");
Attach this menuitem to the menu. file.add (op);
Creating a submenu:
o Create a menu: JMenu font = new JMenu ("Font");
Here, font represents a submenu to be attached to a menu.
o Now attach it to a menu: file.add (font);
o Attach menuitems to the font submenu. font.add (obj);
Note: obj can be a JMenuItem or JCheckBoxMenuItem or JRadioButtonMenuItem
Ex: Write a program to create a menu with several menu items. //Menu Creation
import java.awt.*;
15
import javax.swing.*;
import java.awt.event.*;
class MyMenu extends JFrame implements ActionListener
{
JMenuBar mb;
JMenu file, edit, font;
JMenuItem op, cl, cp, pt;
JCheckBoxMenuItem pr;
MyMenu ()
{
Container c = getContentPane ();
c.setLayout (new BorderLayout ());
mb = new JMenuBar ();
c.add ("North", mb);
//create file, edit menus
file = new JMenu ("File");
edit = new JMenu ("Edit");
//add menus to mb
mb.add (file);
mb.add (edit);
//create menuitems
op = new JMenuItem ("Open");
cl = new JMenuItem ("Close");
cp = new JMenuItem ("Copy");
pt = new JMenuItem ("Paste");
//add menu items to menus
file.add (op);
file.add (cl);
file.add (cp);
file.add (pt);
//disable close
cl.setEnabled (false);
//create checkbox menu item
pr = new JCheckBoxMenuItem ("Print");
//add checkbox menu item to file menu
file.add (pr);
//display line (separator)
file.addSeparator ();
//create submenu
font = new JMenu ("Font");
//add this submenu in file
file.add (font);
//add menu items to font
font.add ("Arial");
font.add ("Times New Roman");
//add action listeners to menu items
op.addActionListener (this);
cl.addActionListener (this);
cp.addActionListener (this);
pt.addActionListener (this);
pr.addActionListener (this);
16
//close the frame
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if (op.isArmed())
System.out.println ("Open is selected");
if (cl.isArmed())
System.out.println ("Close is selected");
if (cp.isArmed())
System.out.println ("Copy is selected");
if (pt.isArmed())
System.out.println ("Paste is selected");
if (pr.isArmed())
System.out.println ("Print is selected");
}
public static void main (String args[])
{
MyMenu ob = new MyMenu ();
ob.setSize (600,450);
ob.setVisible (true);
}
}
Output:
17
Layout Manager:
Layout Manager is an interface that arranges the components on the screen. Layout Manager is
implemented in the following classes: FlowLayout, BorderLayout, CardLayout, GridLayout,
GridBagLayout.
FlowLayout:
FlowLayout is useful to arrange the components in a line after the other. When a line is filled with
components, they are automatically placed in the next line.
Ex: Write a program to create push buttons and arrange them using flow layout. //Flow layout
example
import javax.swing.*;
import java.awt.*;
class Example1 extends JFrame
{
Example1 ()
{
Container c = getContentPane ();
c.setLayout(new FlowLayout(FlowLayout.LEFT,20,40));
// where 20 and 40 specifies hgap and vgap respectively.
JButton b1 = new JButton ("First");
JButton b2 = new JButton ("Second");
JButton b3 = new JButton ("Third");
18
BorderLayout:
BorderLayout is useful to arrange the components in the 4 borders of the frame as well as in the
center. The borders are specified as South, North, East, West and Center.
Ex: Write a program to create push buttons and arrange them using border layout. //Border layout
example
import javax.swing.*;
import java.awt.*;
class Example2 extends JFrame
{
Example2 ()
{
Container c = getContentPane ();
c.setLayout (new BorderLayout ());
JButton b1 = new JButton ("First");
JButton b2 = new JButton ("Second");
JButton b3 = new JButton ("Third");
JButton b4 = new JButton ("Fourth");
JButton b5 = new JButton ("Fifth");
//add(c);
c.add (b1,"North"); c.add (b2,"South"); c.add (b3, "Center");
c.add (b4,"East"); c.add (b5,"West");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
Example2 ob = new Example2 ();
ob.setTitle ("Border Layout...");
ob.setSize (600,250);
ob.setVisible (true);
}
}
Output:
19
CardLayout:
A cardLayout is a layout manager which treats each component as a card. Only one card is visible at
a time and the container acts as a stack of cards.
Ex: Write a program to create push buttons and arrange them using border layout. //Card layout
example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Example3 extends JFrame implements ActionListener
{
Container c;
CardLayout card;
Example3 ()
{
c = getContentPane ();
card = new CardLayout (50,50);
c.setLayout (card);
JButton b1 = new JButton ("First");
JButton b2 = new JButton ("Second");
JButton b3 = new JButton ("Third");
c.add ("button1", b1);
c.add ("button2", b2);
c.add("button3", b3);
b1.addActionListener (this);
b2.addActionListener (this);
b3.addActionListener (this);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
card.next(c); // when a button is clicked show the next card.
}
public static void main(String args[])
{
Example3 ob = new Example3 ();
ob.setTitle ("Card Layout...");
ob.setSize (600,250);
ob.setVisible (true);
}
}
Output:
20
GridLayout:
It is useful to divide the container into a two dimensional grid form that contains several rows and
columns. The container is divided into equal sized rectangles and one component is placed in each
rectangle.
GridLayout (),
GridLayout (int rows, int cols),
GridLayout (int rows, int cols, int hgap, int vgap) are constructors.
Ex: Write a program to create push buttons and arrange them using grid layout. //Grid layout
example
import javax.swing.*;
import java.awt.*;
class Example4 extends JFrame
{
Container c;
GridLayout grid;
Example4 ()
{
c = getContentPane ();
grid = new GridLayout (2,2,50,10);
c.setLayout (grid);
JButton b1 = new JButton ("First");
JButton b2 = new JButton ("Second");
JButton b3 = new JButton ("Third");
c.add ("button1",b1);
c.add ("button2",b2);
c.add ("button3",b3);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[])
{
Example4 ob = new Example4 ();
ob.setTitle ("Grid Layout...");
ob.setSize (600,250);
ob.setVisible (true);
}
}
Output:
21
GridBagLayout:
This layout is more flexible as compared to other layouts since in this layout the components can
span more than one row or column and the size of the components can be adjusted to fit the display
area.
To create grid bag layout: GridBagLayout obj = new GridBagLayout ();
To apply some constraints on the components, we should first create an object to
GridBagConstraints class, as: GridBagConstraints cons = new GridBagConstraints ();
GridBagConstraints.gridx, GridBagConstraints.gridy represents the row and column
positions of the component at upper left corner.
GridBagConstraints.gridwidth, GridBagConstraints.gridheight specifies number of columns
and rows in the components display area.
GridBagConstraints.ipadx, GridBagConstraints.ipady are useful to leave space horizontally
and vertically with in the component.
GridBagConstraints.HORIZONTAL makes the component wide enough to fill its display area
horizontally, but do not change its height.
GridBagConstraints.VERTICAL makes the component tall enough to fill its display area
vertically, but do not change its width.
GridBagConstraints.Both makes the component fill its display area entirely.
GridBagConstraints.insets is useful to leave some space around the component at the four
edges of component. This space is left around the component and the boundary of its
display area. insets is the object of Insets class, so it is created as:
Insets insets = new Insets (5, 10, 15, 20);
Ex: Write a program to create push buttons and arrange them using grid bag layout. //GridBag
layout example
import javax.swing.*;
import java.awt.*;
class Example5 extends JFrameWINGS
{
Container c;
GridBagLayout gbag;
GridBagConstraints cons;
Example5 ()
{
c = getContentPane ();
gbag = new GridBagLayout ();
c.setLayout (gbag);
cons = new GridBagConstraints();
JButton b1 = new JButton ("First");
JButton b2 = new JButton ("Second");
JButton b3 = new JButton ("Third");
JButton b4 = new JButton ("Fourth");
//for all buttons use horizontal filling
cons.fill = GridBagConstraints.HORIZONTAL;
cons.gridx=0;
cons.gridy = 0;
gbag.setConstraints (b1, cons);
c.add (b1);
cons.gridx=1;
cons.gridy =0;
gbag.setConstraints (b2, cons);
22
c.add (b2);
cons.gridx=2;
cons.gridy = 0;
gbag.setConstraints (b3, cons);
c.add (b3);
cons.gridx=0;
cons.gridy = 1;
//add 100px height wise
cons.ipady = 100;
//let button3 occupy 3 columns width-wise
cons.gridwidth = 3;
gbag.setConstraints (b4, cons);
c.add (b4);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
Example5 ob = new Example5 ();
ob.setTitle ("GridBag Layout...");
ob.setSize (600, 250);
ob.setVisible (true);
}
}
Output:
23