Documentation

The Java™ Tutorials
Hide TOC
How to Use Tool Tips如何使用工具提示
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use Tool Tips如何使用工具提示

Creating a tool tip for any JComponent object is easy. 为任何JComponent对象创建工具提示都很容易。Use the setToolTipText method to set up a tool tip for the component. 使用setToolTipText方法为组件设置工具提示。For example, to add tool tips to three buttons, you add only three lines of code:例如,要向三个按钮添加工具提示,只需添加三行代码:

b1.setToolTipText("Click this button to disable the middle button."); 
b2.setToolTipText("This middle button does not react when you click it.");
b3.setToolTipText("Click this button to enable the middle button.");

When the user of the program pauses with the cursor over any of the program's buttons, the tool tip for the button comes up. 当程序用户将光标停留在程序的任何按钮上时,按钮的工具提示会出现。You can see this by running the ButtonDemo example, which is explained in How to Use Buttons, Check Boxes, and Radio Buttons. 您可以通过运行ButtonDemo示例看到这一点,该示例在如何使用按钮、复选框和单选按钮中进行了说明。Here is a picture of the tool tip that appears when the cursor pauses over the left button in the ButtonDemo example.以下是当光标停留在ButtonDemo示例中的左按钮上时出现的工具提示的图片。

ButtonDemo showing a tool tip.

For components such as tabbed panes that have multiple parts, it often makes sense to vary the tool tip text to reflect the part of the component under the cursor. 对于具有多个部分的选项卡式窗格等组件,更改工具提示文本以反映光标下组件的部分通常是有意义的。For example, a tabbed pane might use this feature to explain what will happen when you click the tab under the cursor. 例如,选项卡式窗格可能使用此功能来解释单击光标下的选项卡时发生的情况。When you implement a tabbed pane, you can specify the tab-specific tool tip text in an argument passed to the addTab or setToolTipTextAt method.实现选项卡式窗格时,可以在传递给addTabsetToolTipTextAt方法的参数中指定特定于选项卡的工具提示文本。

Even in components that have no API for setting part-specific tool tip text, you can generally do the job yourself. 即使在没有用于设置零件特定工具提示文本的API的组件中,您通常也可以自己完成这项工作。If the component supports renderers, then you can set the tool tip text on a custom renderer. 如果组件支持渲染器,则可以在自定义渲染器上设置工具提示文本。The table and tree sections provide examples of tool tip text determined by a custom renderer. 表格里部分提供了由自定义渲染器确定的工具提示文本示例。An alternative that works for all JComponents is creating a subclass of the component and overriding its getToolTipText(MouseEvent) method.另一种适用于所有JComponent的方法是创建组件的子类并重写其getToolTipText(MouseEvent)方法。

The Tool Tip API工具提示API

Most of the API you need in order to set up tool tips belongs to the JComponent class, and thus is inherited by most Swing components. 设置工具提示所需的大部分API都属于JComponent类,因此大多数Swing组件都继承了它。More tool tip API can be found in individual classes such as JTabbedPane. 更多工具提示API可以在个别类中找到,如JTabbedPaneIn general, those APIs are sufficient for specifying and displaying tool tips; you usually do not need to deal directly with the implementing classes JToolTip and ToolTipManager.通常,这些API足以指定和显示工具提示;通常不需要直接处理实现类JToolTipToolTipManager

The following table lists the tool tip API in the JComponent class. 下表列出了JComponent类中的工具提示API。For information on individual components' support for tool tips, see the how-to section for the component in question.有关单个组件对工具提示的支持的信息,请参阅相关组件的“操作”部分。

Tool Tip API in the JComponent classJComponent类中的工具提示API
Method方法 Purpose目的
setToolTipText(String) If the specified string is not null, then this method registers the component as having a tool tip and, when displayed, gives the tool tip the specified text. 如果指定的字符串不为空,则此方法将组件注册为具有工具提示,并在显示时向工具提示提供指定的文本。If the argument is null, then this method turns off the tool tip for this component.如果参数为空,则此方法将关闭此组件的工具提示。
String getToolTipText() Returns the string that was previously specified with setToolTipText.返回以前使用setToolTipText指定的字符串。
String getToolTipText(MouseEvent) By default, returns the same value returned by getToolTipText(). 默认情况下,返回与getToolTipText()返回的值相同的值。Multi-part components such as JTabbedPane, JTable, and JTree override this method to return a string associated with the mouse event location. 多部分组件(如JTabbedPaneJTableJTree)重写此方法以返回与鼠标事件位置关联的字符串。For example, each tab in a tabbed pane can have different tool tip text.例如,选项卡式窗格中的每个选项卡可以具有不同的工具提示文本。
Point getToolTipLocation(MouseEvent) Returns the location (in the receiving component's coordinate system) where the upper left corner of the component's tool tip appears. 返回组件工具提示左上角出现的位置(在接收组件的坐标系中)。The argument is the event that caused the tool tip to be shown. 参数是导致显示工具提示的事件。The default return value is null, which tells the Swing system to choose a location.默认返回值为空,这将告诉Swing系统选择一个位置。

Examples That Use Tool Tips使用工具提示的示例

This table lists some examples that use tool tips and points to where those examples are described.下表列出了一些使用工具提示的示例,并指出了这些示例的描述位置。

Example示例 Where Described描述位置 Notes备注
ButtonDemo This section and 本节和How to Use Buttons, Check Boxes, and Radio Buttons如何使用按钮、复选框和单选按钮 Uses a tool tip to provide instructions for a button.使用工具提示为按钮提供说明。
IconDemo How to Use Icons如何使用图标 Uses a tool tip in a label to provide name and size information for an image.使用标签中的工具提示为图像提供名称和大小信息。
TabbedPaneDemo How to Use Tabbed Panes如何使用选项卡式窗格 Uses tab-specific tool tip text specified in an argument to the addTab method.使用addTab方法参数中指定的特定于选项卡的工具提示文本。
TableRenderDemo Specifying Tool Tips for Cells为单元格指定工具提示 Adds tool tips to a table using a renderer.使用渲染器将工具提示添加到表中。
TableToolTipsDemo Specifying Tool Tips for Cells为单元格指定工具提示, Specifying Tool Tips for Column Headers为列标题指定工具提示 Adds tool tips to a table using various techniques.使用各种技术将工具提示添加到表中。
TreeIconDemo2 Customizing a Tree's Display自定义树的显示 Adds tool tips to a tree using a custom renderer.使用自定义渲染器将工具提示添加到树。
ActionDemo How to Use Actions如何使用动作 Adds tool tips to buttons that have been created using Actions.向使用Action创建的按钮添加工具提示。

Previous page: How to Use Tool Bars
Next page: How to Use Trees