Sunteți pe pagina 1din 27

Applet AWT

By
Prof.Manikandan
QMC, Chennai.
manisankar27@gmail.com
1

Out line: AWT Widgets


Graphical components
- Label
-

TextComponent
Button
CheckBox
Choice
List
Container

Graphics
AWT
AWT - components for GUIs

AWT
AWT are used to make up a Graphical User Interface
(GUI)
The java.awt package provides a set of classes.
These classes are platform independent.
We construct a GUI by placing components within a
container.

- Normally, there are only two types of containers used:


Frame - creates a complete window
Panel - a container and a component
- Several Panels may be within a Frame, and within each Panel
may be several components.
4

The Component Class


The Component class is an abstract class.
Component

Container

Panel

Applet

TextComponent

Window

Frame

TextField

Label

Button

TextArea

Dialog
5

The Component Class


void paint (Graphics g)
- this method causes something to be painted in the component.
Example:
g.setFont(new Font("Arial",Font.BOLD,35));
g.setColor(Color.BLUE);

import java.applet.*;
import java.awt.*;

/*<applet code="font" width=300 height=100>


</applet>*/

public class RMKCOLORS extends Applet


{

public void paint(Graphics g)


{

g.setFont(new Font("Arial",Font.BOLD,35));
g.setColor(Color.BLUE);

g.drawString("COLORS AND FONT PROGRAM", 100, 100);


setBackground(Color.darkGray);

g.setFont(new Font("Times New Roman",Font.ITALIC,30));


g.setColor(Color.RED);

g.drawString("DEVELOPED BY II BCA", 100, 120);


}
}

Labels
A label is a component that will display a line of readonly text on the screen.
It has three constructors:
- Label()

center alignment and no text

- Label( String text)


a center aligned Label object displaying the text given
- Label( String text, int alignment)
Label with the given text and alignment specified as either
Label. LEFT, Label. CENTER, and Label. RIGHT

After you have a Label object, you can use methods


defined in the Label class to get and set various
attributes.

Label Example
Example:
import java. awt.*;
public class LabelExample extends java. applet. Applet{
row number
public void init() {
column number
setLayout( new GridLayout( 1, 1));
add( new Label( left, Label. LEFT));
add( new Label( center, Label. CENTER));
add( new Label( right, Label. RIGHT));
}//end of init
}//end of LabelExample
9

Text Component Class


There are two Types: TextField and TextArea that are
very similar to each other.
so the data and methods that are common to both
were removed and placed into the TextComponent
class.
So TextField and TextArea are both subclasses of Text
Component
There are few methods that are inherited by
TextField and TextArea
- String getText()
returns a copy of the current contents of the component
- void setText( String text)
10
sets the current text to what is specified in by the argument

TextField Class
Text fields are a box into which a single line of text
may be placed.
Text fields are different from labels in that they can be
edited.

11

TextArea Class
Text areas are like text fields except they can handle
larger amounts of text.
Text areas can be given any width and height and
have scroll bars by default.

12

TextArea Class
There are five constructors for a TextArea object:
- TextArea()
creates an empty TextArea object

- TextArea( int rows, int columns)


creates an empty text area with the given number of rows
andcolumns( chars)
- TextArea( String text)
creates a default sized text area containing the argument
string
- TextArea( String text, int rows, int columns)
creates a text area displaying the given string with the 13
size
specified

TextArea Class
-

TextArea( String text, int rows, int columns, int scrollbars)


-

There are 4 constant arguments that can be used:


TextArea. SCROLLBARS_ BOTH
TextArea. SCROLLBARS_ NONE
TextArea. SCROLLBARS_ HORIZONTAL_ ONLY
TextArea. SCROLLBARS_ VERTICAL_ ONLY
The default is for both horizontal and vertical scrollbars
to be displayed.

14

TextArea Class Example


An example:
import java. awt.*;
public class TextAreaExample extends java. applet. Applet {
public void init() {
String str = This is an example for a TextArea that \n+
spans multiple lines. \n;
add( new TextArea( str));
}//end of init
}//end of TextAreaExample

15

Button Class
Buttons are a simple UI component that triggers some
action
There are two constructors for a Button object:
- Button()
creates an empty button with no label
- Button( String label)
creates a button with the given string as a label

After creating a Button object, you can get the value of


the Buttons label by using the getLabel() method
which returns a String.
Using the setLabel() method, you can set the label of
the button by passing in a String value.
16

