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

Introduction

Java applets are one of three


kinds of Java programs:
◦ An application is a standalone
program that can be invoked from
the command line.
◦ An applet is a program that runs in
the context of a browser session.
◦ A servlet is a program that is
invoked on a server program, and it
runs in the context of a web server
process.
Applications versus
Applets
 an Applet is a Java program that runs in a web page, while an
application is one that runs from the command line.
 An Applet is a Panel that is automatically inserted into a web
page. The browser displaying the web page instantiates and
adds the Applet to the proper part of the web page.
 The browser tells the Applet when to create its GUI (by calling
the init() method of Applet) and when to start() and stop() any
special processing.
 Applications run from a command prompt. When you execute
an application from the command prompt, the interpreter
starts by calling the application's main() method.
AWT
The Java programming language
provides a class library called the
Abstract Window Toolkit
(AWT) that contains a number of
common graphical widgets
EVENT handling
mechanism Event Object
• describes an
event
• ex.
Event Source ActionEvent
• generates holds state of
events Shift key
• ex. Button Event Listener
• any object can
implement these
interfaces
• ex.
ActionListener has
method
actionPerformed()
The hierarchy of class
java.applet.Applet
 Thefollowing representation shows the
hierarchy of Applet back to Object and the
packages they are defined in..

Object                          java.lang
   +--
Component                 java.awt
        +--Container            java.awt
             +--Window          java.awt
             |    +--Frame      java.awt
             +--Panel           java.awt
                  +--
Window Fundamentals
Component
An abstract class that encapsulate all of
the attributes of a visual component.
All the user interface element that are
displayed on the screen and that interact
with the user are subclasses of
Component
Defines method for managing
events,such as mouse and keyboard
inputs ,positioning and sizing of window..
Responsible for remembering the
foreground and background colors
Window Fundamentals
Container
◦ Subclass of component.
◦ Allows other component objects to be
nested within it
Panel
◦ Subclass of Container and superclass of
applet
◦ Screen output directed to applet , it is
drawn on the surface of a Panel object
◦ A panel is a window that does not contain
a title bar, menu bar and border
◦ Other components can be added by add()
method
Window
Window
◦ Wont create window objects directly
◦ Instead we use a subclass of Window
called Frame
Frame
◦ Has a title bar, menu bar, borders
and resizing corners
◦ Creating a frame object within an
applet, it will contain a warning
message(“A applet window”) to the
user.
Window Fundamentals
Canvas
◦ Encapsulates a blank window upon
which we can draw.
Buttons

A Button has a single line label


and may be "pushed" with a
mouse click.

Type of event generated- Action Event


import java.awt.*;
import java.applet.*;
public class ButtonTest extends
Applet {
public void init()
{
Button button = new
Button("OK"); add(button);
}
}
Checkbox

A Checkbox is a label with a


