📄 tooltip.html
字号:
<div class="linkBHEAD"><a href="internalframe.html">How to Use Internal Frames</a></div><div class="linkBHEAD"><a href="label.html">How to Use Labels</a></div><div class="linkBHEAD"><a href="layeredpane.html">How to Use Layered Panes</a></div><div class="linkBHEAD"><a href="list.html">How to Use Lists</a></div><div class="linkBHEAD"><a href="menu.html">How to Use Menus</a></div><div class="linkBHEAD"><a href="panel.html">How to Use Panels</a></div><div class="linkBHEAD"><a href="passwordfield.html">How to Use Password Fields</a></div><div class="linkBHEAD"><a href="progress.html">How to Use Progress Bars</a></div><div class="linkBHEAD"><a href="rootpane.html">How to Use Root Panes</a></div><div class="linkBHEAD"><a href="scrollpane.html">How to Use Scroll Panes</a></div><div class="linkBHEAD"><a href="separator.html">How to Use Separators</a></div><div class="linkBHEAD"><a href="slider.html">How to Use Sliders</a></div><div class="linkBHEAD"><a href="spinner.html">How to Use Spinners</a></div><div class="linkBHEAD"><a href="splitpane.html">How to Use Split Panes</a></div><div class="linkBHEAD"><a href="tabbedpane.html">How to Use Tabbed Panes</a></div><div class="linkBHEAD"><a href="table.html">How to Use Tables</a></div><div class="linkBHEAD"><a href="textarea.html">How to Use Text Areas</a></div><div class="linkBHEAD"><a href="textfield.html">How to Use Text Fields</a></div><div class="linkBHEAD"><a href="toolbar.html">How to Use Tool Bars</a></div><div class="nolinkBHEAD">How to Use Tool Tips</div><div class="linkBHEAD"><a href="tree.html">How to Use Trees</a></div><div class="linkAHEAD"><a href="icon.html">How to Use Icons</a></div><div class="linkAHEAD"><a href="border.html">How to Use Borders</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Component Problems</a></div></div> </div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Using Swing Components</a> </span> <div class=NavBit> <a target=_top href=toolbar.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=tree.html>Next »</a> </div> <div id=PageTitle>How to Use Tool Tips</div> <blockquote>Creating a tool tip for any <code>JComponent</code>is easy.You just use the <code>setToolTipText</code> methodto set up a tool tip for the component.For example, to add tool tips to three buttons,you add only three lines of code:<blockquote><pre>b1.setToolTipText("Click this button to disable the middle button.");b2.setToolTipText("This middle button does nothing when you click it.");b3.setToolTipText("Click this button to enable the middle button.");</pre></blockquote>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 <code>ButtonDemo</code> example,which is explained in<a href="button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>.Here's a picture of the tool tip that appears when the cursor pauses over the left buttonin <code>ButtonDemo</code>:<p><center><IMG SRC="../../figures/uiswing/components/ButtonDemo-TT.png" WIDTH="489" HEIGHT="96" ALIGN="BOTTOM" ALT="ButtonDemo showing a tool tip."></center></p><p>For componentssuch as tabbed panesthat have multiple parts,it often makes sense to vary the tool-tip textto reflect the part of the componentunder the cursor.For example,a tabbed pane might use this featureto 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 tothe <code>addTab</code> or <code>setToolTipTextAt</code> method.<p>Even in components that have no API for setting part-specific tool-tip text,you can generally do the job yourself.If the component supports renderers,then you can set the tool tip text on a custom renderer.You can find examples of doing this in the<a href="table.html">table</a> and<a href="tree.html">tree</a> sections.An alternative that works for all <code>JComponent</code>sis creating a subclass of the componentand overriding its<code>getToolTipText(MouseEvent)</code> method.</blockquote></blockquote><h3><a name="api">The Tool Tip API</a></h3><blockquote>Most of the API you need to set up tool tipsis in the <code>JComponent</code> class,and thus is inherited by most Swing components.More tool-tip API is in individual classessuch as <code>JTabbedPane</code>.In general, those APIs are sufficientfor specifying and displaying tool tips;you usually don't need to deal directly with the implementing classes,<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JToolTip.html"><code>JToolTip</code></a> and<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ToolTipManager.html"><code>ToolTipManager</code></a>.<p>The following table lists the <code>JComponent</code>tool tip API.For information on individual components' supportfor tool tips,see the how-to section for the component in question.<p><table border=1><caption><a name="JComponent">Tool Tip API in <code>JComponent</code></a></caption><tr><th>Method</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#setToolTipText(java.lang.String)">setToolTipText(String)</a> </td> <td>If the specified string is non-null, then this method registers the component as having a tool tip and makes the tool tip (when displayed) have the specified text. If the argument is null, then this method turns off tool tips for this component. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#getToolTipText()">String getToolTipText()</a> </td> <td>Returns the string that was previously specified with <code>setToolTipText</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#getToolTipText(java.awt.event.MouseEvent)">String getToolTipText(MouseEvent)</a> </td> <td>By default, returns the same value returned by <code>getToolTipText()</code>. Multi-part components such as <a href="tabbedpane.html"><code>JTabbedPane</code></a>, <a href="table.html"><code>JTable</code></a>, and <a href="tree.html"><code>JTree</code></a> override this method to return a string associated with the mouse event location. For example, each tab in a tabbed pane can have different tool-tip text. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#getToolTipLocation(java.awt.event.MouseEvent)">Point getToolTipLocation(MouseEvent)</a> </td> <td>Get the location (in the receiving component's coordinate system) where the upper left corner of the component's tool tip will appear. 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. </td> </tr></table><p></blockquote><h3><a name="eg">Examples that Use Tool Tips</a></h3><blockquote>This table shows some examples that use tool tipsand where those examples are described.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td> <a href="examples/index.html#ButtonDemo"><code>ButtonDemo</code></a></td><td> This section and <a href="button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></td><td> Uses a tool tip to provide instructions for a button.</td></tr><tr><td> <a href="../components/examples/index.html#IconDemoApplet"> <code>IconDemoApplet</code></a></td><td> <a class="TutorialLink" target="_top" href="../components/icon.html">How to Use Icons</a></td><td> Uses a tool tip in a label to provide name and size information for an image.</td></tr><tr><td> <a href="examples/index.html#TabbedPaneDemo"> <code>TabbedPaneDemo</code></a></td><td> <a href="tabbedpane.html">How to Use Tabbed Panes</a></td><td> Uses an argument to the <code>addTab</code> method to specify tool tip text for each tab.</td></tr><tr><td> <a href="examples/index.html#TableRenderDemo"> <code>TableRenderDemo</code></a></td><td> <a href="table.html#celltooltip">Specifying Tool Tips for Cells</a></td><td> Adds tool tips to a table using a renderer. </td></tr><tr><td> <a href="examples/index.html#TableToolTipsDemo"> <code>TableToolTipsDemo</code></a></td><td> <a href="table.html#celltooltip">Specifying Tool Tips for Cells</a>, <a href="table.html#headertooltip">Specifying Tool Tips for Column Headers</a></td><td> Adds tool tips to a table using various techniques. </td></tr><tr><td> <a href="examples/index.html#TreeIconDemo2"> <code>TreeIconDemo2</code></a></td><td> <a href="tree.html#display">Customizing a Tree's Display</a></td><td> Adds tool tips to a tree using a custom renderer. </td></tr><tr><td> <a href="../misc/examples/index.html#ActionDemo"> <code>ActionDemo</code></a></td><td> <a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a></td><td> Adds tool tips to buttons created using <code>Action</code>s.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=toolbar.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=tree.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Use Tool Bars <br><b>Next page:</b> How to Use Trees </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -