Chapter 9 notes

Download Report

Transcript Chapter 9 notes

Java Programming,
Second Edition
Chapter Nine
Applets
In this chapter, you will:
 Write an HTML document to host an




applet
Understand simple applets
Use Labels with simple AWT applets
Write a simple Swing applet and use a
JLabel
Add JTextField and JButton Components
to Swing applets
 Learn about event-driven programming
 Add output to a Swing applet
 Understand the Swing applet life cycle
 Create a more sophisticated interactive
Swing applet
 Use the setLocation() and setEnabled()
methods
To Write a Java Application:
 Write the application in the Java
programming language, and then save it
with a .java file extension
 Compile the application into bytecode
using the javac command. The bytecode is
stored in a file with a .class file extension
 Use the java command to interpret and
execute the .class file
Writing an HTML Document to Host
an Applet
 Applets- Programs that are called from
within another application
 You run applets within
 a page on the Internet
 an intranet
 or a local computer from within another program
called Applet Viewer
 To view an applet, it must be called from
within another document written in HTML
Writing an HTML Document to Host
an Applet
To create an applet:
 Write the applet in the Java programming
language, and save it with a .java file extension
 Compile the applet into bytecode using the javac
command
 Write an HTML document that includes a
statement to call your compiled Java class
 Load the HTML document into a Web browser or
run the AppletViewer program
Writing an HTML Document to Host
an Applet
 Applets are popular because users can
execute them using a Web browser
 Web browser- A program that allows you to
display HTML documents on your
computer screen
 Internet Explorer
 Netscape Navigator
Writing an HTML Document to
Host an Applet
 Code to run an applet from within an HTML document
 <applet>
 </applet>
 Applet tag attributes
 CODE = is followed by the name of the compiled applet you are
calling
 WIDTH = is followed by the width of the applet on the screen
 HEIGHT = is followed by the height of the applet on the screen
Applets
 The WIDTH and HEIGHT attributes are
measured in pixels
 Pixels- Picture elements, or tiny dots that
make up the image on your video monitor
HTML Example
 Example of Garage.html
<applet code="Garage.class" width=250
height=120>
</applet>
Understanding Simple Applets
To write an applet you must also:
 Include import statements to ensure that
necessary classes are available
 Learn to use some Windows components
and applet methods
 Learn to use the keyword extends
Understanding Simple Applets
 Component- A class that defines any
object that you want to display
 Container- A class that is used to define a
component that can contain other
components
Understanding Simple Applets
 Most AWT applets contain 2 import
statements
 import java.applet.*;
 import java.awt.*;
 java.applet- Contains a class named Applet
 Every applet you create is based on Applet
 java.awt- The Abstract Windows Toolkit, or
AWT
Understanding Simple Applets
 Most Swing applets contain 2 import
statements
 import javax.swing.*;
 import java.awt.*;
 javax.swing- A package that contains
classes that define GUI components
(Swing components)
Understanding Simple Applets
 Swing classes- part of a more general set
of GUI programming capabilities that are
known as the Java Foundation Classes, or
JFC
 JFC includes Swing component classes and
selected classes from the java.awt package
AWT and Swing Applets
AWT and Swing applets
 Begin the same way as Java applications
 Must also include
 extends Applet
 extends JApplet
 The extends keyword indicates the applet will
build upon Applet and JApplet
Applets
 Four methods in every applet




public void init()
public void start()
public void stop()
public void destroy()
 Java can create these for you
Using Labels with Simple AWT
Applets
 The java.awt package contains commonly
used Windows components
 Labels
 Menus
 Buttons
 Label- Built-in class that holds text that
you can display within an applet
Using Labels with Simple AWT
Applets
 Label class contains fields that indicate
font and alignment
 You can assign some text to a label with
the setText() method
 Use the add() method to add a component
to an applet window
Writing a Simple Swing Applet and
Using a JLabel
 JLabel- Built-in class that holds text that
you can display within an applet
 The counterpart to the AWT Label
Writing a Simple Swing Applet and
Using a JLabel
Available constructors include:
JLabel() creates a JLabel instance with no image and an
empty string for the title
JLabel(Icon image) creates a JLabel instance with the
specified image
JLabel(Icon image, int horizontalAlignment) creates a
JLabel instance with the specified image and horizontal
alignment
JLabel(String text) creates a JLabel instance with the
specified text
JLabel(String text, Icon icon, int horizontalAlignment)
creates a JLabel instance with the specified text, image,
and horizontal alignment
JLabel(String text, int horizontalAlignment) creates a
JLabel instance with the specified text and horizontal
Writing a Simple Swing Applet and
Using a JLabel
 AWT components are added directly to the
Applet
 Swing components must use a content
pane
 The content pane is an object of the
Container class
 A container can be created using the
