Unit Iv
Unit Iv
AWT CONTROLS:
Java AWT (Abstract Window Toolkit) is an API to develop Graphical
User Interface (GUI) or windows-based applications in Java.
Java AWT is an API that contains large number of classes and
methods to create and manage graphical user interface ( GUI )
applications. .
Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system. AWT is heavy
weight i.e. its components are using the resources of underlying
operating system (OS).
The AWT class hierarchy:
The hierarchy of Java AWT classes are given below.
Component class:
Panel:
Window class:
Window class creates a top level window. Window does not have
borders and menubar.
Frame:
import java.awt.*;
package testawt;
import java.awt.*;
import java.awt.event.*;
public class Testawt extends Frame
{
public Testawt()
{
Button btn=new Button("Hello World");
add(btn); //adding a new Button.
setSize(400, 500); //setting size.
setTitle("StudyTonight"); //setting title.
setLayout(new FlowLayout()); //set default layout for frame.
setVisible(true); //set frame visibilty true.
}
public static void main (String[] args)
{
Testawt ta = new Testawt(); //creating a frame.
}}
Points to Remember:
AWT Label
Label Declaration:
Example:
import java.awt.*;
class LabelDemo1
{
public static void main(String args[])
{
Frame l_Frame= new Frame("studytonight ==> Label
Demo");
Label lab1,lab2;
lab1=new Label("Welcome to studytonight.com");
lab1.setBounds(50,50,200,30);
lab2=new Label("This Tutorial is of Java");
lab2.setBounds(50,100,200,30);
l_Frame.add(lab1);
l_Frame.add(lab2);
l_Frame.setSize(500,500);
l_Frame.setLayout(null);
l_Frame.setVisible(true);
}
}
AWT Button
Text Components
AWT TextField
TextField Declaration:
Example:
We are creating two textfields to display single line text string. This
text is editable in nature, see the below example.
import java.awt.*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("studytonight
==>TextField");
TextField text1,text2;
text1=new TextField("Welcome to studytonight");
text1.setBounds(60,100, 230,40);
text2=new TextField("This tutorial is of Java");
text2.setBounds(60,150, 230,40);
TextF_f.add(text1);
TextF_f.add(text2);
TextF_f.setSize(500,500);
TextF_f.setLayout(null);
TextF_f.setVisible(true);
}
}
AWT TextArea
TextArea Declaration:
Example:
import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to
studytonight.com");
area.setBounds(30,40, 200,200);
textArea_f.add(area);
textArea_f.setSize(300,300);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
} }
Checkbox Syntax
public class Checkbox extends Component implements
ItemSelectable, Accessible
Example:
import java.awt.*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("studytonight
==>Checkbox Example");
Checkbox ckbox1 = new Checkbox("Yes", true);
ckbox1.setBounds(100,100, 60,60);
Checkbox ckbox2 = new Checkbox("No");
ckbox2.setBounds(100,150, 60,60);
checkB_f.add(ckbox1);
checkB_f.add(ckbox2);
checkB_f.setSize(400,400);
checkB_f.setLayout(null);
checkB_f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxDemo1();
} }
Check Box Group
AWT CheckboxGroup
CheckboxGroup Declaration:
Example:
import java.awt.*;
public class CheckboxGroupDemo
{
CheckboxGroupDemo(){
Frame ck_groupf= new Frame("studytonight
==>CheckboxGroup");
CheckboxGroupobj = new CheckboxGroup();
Checkbox ckBox1 = new Checkbox("Yes", obj, true);
ckBox1.setBounds(100,100, 50,50);
Checkbox ckBox2 = new Checkbox("No", obj, false);
ckBox2.setBounds(100,150, 50,50);
ck_groupf.add(ckBox1);
ck_groupf.add(ckBox2);
ck_groupf.setSize(400,400);
ck_groupf.setLayout(null);
ck_groupf.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupDemo();
} }
Choice
AWT Choice
Example:
import java.awt.*;
public class ChoiceDemo
{
ChoiceDemo()
{
Frame choice_f= new Frame();
Choice obj=new Choice();
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
choice_f.add(obj);
choice_f.setSize(400,400);
choice_f.setLayout(null);
choice_f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceDemo();
}}
List Box
In Java, AWT contains a List Class. It is used to represent a list of
items together. One or more than one item can be selected from the
list.
List Declaration:
Example:
import java.awt.*;
ListDemo()
{
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
list_f.add(obj);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
new ListDemo();
} }
Example:
import java.awt.*;
public class PanelDemo1{
PanelDemo1()
{
Frame panel_f= new Frame("studytonight ==> Panel Demo");
Panel panel11=new Panel();
panel11.setBounds(40,80,200,200);
panel11.setBackground(Color.red);
Button box1=new Button("On");
box1.setBounds(50,100,80,30);
box1.setBackground(Color.gray);
Button box2=new Button("Off");
box2.setBounds(100,100,80,30);
box2.setBackground(Color.gray);
panel11.add(box1);
panel11.add(box2);
panel_f.add(panel11);
panel_f.setSize(400,400);
panel_f.setLayout(null);
panel_f.setVisible(true);
}
public static void main(String args[])
{
new PanelDemo1();
} }
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.ScrollPane
syntax:
scroller = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
Menu: The object of MenuItem class adds a simple labeled menu
item on menu. The items used in a menu must belong to the
MenuItem or any of its subclass.
The object of Menu class is a pull down menu component which is
displayed on the menu bar. It inherits the MenuItem class.
Menu class declaration
public class Menu extends MenuItem implements MenuContainer,
Accessible .
Example:
import java.awt.*;
class MenuDemo1
{
MenuDemo1()
{
Frame menu_f= new Frame("studytonight ==> Menu and
MenuItem Demo");
MenuBarmenu_bar=new MenuBar();
Menu menu11=new Menu("Menu");
Menu sub_menu1=new Menu("Sub Menu =>");
MenuItem a1=new MenuItem("Red");
MenuItem a2=new MenuItem("Light Red");
MenuItem a3=new MenuItem("Drak Red");
MenuItem b1=new MenuItem("Green");
MenuItem b2=new MenuItem("Light Green");
MenuItem b3=new MenuItem("Dark Green");
menu11.add(a1);
sub_menu1.add(a2);
sub_menu1.add(a3);
menu11.add(b1);
sub_menu1.add(b2);
sub_menu1.add(b3);
menu11.add(sub_menu1);
menu_bar.add(menu11);
menu_f.setMenuBar(menu_bar);
menu_f.setSize(400,400);
menu_f.setLayout(null);
menu_f.setVisible(true);
}
public static void main(String args[])
{
new MenuDemo1();
} }
// class constructor
ScrollbarExample1() {
// creating a frame
f.add(s);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
new ScrollbarExample1();
} }
Output:
Working with Frame class, Color, Fonts and layout managers:
The class Frame is a top level window with border and title. It uses
BorderLayout as default layout manager.
Class declaration
Following is the declaration for java.awt.Frame class:
public class Frame
extends Window
implements MenuContainer
Java Font
In Java, Font is a class that belongs to the java.awt package. It
implements the Serializable interface. FontUIResource is the direct
known subclass of the Java Font class.
Types of Fonts in Java
There are two types of fonts in Java:
o Physical Fonts
o Logical Fonts
Physical Fonts
Physical fonts are actual Java font library. It contains tables that
maps character sequence to glyph sequences by using the font
technology such as TrueType Fonts (TTF) and PostScript Type 1
Font.
The property of the physical font is that it uses the limited set of
writing systems such as Latin characters or
only Japanese and Basic Latin characters.user can bundle and
instantiate that font by using the createFont() method of the Java
Font class.
Logical Fonts
Java defines five logical font families that are Serif, SansSerif,
Monospaced, Dialog, and DialogInput. It must be supported by the
JRE. Note that JRE maps the logical font names to physical font
because these are not the actual font libraries. For example, AWT
components such as Label and TextField uses logical fonts only.
The color constants in Java are values that cannot be changed and
can be used with different Java programs.
The following table shows the color constants available in the Java
programming. The all-capital version depicts a constant value.
There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
EVENT HANDLING
Events: An event can be defined as changing the state of an object
or behavior by performing actions.
Actions can be a button click, cursor movement, keypress through
keyboard or page scrolling, etc.
The java.awt.event package can be used to provide various event
classes.
Types of Events:
The events can be broadly classified into two categories:
Foreground Events - Those events which require the direct
interaction of user.They are generated as consequences of a person
interacting with the graphical components in Graphical User
Interface. For example, clicking on a button, moving the mouse,
entering a character through keyboard,selecting an item from list,
scrolling the page etc.
Background Events - Those events that require the
interaction of end user are known as background events.
Operating system interrupts, hardware or software failure,
timer expires, an operation completion are the example of
background events.
Event Listeners:
Design Goals
Syntax:
addTypeListener()
where Type represents the Type of event.
import java.awt.*;
import java.awt.event.*;
Searching...
Handling Mouse and Keyboard Events:
Events are central to programming for a graphical user
interface. The GUI program must be prepared to respond to
various kinds of events .The most basic kinds of events are
generated by the mouse and keyboard.In Java, events are
represented by objects. Different types of events are
represented by objects belonging to different classes.
For a mouse event, an object belonging to a class called
MouseEvent is constructed.For a keyboard event, an object
belonging to a class called KeyEvent is constructed.After the
event object is constructed, it is passed as a parameter to a
designated subroutine.For a GUI program like Applet,
although there is no main() routine there, there is still a sort
of main routine running somewhere that executes a loop to
response the events. This loop is called an event loop. It’s
part of “the system”.
Example:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
</applet>
*/
int mouseX=0,mouseY=0;
// coordinates of mouse
addMouseListener(this);
addMouseMotionListener(this);
}
// save coordinates
mouseX = 0;
mouseY = 10;
repaint();
// save coordinates
mouseX = 0;
mouseY = 10;
repaint();
}
// save coordinates
mouseX = 0;
mouseY = 10;
repaint();
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
}
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " +
mouseY);
repaint();
// show status
}
}
output:
Adapter classes:
Java adapter classes provide the default implementation of
listener interfaces. If we inherit the adapter class, we will not
be forced to provide the implementation of all the methods
of listener interfaces. So it saves code.The adapter classes are
found in java.awt.event, java.awt.dnd and
javax.swing.event packages.
import java.awt.*;
import java.awt.event.*;
// object of Frame
Frame f;
// class constructor
AdapterExample() {
f.dispose();
} });
f.setLayout (null);
f.setVisible (true);
} // main method
new AdapterExample();
} }
Output:
Java MouseAdapter Example
MouseAdapterExample.java
import java.awt.*;
import java.awt.event.*;
Frame f;
// class constructor
MouseAdapterExample() {
f.setLayout (null);
f.setVisible (true);
// creating the Graphics object and fetching them from the Frame
object using getGraphics() method
Graphics g = f.getGraphics();
g.setColor (Color.BLUE);
} // main method
public static void main(String[] args) {
new MouseAdapterExample();
} }
Output:
Inner classes:
Java inner class or nested class is a class that is declared
inside the class or interface.
Additionally, it can access all the members of the outer class,
including private data members and methods.
//code
class Java_Inner_class{
//code
} }
Advantage of Java inner classes
Types:
Syntax:
class Outer{
//code
class Inner{
//code
} }
Example:
class TestMemberOuter1{
class Inner{
} }
Output:
data is 30
Java anonymous inner class is an inner class without a name and for
which only a single object is created. An anonymous inner class can
be useful when making an instance of an object with certain "extras"
such as overloading methods of a class or interface, without having
to actually subclass a class.
TestAnonymousInner.java
class TestAnonymousInner{
};
p.eat();
} }
Output:nice fruits
A class i.e., created inside a method, is called local inner class in java.
Local Inner Classes are the inner classes that are defined inside a
block. Generally, this block is a method body. Sometimes this block
can be a for loop, or an if clause. Local Inner classes are not a
member of any enclosing classes. They belong to the block they are
defined within, due to which local inner classes cannot have any
access modifiers associated with them. However, they can be
marked as final or abstract. These classes have access to the fields of
the class enclosing it.
LocalInner1.java
public class localInner1{
void display(){
class Local{
void msg(){System.out.println(data);}
l.msg();
obj.display();
} }
Output:30