1 Applet and Basic AWT Lecture

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Using Applet

What is an applet?
 An applet is a java program that is intended to run upon call from within another
application particularly a web browser.

Ex.#1: Applet Program

import java.applet.Applet; // import Applet class


import java.awt.*; // (Abstract Windowing Toolkit)

public class SampleApplet extends Applet


{
String name = new String("Mark Jezekiah");
Label lblgreet = new Label("Hello");
TextField txtgreet = new TextField(" ",20);
Button btnClear = new Button("Clear");

public void init()


{

lblgreet.setForeground(Color.yellow);
txtgreet.setForeground(Color.blue);

lblgreet.setBackground(Color.blue);
txtgreet.setBackground(Color.yellow);

txtgreet.setText(name);
add(lblgreet);
add(txtgreet);
add(btnClear);
}
}

Adding Events in an Applet

 An event occurs when you take action on a component


o Ex. clicking the mouse; entering text
 A component on which event is generated is the source of the event
o Ex. Button that a user clicks; TextField that is use to enter text
 An object that is interested in an event is a listener
o Ex. Applet is an object use to be a listener for an event
 To respond to user events in an applet, do the following:
o Prepare the applet to accept event messages
 import the java.awt.event.*; package
 the package includes event classes such as
ActionEvent, ComponentEvent and TextEvent

 add the phrase implements ActionListener to the class


 ActionListener is an interface, or a set of specifications
for methods that can be use with Event objects.
 ActionListener provides standard event method
specifications that allow working with ActionEvent
which is the type of event that occurs when clicking a
button.

o Tell the applet to expect events to happen


 addActionListener( ) method tells the applet to expect
ActionEvents
 Ex. btnClear.addActionListener(this);

o Tell the applet how to respond to any events happen


 actionPerformed(ActionEvent x) method tells the applet to
execute a response to any event
 ActionListener interface contains the actionPerformed
method specification
 Ex. public void actionPerformed(ActionEvent x)
Using AWT (Abstract Windowing Toolkit)

 AWT is a set of classes that enables you to create a GUI (Graphical User
Interface)
 AWT consists of:
o Components - anything that can be put onto the user interface
o Containers - a component that can contain other components
 import java.awt.*;

Basic AWT Components

 Label
o Single line of text used for labeling components which cannot be edited
o Declaration & Instantiation:
Label name = new Label(String s);
Ex. Label lbl = new Label(“Name”);
o Properties:
 text
 foreground color
 background color
o Constructors:
 Label()- no text value
 Label(String) - with text value
 Label(String, Alignment) – with text value & alignment
 Label.RIGHT
 Label.CENTER
 Label.LEFT
o Methods:
 void setForeground(Color) – set the text color
 void setBackground(Color) – set the background color
 Color.cyan
 Color.magenta
 Color.lightGray
 Color.darkGray
 Color.(Other Basic Colors)
 void setText(String) - set a new text value
 void setFont(Font) -set the font type, format, and size
 Type:
o “Font.Serif”
o “Font.Monospaced”
o “Font.SansSerif”
o “Font.Dialog”
o “Font.DialogInput”
 Style
o Font.Plain
o Font.Bold
o Font.Italic
o Font.Plain+Font.Italic
o Font.Bold+Font.Italic
 Size - 8 to 100

 Font MyFont = new Font("SansSerif",Font.BOLD, 16);


lblMyFont.setFont(MyFont);
 TextField
o Single editable line of text
o Used to input or fill-in data or information
o Uses ActionListener which handles the event of pressing the enter key.
o Declaration and Instantiation:
TextField name = new TextField(size);
Ex. TextField txt= new TextField(20);
o Properties:
 Text
 Width
o Contstructors:
 TextField() – no text
 TextField(int col) – specifies the width
 TextField(String) – with text
 TextField(String, int) - with text and size
o Methods
 void setForeground(Color) – set the text color
 void setBackground(Color) – set the background color
 void setText(String) - set a new text value
 String getText() - returns the text value
 void setEchoChar(char) – masking the text value (password)
 void setEditable(boolean) – set editable to true or false
 void requestFocus( ) – set the focus of the cursor to the textfield
 void setFont(Font) -set the font type, format, and size
 Button
o Used for triggering an action or command
o Uses ActionListener which handles the event of clicking the button
o Declaration and Instantiation:
Button name = new Button(String s);
Ex. Button bt = new Button(“Exit”);
o Properties:
 label
o Contructors:
 Button() - no label
 Button(String) - with label

o Methods:
 void setLabel(String) - set a label value
 String getLabel() - returns the label value
 void setEnabled(boolean) – set to false which makes inaccessible
Ex#1: Applet Program (Adding Events)

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

public class DisplayClear extends Applet implements ActionListener


{ //create a listener for an action
TextField txt1 = new TextField(15);
TextField txt2 = new TextField("Sample Applet");
TextField txt3 = new TextField("Sample Applet size 25", 25);
Button btnDisplay = new Button("Display");
Button btnClear = new Button("Clear");
Label lblName = new Label("Enter your name: ");

public void init()


{
add(lblName);
add(txt1); add(txt2); add(txt3);
add(btnDisplay); add(btnClear);

btnClear.addActionListener(this); // add a listener to a Button object


btnDisplay.addActionListener(this);
}

public void actionPerformed(ActionEvent x) // method where the action


{ // would validate and perform
if(x.getSource()==btnClear) // the things to be done
{ txt2.setText(" ");
txt3.setText(" ");
}
else
{ txt2.setText("Hello " + txt1.getText());
txt3.setText("Good Day");
}
}
}

You might also like