Ajp Notes Chap1
Ajp Notes Chap1
Ajp Notes Chap1
02 Swing 10
03 Event Handling 12
04 Networking 10
05 Interacting with 12
Database
06 Servlet 14
70
Unit 1
Introduction to Abstract Windowing Toolkit (AWT) (12 Marks)
Unit Outcomes
Develop GUI using awt
Create frame window using different awt component
Arrange GUI components using Layout Manager
Develop program using menu and Dialog Boxes
AWT (Abstract Window Toolkit)
• It is a platform dependent API for creating Graphical User Interface
(GUI) for java
• AWT contains numerous classes and methods that allow you to create
and manage window
• import java.awt.*;
• Java AWT components are platform-dependent i.e. components are
displayed according to the view of operating system.
• AWT is heavyweight i.e. its components uses the resources of system.
Fram
AWT Class Hierarchy
setBackground(Color.BLUE);
}
public static void main(String args[])
{
Simpleframe f=new Simpleframe();
}
}
Component
• Component is an object having a graphical representation that can be
displayed on the screen and that can interact with the user for eg
button,label
• At the top of the AWT hierarchy is the Component class.
• Which object is responsible for remembering the current foreground
and background colors and the currently selected text font? (Answer:
Component)
• It extends: Object Class
• Implements: ImageObserver, MenuContainer, Serializable
Container
• Container class is a subclass of Component.
• Provided additional methods that allow other
Component to place on it.
• Container is responsible for laying out (that is, positioning) any
components that it contains.
• It does this through the use of various layout
managers.
Panel
• Panel is a concrete subclass of Container
• Panel is the immediate superclass of Applet.
• When screen output is directed to an applet,
• it is drawn on the surface of a Panel object.
Window
• Creates top-level window means directly on the desktop.
• The window is the container that have no borders and menu bars.
• Uses Frame, Dialog class which is subclass of Window class for
creating window.
Frame
• It is subclass of Window and has a title bar, menu bar, borders, and
resizing corners.
AWT Controls
• AWT Controls: Component which allows you to interact with
application.
• Labels
• Button
• Checkbox
• Checkbox group
• Scrollbars
• Text field
• Text Area
• About Alighment
• void setAlignment(int how)
• int getAlignment( )
import java.awt.*;
class Labelframe extends Frame
{
Labelframe()
{
Label l1=new Label("Name",Label.LEFT);
l1.setBounds(30,30,80,30);
add(l1);
setSize(300,300);
//frame size 300 width and 300 height
setLayout(null);
setVisible(true);
//now frame will be visible, by default not visible
}
public static void main(String args[])
{
Labelframe f=new Labelframe();
}
}
Label(using Applet)
import java.awt.*;
import java.applet.*;
/*
<applet code="Labelapplet" width=500 height=200>
</applet>*/
public class Labelapplet extends Applet
{
public void init( )
{
setFont(new Font ( "Times new roman" , Font.BOLD, 14));
Label l1 = new Label("aligned left", Label.LEFT);
add(l1);
import java.awt.*;
import java.applet.*;
/*
<applet code="Buttonapplet" width=250 height=150>
</applet>
*/
public class Buttonapplet extends Applet
{
Button b1,b2;
public void init()
{
b1 = new Button("Yes");
b2 = new Button("No");
add(b1);
add(b2);
//getLabel and setLabel
Button b3=new Button();
String str=b2.getLabel();
b3.setLabel(str);
add(b3);
}
}
Program
/*
<applet code="Checkboxapp" width=250 height=200>
</applet>
*/
public class Checkboxapp extends Applet
{
Checkbox c1,c2;
add(c1);
add(c2);
}
}
CheckboxGroup(Radio Button)
It is possible to create a set of mutually exclusive check boxes in which
one and only one check box in the group can be checked at any one
time. These check boxes are often called radio buttons,
Methods
getSelectedCheckbox(),
setSelectedCheckbox().
import java.awt.*;
import java.applet.*;
/*
<applet code="CBGroupapp" width=250 height=200>
</applet>
*/
add(c1);
add(c2);
}
}
program
import java.awt.*;
import java.applet.*;
/*
<applet code="Choiceapp" width=300 height=180>
</applet>
*/
public class Choiceapp extends Applet
{
Choice sub;
add(sub);
}
}
AWT Control: List
•List class provides a compact, multiple-choice, scrolling selection list.
•List object can be constructed to show any number of choices in the
visible window.
•In Choice only one item is shown.
•Constructors
List( )
List(intnumRows)
List(intnumRows, booleanmultipleSelect)
for eg.
List(4,true)
To retrieve item:
String getItem(int index)
•Active Item
void select(int index)
Program-
import java.awt.*;
import java.applet.*;
/*
<applet code="Listapp" width=300 height=180>
</applet>
*/
sub.select(1);
// add lists to window
add(sub);
}
}
Choice List
Program-
// Demonstrate text field.
import java.awt.*;
import java.applet.*;
/*
<applet code="TextFieldapp" width=380 height=150>
</applet>
*/
t1 = new TextField(12);
t2= new TextField(8);
t2.setEchoChar('*');
add(l1);
add(t1);
add(l2);
add(t2);
}
}
import java.awt.*;
import java.applet.*;
/*
<applet code="TextAreaapp" width=300 height=250>
</applet>
*/
add(text);
}
} No of Lines
Scroll Bars
The object of Scrollbar class is used to add horizontal and vertical
scrollbar.
Scrollbar is a GUI component allows us to see invisible number of
rows and columns.
Scrollbar defines the following constructors:
Scrollbar( )
Scrollbar(int style)
Scrollbar(int style, intinitialValue, intthumbSize, int min, int max)
for eg.
Scrollbar(Scrollbar.HORIZONTAL,0, 60, 0, 300);
program-
// Demonstrate scroll bars.
import java.awt.*;
import java.applet.*;
/*
<applet code="Sbar_app" width=300 height=200>
</applet>
*/
add(v);
add(h);
}
}