# Swing 组件支持的监听器

> 原文： [https://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html](https://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html)

通过查看可以在其上注册的事件侦听器的类型，您可以了解组件可以触发的事件类型。例如，`JComboBox`类定义了这些侦听器注册方法：

*   `addActionListener`
*   `addItemListener`
*   `addPopupMenuListener`

因此，除了从`JComponent`继承的侦听器方法之外，组合框还支持 action，item 和弹出菜单侦听器。

Swing 组件支持的监听器分为两类：

*   [所有 Swing 组件支持的听众](#all)
*   [Swing 组件支持的其他听众](#many)

## 所有 Swing 组件支持的监听器

因为所有 Swing 组件都来自 AWT `Component`类，所以您可以在任何 Swing 组件上注册以下侦听器：

**[component listener](componentlistener.html)**

Listens for changes in the component's size, position, or visibility.

**[focus listener](focuslistener.html)**

Listens for whether the component gained or lost the keyboard focus.

**[key listener](keylistener.html)**

Listens for key presses; key events are fired only by the component that has the current keyboard focus.

**[mouse listener](mouselistener.html)**

Listens for mouse clicks, mouse presses, mouse releases and mouse movement into or out of the component's drawing area.

**[mouse-motion listener](mousemotionlistener.html)**

Listens for changes in the mouse cursor's position over the component.

**[mouse-wheel listener](mousewheellistener.html)**

Listens for mouse wheel movement over the component.

[**Hierarchy Listener**](https://docs.oracle.com/javase/8/docs/api/java/awt/event/HierarchyListener.html)

Listens for changes to a component's containment hierarchy of changed events.

[**Hierarchy Bounds Listener**](https://docs.oracle.com/javase/8/docs/api/java/awt/event/HierarchyBoundsListener.html)

Listens for changes to a component's containment hierarchy of moved and resized events.

所有 Swing 组件都来自 AWT `Container`类，但其中许多不用作容器。因此，从技术上讲，任何 Swing 组件都可以触发[容器事件](containerlistener.html)，它会通知侦听器组件已添加到容器中或从容器中删除。然而，实际上，只有容器（例如面板和框架）和复合组件（例如组合框）通常会触发容器事件。

`JComponent`为另外三种侦听器类型提供支持。您可以注册[祖先监听器](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/AncestorListener.html)，以便在将容器的容器祖先添加到容器中或从容器中移除，隐藏，显示或移动时收到通知。此侦听器类型是一个实现细节，它先于层次结构侦听器。

另外两个侦听器类型是 Swing 组件与 JavaBeans 规范的一致性的一部分。除此之外，这意味着 Swing 组件支持绑定和约束属性，并通知侦听器属性的更改。 [属性更改侦听器](propertychangelistener.html)侦听绑定属性的更改，并由多个 Swing 组件（如[格式化文本字段](../components/formattedtextfield.html)）用于跟踪组件绑定属性的更改。此外，构建器工具使用属性更改侦听器以及[可否决更改侦听器](https://docs.oracle.com/javase/8/docs/api/java/beans/VetoableChangeListener.html)来侦听受约束属性的更改。有关更多信息，请参阅 [JavaBeans](../../javabeans/) 跟踪中的[属性](../../javabeans/writing/properties.html)课程。

## Swing 组件支持的其他监听器

下表列出了 Swing 组件及其支持的专用侦听器，不包括所有`Component`，`Container`或`JComponent`支持的侦听器。在许多情况下，事件是直接从组件触发的。在其他情况下，事件是从组件的数据或选择模型触发的。要查找您感兴趣的特定组件和侦听器的详细信息，请先查看组件操作方法部分，然后根据需要访问侦听器操作方法部分。

This table lists Swing components with their specialized listeners
| 零件 | [动作听众](actionlistener.html) | [Caret Listener](caretlistener.html) | [改变听众](changelistener.html) | [文件监听](documentlistener.html)，
[可撤销编辑监听](undoableeditlistener.html) | [项目监听](itemlistener.html) | [列表选择监听器](listselectionlistener.html) | [窗口监听器](windowlistener.html) | 其他类型的听众 |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| [按钮](../components/button.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |
| [复选框](../components/button.html#checkbox) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |
| [颜色选择器](../components/colorchooser.html) |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |
| [组合框](../components/combobox.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |
| [对话框](../components/dialog.html) |  |  |  |  |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |
| [编辑器面板](../components/editorpane.html) |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  | [超链接](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/HyperlinkListener.html) |
| [文件选择器](../components/filechooser.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |  |  |
| [格式化文本字段](../components/formattedtextfield.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |
| [框架](../components/frame.html) |  |  |  |  |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |
| [内部框架](../components/internalframe.html) |  |  |  |  |  |  |  | [内部框架](internalframelistener.html) |
| [清单](../components/list.html) |  |  |  |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | [列表数据](listdatalistener.html) |
| [菜单](../components/menu.html) |  |  |  |  |  |  |  | [菜单](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/MenuListener.html) |
| [菜单项](../components/menu.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  | [菜单键](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/MenuKeyListener.html)
[菜单拖动鼠标](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/MenuDragMouseListener.html) |
| [选项面板](../components/dialog.html) |  |  |  |  |  |  |  |  |
| [密码字段](../components/passwordfield.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |
| [弹出菜单](../components/menu.html) |  |  |  |  |  |  |  | [弹出菜单](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/PopupMenuListener.html) |
| [进度条](../components/progress.html) |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |
| [单选按钮](../components/button.html#radiobutton) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |
| [滑块](../components/slider.html) |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |
| [微调器](../components/spinner.html) |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |
| [标签式窗格](../components/tabbedpane.html) |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |
| [表](../components/table.html) |  |  |  |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | [表模型](tablemodellistener.html)
[表列模型](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/TableColumnModelListener.html)
[细胞编辑器](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/CellEditorListener.html) |
| [文字区](../components/textarea.html) |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |
| [文字字段](../components/textfield.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |
| [文字窗格](../components/editorpane.html) |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  | [超链接](https://docs.oracle.com/javase/8/docs/api/javax/swing/event/HyperlinkListener.html) |
| [切换按钮](../components/button.html) | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |
| [树](../components/tree.html) |  |  |  |  |  |  |  | [树扩展](treeexpansionlistener.html)
[树将扩展](treewillexpandlistener.html)
[树模型](treemodellistener.html)
[树选择](treeselectionlistener.html) |
| viewport
（由 [scrollpane](../components/scrollpane.html) 使用） |  |  | 

![checked](img/12bd475494acc0d71eb308e2275a6457.jpg)

 |  |  |  |  |  |