Components, Events, Etc.

slimwhimperΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 8 μήνες)

87 εμφανίσεις

Java Applets: GUI
Components, Events, Etc.

Ralph Westfall

June, 2010

What Is an Applet?

a special Java class that doesn't run in a
DOS window

runs in a web browser on client, in a
window, on a HTML page

Java has security features to keep the
applet from acting like a virus on the client

Creating an Applet

need import statements to bring in
classes, etc. that Applet will use

import java.applet.*;

import java.awt.*;

put extends Applet after class name

has Applet functionality, plus what you add

has a public void init() method

has no main() method

// code below

GUI Components

components are objects (or like objects)

GUI means graphic user interface

graphic is the opposite of text

you see something other than just plain

interface is what the user works with
when using a computer program

GUI Components for Applets

some "heavyweight" (platform
dependent) components in Java

Label: writes text on the Applet

Button: push button to trigger action

TextField: one line input box

TextArea: multi
line input box

others: scrollbar, etc. (similar to many
components in Windows programs)

"Heavyweight" vs. Swing

earlier Java versions had "heavyweight"
platform dependent AWT (abstract
windowing toolkit) components

different on different operating systems

"lightweight" Swing components are
platform independent in many ways

e.g., look the same on PC or Apple


Java Swing class (lightweight) "bean"
that includes components including:




Java beans: classes with extra features

makes them easier to use with other
classes, languages, and on different

Additional Components

the Java Swing class has additional GUI
components (that are NOT platform

JCheckBox: yes or no choices

JRadioButton: can only select 1 in a group

JComboBox: drop down list that user can
type one or more characters into

Using GUI Components

need to declare the object

Label aLabel = new Label("Hello");

Button aButton = new Button("Click");

need to add objects to the Applet,
usually in its init() method

public void init()




GUI Component Methods

can use methods to set or change
properties on components

font on labels (type, style, size)

text on a button e.g., OK

input box size(s)

default content inside an input box

make content of input box editable or not

GUI Component Methods


can use methods to manipulate

set focus (cursor position) onto a TextField

get contents (user input) from a TextField

e.g., getting String from JOptionPane

Driven Programming

event = something that happens

mouse click, mouse over, mouse out,
button push, key stroke, time of day, etc.

driven program flow is (partially)
controlled by external events

rather than just by internal logic


object that waits for an event and then
triggers a method that responds to it

associated with another object e.g.,
button or text component

Using a Listener

need another import statement at top

import java.awt.event.*;

the class also needs to "implement" an

public class Greet extends Applet

implements ActionListener

Using a Listener


attach a listener method to an object

public void init()


add(aButton); // declared above init



Using a Listener


add an actionPerformed method

public void actionPerformed(ActionEvent





/*will get fatal error if have an ActionListener
without actionPerformed method in class*/

Adding Interactive Output

can add statements to actionPerformed

e.g., labels with text from user inputs

may need to force Applet to redraw
itself by adding following methods:

invalidate(); // marks Applet as out

validate(); // redraws out
date Applet

Other Interactivity

can also add methods to
actionPerformed method to remove GUI


Controlling Position

when add components without a layout
manager, Java chooses where to put
each item

left to right until row is filled, and then
starts a new row below previous

as space permits, centered in row

can subsequently move components to
locations identified by coordinates

Window Coordinate System


number is x (horizontal) position


number is y (vertical) position

upper left corner is 0, 0

100, 0 is top of window, 100 pixels to right
of upper left corner

0, 100 is left side of window, 100 pixels
down from upper left corner

100, 100 is 100 pixels over, 100 down

Placing Components on Applet

use setLocation method with a

myLabel.setLocation(50, 150); // where?

Disable and Enable

disable makes it impossible to click a
button, checkbox, etc. e.g.

disable Print button until inputs are entered
and calculations are completed

enable makes the item functional again

enable Print button when data is OK

Enabling Components


button previously declared by user will not
respond to clicks


default condition, don't have to set to true
if didn't previously set it to false