AWT came out with JDK 1.0 and is the oldest GUI scheme. All the Java schemes have components, layout managers and event handling. Components are things like windows, buttons, menus. Layout managers make it easy to layout the components and event handling provide facilities to respond to mouse clicks like clicking a button or an area in the window.
Ex: awt1.java
import java.awt.*;
public class awt1 extends Frame
{
awt1()
{
Button buttonObj = new Button("click me");
buttonObj.setBounds(30,100,80,30);// setting button position
add(buttonObj);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[])
{
awt1 frameObj =new awt1();
}
}
Output:
Notice that the window does not exit normally using the "Close" on the top left hand side menu or the right top "X" . Let's add some code to fix this.
Ex: "awt1a.java"
import java.awt.* ;
import java.awt.event.* ;
public class awt1a extends Frame
{
awt1a()
{
Button buttonObj = new Button("click me");
buttonObj.setBounds(30,100,80,30);// setting button position
add(buttonObj);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
} }
) ;
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[])
{
awt1a frameObj =new awt1a();
}
}
Here we are using an anonymous class. Since we are already extending the "Frame" class we cannot extend the "WindowAdapter" class. Recall that an anonymous class can be created for an interface or a base class.
Ex: "an1.java"
interface i1
{
public void method1() ;
}
abstract class abs
{
public abstract void method2() ;
}
public class an1
{
public static void main(String args[])
{
i1 i1Object = new i1(){
public void method1() {
System.out.println( "i1: method1." ) ;
} } ;
i1Object.method1() ;
abs absObject = new abs(){
public void method2() {
System.out.println( "abs: method2." ) ;
}
};
absObject.method2() ;
} //main
}
Output:
C:\Java\GUI\awt>java an1
i1: method1.
abs: method2.
We could have chosen to create a separate class for the "WindowAdapter" .
Fie: "awt1b.java"
import java.awt.* ;
import java.awt.event.* ;
class myListener extends WindowAdapter
{
Frame frameobj ;
myListener( Frame frameobjP )
{
frameobj = frameobjP ;
}
public void windowClosing(WindowEvent event)
{
frameobj.dispose();
}
}
public class awt1b extends Frame
{
awt1b()
{
Button buttonObj = new Button("click me");
buttonObj.setBounds(30,100,80,30);// setting button position
add(buttonObj);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
myListener myListenerObj =
new myListener( this ) ;
addWindowListener( myListenerObj ) ;
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[])
{
awt1b frameObj =new awt1b();
}
}
The "WindowAdapter" is an abstract class that implements "WindowListener" .
https://docs.oracle.com/javase/7/docs/api/index.html?java/awt/event/WindowListener.html
The "WindowListener" interface has many methods. The "WindowAdapter" provides empty method implementations of those methods and is easier to use as we only need to implement the method that we are interested in. Rewriting the above example using "WindowListener" produces the following code"
import java.awt.* ;
import java.awt.event.* ;
public class awt1c extends Frame implements WindowListener
{
awt1c()
{
Button buttonObj = new Button("click me");
buttonObj.setBounds(30,100,80,30);// setting button position
add(buttonObj);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
addWindowListener( this ) ;
setVisible(true);//now frame will be visible, by default not visible
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
dispose() ;
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent arg0) {}
public static void main(String args[])
{
awt1c frameObj =new awt1c();
}
}
As we can see there are different ways to implement certain functionality. We created an example with a window containing a single button. Next we shall do something when the button is clicked. Similar to "WindowListener" class we have "ActionListener" class.
File: "event1.java"
import java.awt.*;
import java.awt.event.*;
public class event1 extends Frame implements ActionListener
{
event1()
{
Button buttonObj =new Button("click me");
buttonObj.setBounds(30,100,80,30);// setting button position
buttonObj.addActionListener( this ) ;
add(buttonObj);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public void actionPerformed(ActionEvent event)
{
//code that reacts to the action...
System.out.println( "Button clicked." ) ;
}
public static void main(String args[])
{
event1 frameObj =new event1();
}
}
Our main class implements an interface called "ActionListener" that is responsible for receiving action events. We add our class to the button with the method "addActionListener" . Our function that get called is "actionPerformed" . What if we had 2 buttons
and wanted to write separate code when the buttons got called.
Exercise1 :
Add another button to the above code. Label the first button as "Button1" and second button as "Button2" . Write a different message to the console depending on which button is clicked.
Solution1:
File: "event2.java"
import java.awt.*;
import java.awt.event.*;
public class event2 extends Frame implements ActionListener
{
Button buttonObj1 = null ;
Button buttonObj2 = null ;
event2()
{
buttonObj1 =new Button("Button1") ;
buttonObj1.setBounds(30,100,80,30);// setting button position
buttonObj1.addActionListener( this ) ;
buttonObj2 =new Button("Button2") ;
buttonObj2.setBounds(100,100,80,30);// setting button position
buttonObj2.addActionListener( this ) ;
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dispose();
}
}
) ;
add(buttonObj1);//adding button into frame
add(buttonObj2);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public void actionPerformed(ActionEvent event)
{
//code that reacts to the action...
if( event.getSource() == buttonObj1 )
System.out.println( "Button 1 clicked." ) ;
if( event.getSource() == buttonObj2 )
System.out.println( "Button 2 clicked." ) ;
}
public static void main(String args[])
{
event2 frameObj =new event2();
}
}
All the GUI schemes provide layout managers that facilitate laying out the controls. We can select the layout manager that works for our specific application. It is also possible to combine different layout managers for a single Window and this is often used in complex GUI applications.
The border layout splits our screen into 5 sections and we can place a component in any of those sections.
BorderLayout
File: "layout1.java"
import java.awt.* ;
import java.awt.event.*;
public class layout1 extends Frame
{
public void doBorderLayout()
{
setVisible(true);
BorderLayout bObj = new BorderLayout();
setTitle("Border Layout");
setLayout ( bObj ) ;
setSize(300, 300);
add(new Button("North"), BorderLayout.NORTH);
add(new Button("South"), BorderLayout.SOUTH);
add(new Button("East"), BorderLayout.EAST);
add(new Button("West"), BorderLayout.WEST);
add(new Button("Center"), BorderLayout.CENTER);
}
public layout1()
{
super("Layout Example") ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
doBorderLayout() ;
}
public static void main(String[] args)
{
layout1 obj1 = new layout1() ;
}
}
Output:
FlowLayout
FlowLayout lays out the components from left to right if there is space other wise the components are laid out on the next row.
File: "layout2.java"
import java.awt.* ;
import java.awt.event.*;
public class layout2 extends Frame
{
public void doFlowLayout()
{
setVisible(true);
FlowLayout flowObj = new FlowLayout();
setTitle("Flow Layout");
setLayout ( flowObj ) ;
setSize(300, 300);
add( new Button("1") );
add( new Button("2") );
add( new Button("3") );
add( new Button("4") );
add( new Button("5") );
}
public layout2()
{
super("Layout Example") ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
doFlowLayout() ;
}
public static void main(String[] args)
{
layout2 obj1 = new layout2() ;
}
}
Output:
GridLayout
The "GridLayout" lets us frame the window into a row by column grid and place the component in the cells. We can keep adding a component and the layout will keep placing the component in the next cell. We can skip a cell by placing an empty component in it.
import java.awt.* ;
import java.awt.event.*;
public class layout4 extends Frame
{
public void doGridLayout()
{
setVisible(true);
GridLayout gridLayoutObj = new GridLayout( 2 , 2 ) ;
setTitle("GridLayout Layout");
setLayout ( gridLayoutObj ) ;
setSize(300, 300);
add( new Button("1") ) ;
add( new Button("2") ) ;
add( new Label( "" ) ) ;
add( new Button("3") ) ;
}
public layout4()
{
super("Layout Example") ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
doGridLayout() ;
}
public static void main(String[] args)
{
layout4 obj1 = new layout4() ;
}
}
Output:
GridBagLayout
The GridBagLayout is similar to GridLayout except that a control can span over multiple cells.
File: "layout3.java"
import java.awt.* ;
import java.awt.event.*;
public class layout3 extends Frame
{
public void doGridbagLayoutLayout()
{
setVisible(true);
GridBagLayout gridbagLayoutObj = new GridBagLayout();
setTitle("GridbagLayout Layout");
setLayout ( gridbagLayoutObj ) ;
setSize(300, 300);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0 ;
gbc.gridy = 0 ;
add( new Button("1") , gbc ) ;
gbc.gridx = 1 ;
gbc.gridy = 0 ;
add( new Button("2") , gbc ) ;
gbc.gridx = 0 ;
gbc.gridy = 1 ;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2 ;
add( new Button("3") , gbc ) ;
}
public layout3()
{
super("Layout Example") ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
doGridbagLayoutLayout() ;
}
public static void main(String[] args)
{
layout3 obj1 = new layout3() ;
}
}
Output: