Chapter 13 - Java AWT – Part II (Optional). In this chapter we will: discuss the use of Java in animating World Wide Web applications, show how to write Java applets, introduce Java graphical components (e.g. Textfields, Buttons, and Labels), show how Java programs can be made to react to user actions on Java graphical components, show how Java applications can be run from inside web browsers.
Trang 2Chapter Preview
In this chapter we will:
discuss the use of Java in animating World Wide
Web applications
show how to write Java applets
introduce Java graphical components (e.g Texttfields, Buttons, and Labels)
show how Java programs can be made to react to
user actions on Java graphical components
show how Java applications can be run from inside web browsers
Trang 3Frames
e A frame is a window with a title bar and a border
e The Frame class Is a subclass of Container class
* Container class objects may have other
components (e.g Buttons) added to them using the
add method
¢ Atypical Java GUI will create and display one or
more frames
¢ To make a frame visible the message
setVisbible( true) must be sent to the frame
Trang 4Can use pack( ) to set size to accommodate
preferred sizes of components)
Trang 5f = new Frame(“Two Buttons Frame”);
redButton = new Button(“Red” );
blueButton = new Button(“Blue”);
Trang 7UML Notation
The filled diamond represents composition
This shows that the class TwoButtons
contains a class Frame which contains a
class Buttons
A good UML diagram only shows the classes and associations that are important to
understanding the architecture
This UML diagram does not Include super
classes like Component or Container
Trang 8Using Inheritance
¢ It is possible to use inheritance to allow
TwoButtons to become a frame tn Its own right
¢ Note that the UML diagram will show
TwoButtons as a subclass of Frame
¢ You will need to use super ( )to set the
frame title
Trang 10Frame Inheritance Example
public class TwoButtons extends Frame {
Button redButton, blueButton;
public TwoButttons() {
super(“Two Buttons Frame”);
redButton = new Button(“Red” );
blueButton = new Button(“Blue”);
Trang 11Other Simple Java Components
¢ Label
— contains some text that can be set using a
constructor or the method setLabel
¢ TextField
— a box into which the user can type text
— text can be edited (backspace, delete, etc.)
— text can be retrieved using getText( )
— text can be set using setText( )
Trang 12Java AWT Event Model
Including reactive program components involves:
1 Having the class header declare itself as
Implementing the ActionListener interface
Typically in the class constructor the class instance registers itself as being interested in listening for
events from a newly created component
One method (e.g actionPer formed) of the
ActiveListener interface is defined
Trang 13Frame Inheritance Example
public class classname extends Frame implements ActionListener {
public void actionPerformed(ActionEvent e) {
what to do when button is pushed
}
}
Trang 15UML Notation for Button Program
¢ Abstract class indicated by italicizing the
class name
¢ Implements association (concrete class)
indicated by line with a closed arrowhead
¢ UML stereotype (constructors, accessor,
interface implementation methods) indicated
by enclosing a descriptive word inside
guillements << >>
Trang 16Closing the Window
¢ You must implement the WindowListener and its seven required methods
ClosableFrame
Trang 17generatespP
Trang 18Using Conditionals with Reactive Components
public void actionPerformed(ActionEvent e) {
double fahr, cent;
// check for user input in tFahr TextField
Trang 19Checkboxes
Used to allow user to select one or more items
Always has a label
Program often needs to react to the user selection(s)
Declaration example:
Checkbox powerBrakes = new Checkbox(“Power Brakes”);
Checkbox powerSteering = new Checkbox(“Power Steering”);
Checkbox ac = new Checkbox(“Air Conditioning” );
add(powerBrakes );
add(powerSteer1ng);
add(ac);
Trang 20The ItemEvent argument Is used to tell
which item triggered the event by calling
getSource
Trang 21Radio Buttons
A group of Checkboxes in which only one item can be selected at a time
Implemented using a Java CheckboxGroup
ltems are declared initially as selected (true) or
Trang 22Processing Radio Buttons
public void compute() {
boolean female =
(gender getSelectedCheckbox() == femaleCheck);
if ((bodyMassIndex > 27.8) ||
(female && (bodyMassIndex > 27.3))
lHigh.setText(“This is considered high”);
else
1High.setText(“this is not considered high”);
Trang 23Drawing Ina Frame
¢ To draw in a Frame you need to the override the frame’s paint method:
public void paint(Graphics g)
¢ Graphics objects are defined by the Java
runtime system and are used for drawing
operations
¢ The Frame should be considered to be a grid with upper left coordinates (0,0) and positive coordinates (x,y) for the lower right
Trang 24Typical Drawing Code
public class MyDrawing extends ClosableFrame {
Trang 25Repaint and Update
¢ The paint method requires an argument and actionPerformed does not know what to supply when called
¢ The method repaint will clear the Frame
and then call paint along with supplying the needed missing argument
¢ You can prevent clearing the Frame when
using repaint by overriding the method
update before calling repaint
Trang 26
«contains
«governs layout
Trang 27
Panel and Canvas Classes
¢ The Panel class Is container subclass that Is used to reserve a rectangular portion of a
Frame to place other components
¢ The Canvas class Is not a Container
Subclass, but does allow you to reserve a
portion of a Frame to draw In
Trang 28Comparing Layout Managers
FlowLayout
— Default frame layout
— Components are placed on new line only when needed
— Frame is divided into north, south, east, west, and center
— Components are placed by the programmer in the desired location using the add method
Trang 29Sonne hati
Trang 30
Typical GUI Frame
Panel pi = new Panel();
Trang 31¢ Scrollbars can be displayed vertically
¢ User movement options
— Clicking either end button causes bubble to move in unit Increments
— Clicking the are between bubble and end button
causes movement in 10 unit increments
— Clicking and dragging the bubble in the desired
direction
Trang 32Hypertext Markup Language
Trang 33In my spare time I like to do origami
and spend quiet evenings at home with my wife Josephine
</P>
Trang 34HTML Using Pictures and Links
¢ Displays a centered picture
¢ To display a picture link to another web page
<A HREF="“jojo.html”><IMG SRC=“jojo.jpg”></A>
Trang 35Applet Tag and HTML
¢ To include a Java applet as part of an HTML
public class BodyMass extends Applet {
public_html void start() {
new BodyMass();
}
}
Trang 36Transforming a Java Program
4 Change constructor heading to
public void start();
1 Eliminate any Java code that is meaningless
In applets