small pushbutton.
The state of a Checkbox is either
true (button is checked) or false
(button not checked).
The default initial state is false.
Clicking a Checkbox toggles its
state.
Checkbox m = new
Checkbox("Label", true);
Type of event generated- Item Event
(when the check box is selected or
deselected)
import java.awt.*;
import java.applet.*;
public class CheckboxSimpleTest
extends Applet {
public void init() {
Checkbox m = new Checkbox ("Allow Mixed
Case");
add(m);
}
}
CheckboxGroup
A CheckboxGroup is used to
control the behavior of a group of
Checkbox objects (each of which
has a true or false state).
Exactly one of the Checkbox
objects is allowed to be true at
one time.
Checkbox objects controlled with
a CheckboxGroup are usually
referred to as "radio buttons".
import java.awt.*;
import java.applet.*;
public class CheckboxGroupTest extends Applet {
public void init() {
// create button controller
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("Show lowercase
only", cbg, true);
Checkbox cb2 = new Checkbox("Show uppercase
only", cbg, false);
add(cb1);
add(cb2);
}
}
Type of event generated- Item Event
(when the check box is selected or
deselected)
Choice
Choice objects are drop-down
lists. The visible label of the
Choice object is the currently
selected entry of the Choice.

Type of event generated- Item Event


(when the choice is changed)
Example
import java.awt.*;
import java.applet.*;
public class ChoiceSimpleTest
extends Applet
{
public void init() {
Choice rgb = new Choice();
rgb.add("Red");
rgb.add("Green");
rgb.add("Blue"); add(rgb);
}
}
Label

A Label is a displayed Label


object. It is usually used to help
indicate what other parts of the
GUI do, such as the purpose of a
neighboring text field.
import java.awt.*;
import java.applet.*;
public class LabelTest extends Applet {
public void init()
{
add(new Label("A label"));
//equivalent to Label a1=new Label(“A
label”));
add(a1);
// right justify next label
add(new Label("Another label",
Label.RIGHT));
List

A List is a scrolling list box that


allows you to select one or more
items.
Multiple selections may be used
by passing true as the second
argument to the constructor.
import java.awt.*;
import java.applet.*;
public class ListSimpleTest extends
Applet {
public void init() {
List list = new List(5, false);

list.add("Seattle");
list.add("Washington");
list.add("New York");
list.add("Chicago");
list.add("Miami"); list.add("San Jose");
list.add("Denver");

add(list);
}
Type of event generated-
Item Event (when the item is
selected or deselected)
Action Event(when an item is
double clicked)
TextField

A TextField is a scrollable text


display object with one row of
characters.
The preferred width of the field
may be specified during
construction and an initial string
may be specified.
import java.awt.*;
import java.applet.*;
public class TextFieldSimpleTest
extends Applet
{
public void init()
{ TextField f1 = new TextField("type
something");
add(f1);
}
}
TextArea

A TextArea is a multi-row text


field that displays a single string
of characters, where newline ('\n'
or '\n\r' or '\r', depending on
platform) ends each row.
The width and height of the field
is set at construction, but the text
can be scrolled up/down and
left/right.
import java.awt.*;
import java.applet.*;
public class TextAreaSimpleTest
extends Applet {
TextArea disp;
public void init() {
disp = new TextArea("Code goes
here", 10, 30);
add(disp);
}
Event Handling
Low-level Events
 Low-level events represent a low-level input or
window operation, like a key press, mouse
movement, or window opening.
 The following table displays the different low-
level events, and the operations that generate
each event (each operation corresponds to a
method of the listener interface):
 ComponentEvent Hiding, moving, resizing,
showing
 ContainerEvent Adding/removing component
 FocusEvent Getting/losing focus
 KeyEvent Pressing, releasing, or typing (both) a
key
 MouseEvent Clicking, dragging, entering,
exiting, moving, pressing,
or releasing
Example
For instance, typing the letter 'A'
on the keyboard generates three
events, one for pressing, one for
releasing, and one for typing.
Depending upon your interests,
you can do something for any of
the three events.
Semantic Events
Semantic events represent interaction
with a GUI component; for instance
selecting a button, or changing the
text of a text field.

ActionEvent Do the command


AdjustmentEvent Value adjusted
ItemEvent State changed
TextEvent Text changed
Event Sources
The following table represents the
different event sources. Keep in mind the
object hierarchy. For instance, when
Component is an event source for
something, so are all its subclasses:
Low-Level Events
Component
ComponentListener
FocusListener KeyListener
MouseListener MouseMotionListener

Container ContainerListener
Window WindowListener
Semantic Events
Button
List
MenuItem
TextField ActionListener
Choice
Checkbox
MenuItem
List ItemListener
Scrollbar
AdjustmentListener
TextArea
TextField TextListener
Canvas

A Canvas is a graphical
component representing a region
where you can draw things such
as rectangles, circles, and text
strings.
 You subclass Canvas to override
its default paint() method to
define your own components.
import java.awt.*;
class DrawingRegion extends Canvas
{
public DrawingRegion()
{
setSize(100, 50);
}
public void paint(Graphics g)
{
g.drawRect(0, 0, 99, 49);
// draw border
g.drawString("A Canvas", 20,20); }
}
import java.applet.*;
public class CanvasPaintTest extends
Applet {
public void init()
{
DrawingRegion region = new
DrawingRegion();
add(region);
}
}

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