Java Applets

Download Report

Transcript Java Applets

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
machine
Creating an Applet

need import statements to bring in
classes, etc. that Applet will use



put extends Applet after class name


import java.applet.*;
import java.awt.*;
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
text
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
JOptionPane

Java Swing class (lightweight) "bean"
that includes components including:




Label,
TextField
Buttons
Java beans: classes with extra features

makes them easier to use with other
classes, languages, and on different
platforms
Additional Components

the Java Swing class has additional GUI
components (that are NOT platform
dependent)



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()
{
add(aLabel);
}
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 - 2

can use methods to manipulate
components


set focus (cursor position) onto a TextField
get contents (user input) from a TextField

e.g., getting String from JOptionPane
Event-Driven Programming

event = something that happens


mouse click, mouse over, mouse out,
button push, key stroke, time of day, etc.
event-driven program flow is (partially)
controlled by external events

rather than just by internal logic
"Listener"


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
ActionListener
public class Greet extends Applet
implements ActionListener
Using a Listener - 2

attach a listener method to an object
public void init()
{
add(aButton); // declared above init
aButton.addActionListener(this);
}
Using a Listener - 3

add an actionPerformed method
public void actionPerformed(ActionEvent
thisEvent)
{
aLabel.setText("Hi");
}
/*will get fatal error if have an ActionListener
without actionPerformed method in class*/
Adding Interactive Output

can add statements to actionPerformed
method


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-of-date
validate(); // redraws out-of-date Applet
Other Interactivity

can also add methods to
actionPerformed method to remove GUI
components
remove(aLabel);
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


1st number is x (horizontal) position
2nd 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
component
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
clickButton.setEnabled(false);

button previously declared by user will not
respond to clicks
clickButton.setEnabled(true);

default condition, don't have to set to true
if didn't previously set it to false