Gui Programming With Java
Gui Programming With Java
• The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, Checkbox, Choice, List etc.
AWT Hierarchy
• Component:
• Container:
The Container is a component in AWT that can contain another components like
buttons, textfields, labels etc. The Container class extends Frame and Panel.
• Frame:
The Frame is the container that contain title bar and can have menu bars. It can
have other components like button, textfield etc.
• Panel:
The Panel is the container that doesn't contain title bar and menu bars. It can
have other components like button, textfield etc.
Method Description
setSize(int width,int height) sets the size (width and height) of the component.
To create simple awt example, you need a frame. There are two ways to create a
frame in AWT.
• By extending Frame class (inheritance)
Ex:
class Example extends Frame
{
……..
}
• By creating the object of Frame class (association)
Ex:
class Example
{
Frame obj=new Frame();
……..
}
SimpleExample.java
import java.awt.*;
public class AwtExample
{
public static void main(String[] args)
{
Frame f=new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("Simple Example");
}
}
Output:
Javac AwtExample.java
Java AwtExample
• Button:
The button class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed.
Syntax:
Button b=new Button(“Text");
(Or)
Button b1,b2;
b1=new Button(“Text”);
b.setBounds(50,100,80,30);
setBounds(int x,int y,int width,int height)
This method is used to declare location, width & height of all components of
AWT. X Y
Example: setBounds(50,100,80,30);
width Height
The Label class is a component for placing text in a container. It is used to display
a single line of read only text. The text can be changed by an application but a user
cannot edit it directly.
Syntax:
Label l1=new Label(“Text”);
(or)
Label l1,l2;
l1=new Label(“Text”);
TextField:
The TextField class is a text component that allows the editing of a single line
text.
Syntax:
TextField t1=new TextField(“Text”);
(or)
TextField t1,t2;
t1=new TextField(“Text”);
• TextArea :
The TextArea class is a multi line region that displays text. It allows the editing of
multiple line text.
Syntax:
TextArea t1=new TextArea(“Text”);
(or)
TextArea t1,t2;
t1=new TextArea(“Text”);
• Checkbox :
The Checkbox class is used to create a checkbox. It is used to turn an option on
(true) or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from
"off" to "on".
Syntax:
Checkbox c1=new Checkbox(“Text”);
(or)
Checkbox c1,c2;
c1=new Checkbox(“Text”);
4 GUI Programming in Java |Madhu T.
• Choice :
The Choice class is used to show popup menu of choices. Choice selected by user
is shown on the top of a menu.
Syntax:
Choice c=new Choice();
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
• List :
The List class represents a list of text items. By the help of list, user can choose
either one item or multiple items.
Syntax:
List ls=new List(Size);
ls.add("Item 1");
ls.add("Item 2");
ls.add("Item 3");
Event describes the change in state of component. Events are generated as result
of user interaction with the graphical user interface components. For example, clicking
on a button, moving the mouse, entering a character through keyboard and selecting an
item from list.
Def: Event Handling is the mechanism that controls the event and decides what should
happen if an event occurs.
This mechanism have the code which is known as event handler that is executed
when an event occurs.
The java.awt.event package provides many event classes and Listener interfaces for
event handling.
By using addActionListener(ActionListener a)
Example:
Button b=new Button(“Submit”);
b.setBounds(100,50,80,30);
b.addActionListener(this);