Button Class
An Example:
import java. awt.*;
public class ButtonExample extends java. applet. Applet {
public void init() {
add( new Button( Rewind));
add( new Button( Fast Forward));
add( new Button( Play));
add( new Button( Stop));
add( new Button( Pause));
}//end of init
}//end of ButtonExample
17

Checkbox Class
Check boxes are UI components that have two states: on
and off (or checked and unchecked, or true and false)
used to indicate optional features of some other action.
Check boxes can be used in two ways:

- Nonexclusive
given a series of checkboxes, any of them can be selected;
can be checked or unchecked independently of each other
- Exclusive
given a series of checkboxes, only one can be selected at any one
time, also known as radio buttons or checkboxgroups

18

Checkbox Class
Jan. 2004

There are five constructors:

- Checkbox()
creates a Checkbox with no label and false
- Checkbox( String label)
creates a Checkbox with the given label and initialized to
false
- Checkbox( String label, boolean state)
creates a Checkbox with the given label and state
- Checkbox( String label, boolean state, CheckboxGroup group)
- Checkbox( String label, CheckboxGroup group, boolean state)
The last two create a Checkbox with the label and state
specified as well as specifying a group that this Checkbox will belong
to.
19

Checkbox Class
An Example:
import java. awt.*;
public class CheckboxExample extends java. applet. Applet {
public void init() {
setLayout( new FlowLayout( FlowLayout. LEFT));
add( new Checkbox( Red));
add( new Checkbox( Orange));
add( new Checkbox( Yellow));
add( new Checkbox( Green));
add( new Checkbox( Blue));
add( new Checkbox( Indigo));
add( new Checkbox( Violet));
}//end of init
}//end of CheckboxExample

20

CheckBoxGroups
A CheckboxGroup is a collection of Checkboxes in
which
only one of them can be selected at one time; - also
known as radio buttons
To create a series of radio buttons, first create an
instance of CheckboxGroup
- ChecboxGroup cbg = new CheckboxGroup();

Then create and add the radio buttons into the group
- use either of the following two methods:

Checkbox( String label, boolean state, CheckboxGroup group)


Checkbox( String label, CheckboxGroup group, boolean state)
21

CheckBoxGroups
An Example:
{

import java. awt.*;


public class CheckboxGroupExample extends java. applet. Applet
public void init() {
setLayout( new FlowLayout( FlowLayout. LEFT));
CheckboxGroup cbg = new CheckboxGroup();
add( new Checkbox( One, false, cbg));
add( new Checkbox( Two, false, cbg));
add( new Checkbox( Three, true, cbg));
add( new Checkbox( Four, false, cbg));
}//end of init
}//end of CheckboxGroupExample
22

CheckBoxGroups
Since CheckboxGroups only allow one button to be
selected at one time, the last Checkbox to be added to
the group with the state being true will be the one
selected by default.
So in the previous example, radio button with the label
three would be selected.

23

Choice Class
Choice menus are more complex than labels, buttons,
or checkboxes.
Choice menus are pop-up or pull-down lists in which
an item can be selected.
To create a Choice menu, we first need an instance of
the Choice class and then add items to it.
The items added are enumerated from top to bottom
starting with the index 0.
One of the methods in this class to make note of is:
- String getSelectedItem()
returns the text of the current selection

24

Choice Class
An Example:

import java. awt.*;


public class ChoiceExample extends java. applet. Applet {
public void init() {
Choice c = new Choice();
c. add( Red);
c. add( Orange);
c. add( Yellow);
c. add( Green);
c. add( Blue);
c. add( Indigo);
c. add( Violet);
add( c);
}//end of init
}//end of ChoiceExample

25

List Class
A List is functionally similar to a Choice menu in that
it lets you choose from a list, but Lists do not pop-up,
they are permanently displayed on the screen.
You can choose more than one item on the List if that
capability is enabled.
List items are enumerated from top to bottom starting
at index 0 just like a Choice menu.

26

List Class
There are three constructors for a List object:

- List()
creates a default sized List object in single selection mode
- List( int rows)
creates a List object in single selection mode with the
number of rows given
- List( int rows, boolean multipleselection)
creates a List object with the specified number of rows.
The List is in multiple selection mode if the boolean argument is
true and single selection otherwise.

27

S-ar putea să vă placă și