The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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
示例中的左按钮上时出现的工具提示的图片。
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.addTab
或setToolTipTextAt
方法的参数中指定特定于选项卡的工具提示文本。
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 另一种适用于所有JComponent
s is creating a subclass of the component and overriding its getToolTipText(MouseEvent)
method.JComponent
的方法是创建组件的子类并重写其getToolTipText(MouseEvent)
方法。
Most of the API you need in order to set up tool tips belongs to the 设置工具提示所需的大部分API都属于JComponent
class, and thus is inherited by most Swing components. JComponent
类,因此大多数Swing组件都继承了它。More tool tip API can be found in individual classes such as 更多工具提示API可以在个别类中找到,如JTabbedPane
. JTabbedPane
。In general, those APIs are sufficient for specifying and displaying tool tips; you usually do not need to deal directly with the implementing classes 通常,这些API足以指定和显示工具提示;通常不需要直接处理实现类JToolTip
and ToolTipManager
.JToolTip
和ToolTipManager
。
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.有关单个组件对工具提示的支持的信息,请参阅相关组件的“操作”部分。
setToolTipText(String) |
|
String getToolTipText() |
setToolTipText .setToolTipText 指定的字符串。 |
String getToolTipText(MouseEvent) |
getToolTipText() . getToolTipText() 返回的值相同的值。JTabbedPane , JTable , and JTree override this method to return a string associated with the mouse event location. JTabbedPane 、JTable 和JTree )重写此方法以返回与鼠标事件位置关联的字符串。 |
Point getToolTipLocation(MouseEvent) |
This table lists some examples that use tool tips and points to where those examples are described.下表列出了一些使用工具提示的示例,并指出了这些示例的描述位置。
ButtonDemo |
||
IconDemo |
||
TabbedPaneDemo |
addTab method.addTab 方法参数中指定的特定于选项卡的工具提示文本。 | |
TableRenderDemo |
||
TableToolTipsDemo |
||
TreeIconDemo2 |
||
ActionDemo |
Action s.Action 创建的按钮添加工具提示。 |