Java Reference
In-Depth Information
Figur e 10•13. The Scribble application
to an
ActionEvent
generated by a component. What the
Action
interface adds to
the
ActionListener
interface is the ability to associate arbitrary properties with an
Action
object. The
Action
interface also defines standard property names that can
specify the name, icon, and description of the action performed by the listener.
Action
objects are particularly convenient to use because they can be added
directly to
JMenu
and
JToolBar
components; the components use the action name
and/or icon to automatically create appropriate menu items or toolbar buttons to
represent the action. (In Java 1.3,
Action
objects can also be passed directly to the
constructor methods of components such as
JButton
.) Action objects can also be
enabled or disabled. When an action is disabled, any component that has been
created to represent it is also disabled, preventing the user from attempting to per-
form the action.
Example 10•16: Scribble.java
package com.davidflanagan.examples.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* This JFrame subclass is a simple "paint" application.
**/
public class Scribble extends JFrame {
/**
* The main method instantiates an instance of the class, sets it size,
* and makes it visible on the screen
**/
public static void main(String[] args) {
Scribble scribble = new Scribble();
scribble.setSize(500, 300);
scribble.setVisible(true);
}
// The scribble application relies on the ScribblePane2 component developed
// earlier. This field holds the ScribblePane2 instance it uses.
ScribblePane2 scribblePane;
/**
* This constructor creates the GUI for this application.