getContentPane() method
Changing a JLabel’s Font
 Font object- Holds typeface and size information
 setFont() method requires a Font object
argument
 To construct a Font object you need 3
arguments
 Typeface
 Style
 Point size
Changing a JLabel’s Font
To construct a Font object you need 3 arguments
 Typeface
 String representing a font
 Common fonts are Arial, Courier, and New Times Roman
 Is only a request
 Style- applies an attribute to displayed text
 Font.PLAIN
 Font.BOLD
 Font.ITALIC
 Point size
 Integer that represents 1/72 of an inch
 Printed text is usually 10- or 12 points
Adding JTextField Components to
Swing Applets
 JTextField- Component into which a user can
type a single line of text data
 JText field can be constructed from
 public JTextField() constructs a new JTextField
 public JTextField(int numColumns) constructs a new
empty JTextField with a specified number of columns
 public JTextField(String text) constructs a new
JTextField initialized with the specific text
 public JTextField(String text, int columns) constructs a
new JTextField with the specified text and columns
Other JTextField Methods
 setText() method- Allows you to change
the text in a JTextField that has already
been created
 getText() method- Allows you to retrieve
the string of text in a JTextField
Other JTextField Methods
 Keyboard focus- When the user clicks within the
JTextField, the JTextField has focus, which
means the next entries from the keyboard will be
at that location
 requestFocus() method- To have the insertion
point appear automatically within the TextField
without requiring the user to click in it first
Other JTextField Methods
 Editable- The capacity for a field to accept
keystrokes
 setEditable() method- Used to change the
editable status of a JTextField
Adding JButton Components to
Swing Applets
 JButton- Creates a button
 JButton can be constructed from
 public JButton() constructs a button with no set text
 public JButton(Icon icon) creates a button with an
icon of type Icon or ImageIcon
 public JButton(String text) creates a button with the
specific text
 public JButton(String text, int columns) constructs a
new JTextField with the specified text and columns
Adding JButton Components to
Swing Applets
 setLabel() method
 To change a JButton’s label
 readyJButton.setLabel(“Don’t press me again!”)
Adding Multiple Components to a
JApplet
 To add multiple components in a container
use a layout manager
 To control component positioning
 Default behavior is to use a border layout
 Border layouts
 Flow layouts
Adding Multiple Components to a
JApplet
 Border layouts
 Created by the BorderLayout class
 Divide a container into 5 sections
 North, South, East, West, and center
 Created with the BorderLayout() or
BorderLayout(int, int) methods
Adding Multiple Components to a
JApplet
 Flow Layouts
 Places components in a row, and when a row
is filled, it automatically spills components
onto the next row
 Default positioning of the row of components
is centered in the container
Learning about Event-Driven
Programming
 Event- Occurs when someone using your applet




takes action on a component
Procedural- Programmers dictate the order in
which events occur
Event-driven programs- The user can initiate any
number of events in any order
Source- Component on which an event is
generated
Listener- Object that is interested in an event
Preparing Your Swing Applet to
Accept Event Messages
 Prepare your applet to accept mouse
events by:
 importing the java.awt.event package
 adding the phrase implements ActionListener
to the class header
 ActionListener is an interface
 Interface- A set of specifications for
methods that you can use with event
objects
Telling Your Swing Applet to Expect
Events to Happen
 addActionListener() method
 To tell the applet to expect ActionEvents
 aButton.addActionListener(this);
Telling Your Swing Applet How to
Respond to Any Events That Happen
 actionPerformed(ActionEvent e) method
 When a JApplet has registered as a listener
with a JButton, and a user clicks the JButton
the actionPerformed method executes
Adding Output to a Swing
Applet
 You can add components to an applet
using the add() method
 You can also remove components from an
applet using the remove() method
 Remove(answer);
Understanding the Swing Applet
Life Cycle
 Override- When you write a method that
has the same method header as an
automatically provided method
Understanding the Swing Applet
Life Cycle
 start() method- Executes after the init()
method
 Executes every time the applet becomes
active after it has been inactive
 stop() method- When a user leaves a web
page
 You do not usually write your own stop()
methods
Understanding the Swing Applet
Life Cycle
 destroy() method- When the user closes
the browser or AppletViewer
 You do not usually write your own destroy()
methods
Using the setLocation() and
setEnabled() Methods
 setLocation() method- Allows you to place a
component at a specific location within the
AppletViewer window
 X-axis- Horizontal position in a window
 X-coordinate- Value increases as you travel from left
to right across the window
 Y-axis- Vertical position in the window
 Y-coordinate- Value increases as you travel from top
to bottom in the window
The setEnabled() Method
 setEnabled() method- To make a
component unavailable and, in turn, to
make it available again
 True if you want to enable a component
 False if you want to disable a component
 If (yLoc==280)
pressButton.setEnabled(false);