文档

Java™ 教程-Java Tutorials 中文版
如何编写组件监听器
Trail: Creating a GUI With JFC/Swing
Lesson: Writing Event Listeners
Section: Implementing Listeners for Commonly Handled Events

如何编写组件监听器

The Component listener is a listener interface for receiving component events. A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Some of the examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface.

The class that is interested in processing a component event either implements this interface and all the methods it contains, or extends the abstract ComponentAdapter class overriding only the methods of interest. The listener object created from that class is then registered with a component using the component's addComponentListener method. When the component's size, location, or visibility changes, the relevant method in the listener object is invoked, and the ComponentEvent is passed to it.

One or more component events are fired by a Component object just after the component is hidden, made visible, moved, or resized.

The component-hidden and component-shown events occur only as the result of calls to a Component 's setVisible method. For example, a window might be miniaturized into an icon (iconified) without a component-hidden event being fired.

To write a simple Component listener program, follow the steps mentioned below:

The following example demonstrates component events. The window contains a panel that has a label and a check box. The check box controls whether the label is visible. A text area displays a message every time the window, panel, label, or check box fires a component event.

A window demonstrating component events

试试这个: 
  1. Click the Launch button to run ComponentEventDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.
    Launches the ComponentEventDemo example
  2. When the window appears, one or more component-shown events have been fired.
  3. Click the check box to hide the label.
    The label fires a component-hidden event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.
  4. Click the check box again to show the label.
    The label fires a component-shown event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.
  5. Iconify and then deiconify the window.
    You do not get component-hidden or -shown events. If you want to be notified of iconification events, you should use a window listener or a window state listener.
  6. Resize the window.
    You will see component-resized (and possibly component-moved) events from all four components — label, check box, panel, and frame. If the frame and panel's layout manager did not make every component as wide as possible, the panel, label, and check box would not have been resized.

You can find the demo's code in ComponentEventDemo.java. Here is just the code related to handling component events:

public class ComponentEventDemo ... implements ComponentListener {
    static JFrame frame;
    JLabel label;
    ...
    public ComponentEventDemo() {
        ...
        JPanel panel = new JPanel(new BorderLayout());
        label = new JLabel("This is a label", JLabel.CENTER);
        label.addComponentListener(this);
        panel.add(label, BorderLayout.CENTER);

        JCheckBox checkbox = new JCheckBox("Label visible", true);
        checkbox.addComponentListener(this);
        panel.add(checkbox, BorderLayout.PAGE_END);
        panel.addComponentListener(this);
        ...
        frame.addComponentListener(this);
    }
    ...
     public void componentHidden(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Hidden");
    }

    public void componentMoved(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Moved");
    }

    public void componentResized(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Resized ");            
    }

    public void componentShown(ComponentEvent e) {
        displayMessage(e.getComponent().getClass().getName() + " --- Shown");

    }

    public static void main(String[] args) {
        ...
        //Create and set up the window.
        frame = new JFrame("ComponentEventDemo");
        ...
        JComponent newContentPane = new ComponentEventDemo();
        frame.setContentPane(newContentPane);
        ...
    }
}

The Component Listener API

The ComponentListener Interface

All of these methods are also in the adapter class, ComponentAdapter.

方法 目的
componentHidden(ComponentEvent) Called after the listened-to component is hidden as the result of the setVisible method being called.
componentMoved(ComponentEvent) Called after the listened-to component moves, relative to its container. For example, if a window is moved, the window fires a component-moved event, but the components it contains do not.
componentResized(ComponentEvent) Called after the listened-to component's size (rectangular bounds) changes.
componentShown(ComponentEvent) Called after the listened-to component becomes visible as the result of the setVisible method being called.

The ComponentEvent Class

方法 目的
Component getComponent() Returns the component that fired the event. You can use this instead of the getSource method.

Examples that Use Component Listeners

The following table lists the examples that use component listeners.
例子 Where Described Notes
ComponentEventDemo This section Reports all component events that occur on several components to demonstrate the circumstances under which component events are fired.

Previous page: How to Write a Change Listener
Next page: How to Write a Container Listener