Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

23
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010

Transcript of Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Page 1: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Java Applets: GUI Components, Events, Etc.

Ralph WestfallJune, 2010

Page 2: 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

Page 3: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 4: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 5: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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)

Page 6: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

"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

Page 7: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 8: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 9: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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);}

Page 10: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 11: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 12: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 13: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

"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

Page 14: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Using a Listener need another import statement at

topimport java.awt.event.*;

the class also needs to "implement" an ActionListenerpublic class Greet extends Applet

implements ActionListener

Page 15: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Using a Listener - 2 attach a listener method to an

objectpublic void init(){ add(aButton); // declared above init aButton.addActionListener(this);

}

Page 16: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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*/

Page 17: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 18: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Other Interactivity can also add methods to

actionPerformed method to remove GUI components

remove(aLabel);

Page 19: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 20: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 21: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

Placing Components on Applet use setLocation method with a

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

Page 22: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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

Page 23: Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.

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