Вы находитесь на странице: 1из 32

Abstract Windowing Toolkit

Procedure for Creating GUI

Create an element. For instance, Check box


or list box or label etc...
Determine its initial appearance
Decide whether it should occupy a specific
position or any default position
Add it to the screen interface

AWT ( Abstract Windowing Toolkit)

Set of Java Classes that allow us to create


a Graphical User Interface (GUI)
Accept user input through the key board
and the mouse.
AWT Provides various items which enable
creation of an attractive and efficient GUI -

Containers
Components
Layout Managers
Graphics and drawing capabilities
Fonts
Events

Container

Container is an area that can hold elements,


can be drawn and painted.
There is a class named Container in java.awt
package, which directly or indirectly derives
two commonly used containers Frames and
Panels.
Frames and Panels are by far the most
commonly used containers.
Frames are separate window...Panels are
areas contained within a window....

Frame

It can either act as a container or as a


component.
Created using one of the following
constructors

Frame() ---> Creates a Frame which is invisible


Frame(String title)
Creates a Frame given title which is invisible

import java.awt.*;
class FrameDemo extends Frame
{
FrameDemo(String title)
{
super(title);
}
public static void main(String args[])
{
FrameDemo f = new FrameDemo ("I Have been
Framed !!!");
f.setSize(500,500);
f.setVisible(true);
}
}

Panel

Panels are containers used to group a


number of components together
The simplest of creating a panel is through
its constructor

Panel()

import java.awt.*;
class Paneltest extends Panel
{
Paneltest()
{
}

public static void main(String args[])


{
Paneltest p=new Paneltest();
Frame f=new Frame("Testing a Panel");
f.add(p);
f.setSize(500,500);
f.setVisible(true);
}
}

Component

Anything that can be placed on a


userinterface and can be made visible or
resized
Commonly used examples of components
are textfields, labels, checkboxes, textareas
etc....

Label

Label class in java used to create a text label


on the screen.
Labels can be created using one of the
following constructors-

Label() : Creates an empty label


Label(String labeltext) : Creates label with a given
text
Label(String labeltext,int alignment): Creates a
label with given alignment where alignment can be
Label.LEFT, Label.RIGHT or Label.CENTER

Commonly used methods of a


Label
Method
Purpose
setText(String s) Set the text of the label
getText()
Retrieve the text of a label

TextField

TextField() : Creates a new textfield


TextField(int columns) : Creates a new
textfield with given number of columns
TextField(String s) : Creates a new textfield
with given string
TextField(String s,int columns) : Creates a
new textfield with given string and given
number of columns.

import java.awt.*;
class TextLabel extends Frame {
TextField txtName = new TextField(20);
Label lblname = new Label("Name: ");
public TextLabel (String title)
{
super(title);
setLayout(new FlowLayout());
add(lblname);
add(txtName);
}
public static void main(String args[])
{
TextLabel t = new TextLabel("Testing Components");
t.setSize(500,500);
t.setVisible(true);
}
}

Text Area

TextArea() Creates a new TextArea


TextArea(int rows, int cols) : Creates a new
TextArea with given number of rows and
columns

import java.awt.*;
class TextComments extends Frame {
TextArea txtComment=new TextArea();
Label lbl = new Label("Comments: ");

public TextComments (String title)


{
super(title);
setLayout(new FlowLayout());
add(lbl);
add(txtComment);
}
public static void main(String args[])
{
TextComments t = new TextComments("Testing
Components");
t.setSize(500,500);
t.setVisible(true);
}
}

Buttons

Button()
Button(String text)
SetLabel() & getLabel()

import java.awt.*;
class Buttontest extends Frame {
Button btnHai = new Button("Hai");
Button btnHallo = new Button("Hallo");
Button btnHowru = new Button("How R U");

public Buttontest (String title)


{
super(title);
setLayout(new FlowLayout());
add(btnHai);
add(btnHallo);
add(btnHowru);
}
public static void main(String args[])
{
Buttontest b = new Buttontest("Testing Components");
b.setSize(500,500);
b.setVisible(true);
}
}

Checkboxes & RadioButtons

Checkbox() ---> Creates an empty check


box
Checkbox(String text) ---> Creates a
checkbox with given string as label

CheckboxGroup cg = new CheckboxGroup();


Checkbox male = new
Checkbox(male,cg,true);
Checkbox female = new
Checkbox(female,cg,false);

SetState() && getState()

import java.awt.*;
class Hobbies extends Frame {
Checkbox c1=new Checkbox("Reading",false);
Checkbox c2=new Checkbox("Music",false);
Checkbox c3=new Checkbox("Painting",false);
Checkbox c4=new Checkbox("Movies",false);
Checkbox c5=new Checkbox("Dancing",false);
Label lbl = new Label("What's Your hobby??");
public Hobbies (String s)
{
super(s);
setLayout(new FlowLayout());
add(lbl);
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
}

public static void main(String args[])


{
Hobbies h = new Hobbies("A basket full of checkboxes");
h.setSize(500,500);
h.setVisible(true);
}
}

import java.awt.*;
class Qualification extends Frame {
CheckboxGroup cg=new CheckboxGroup();
Checkbox r1=new
Checkbox("Undergraduate",cg,false);
Checkbox r2=new Checkbox("Graduate",cg,false);
Checkbox r3=new Checkbox("Post Graduate",cg,false);
Checkbox r4=new Checkbox("Doctorate",cg,false);

Label lbl = new Label("What's Your hobby??");


public Qualification (String s)
{
super(s);
setLayout(new FlowLayout());
add(lbl);
add(r1);
add(r2);
add(r3);
add(r4);

public static void main(String args[])


{
Qualification q = new
Qualification("Literacy");
q.setSize(500,500);
q.setVisible(true);
}
}

Creating Lists

Choice moviestars=new Choice()


moviestars.addItem(MohanLal)
moviestars.addItem(Mammootty)

import java.awt.*;
class Stars extends Frame {
Choice moviestars=new Choice();
Label lbl = new Label("Who is your favourite movie
star??");
public Stars(String s)
{
super(s);
setLayout(new FlowLayout());
moviestars.addItem("Mohanlal");
moviestars.addItem("Mammootty");
moviestars.addItem("Nayanthara");
moviestars.addItem("KavyaMadhavan");
moviestars.addItem("KavyaMadhavan");
moviestars.addItem("AmeerKhan");
moviestars.addItem("Sharookh");
add(lbl);
add(moviestars);
}

public static void main(String args[])


{
Stars s = new Stars("A sky full of stars");
s.setSize(500,500);
s.setVisible(true);
}
}

Вам также может понравиться