⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 label.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
label3 = new JLabel(icon);</pre></blockquote>The code for the <code>createImageIcon</code> methodis similar to that used throughout this tutorial.You can find it explained in<a class="TutorialLink" target="_top" href="../components/icon.html">How to Use Icons</a>.<p>Often, a label describes another component.When this is true,you can improve your program's accessibilityby using the <code>setLabelFor</code> methodto identify the component the label describes.For example:<blockquote><pre>amountLabel.setLabelFor(amountField);</pre></blockquote>The preceding code,taken from the <code>FormattedTextFieldDemo</code> examplediscussed in<a href="formattedtextfield.html">How to Use Formatted Text Fields</a>,lets assistive technologies knowthat the label (<code>amountLabel</code>)provides information about the formatted text field (<code>amountField</code>).For more information about assistive technologies, see<a class="TutorialLink" target="_top" href="../misc/access.html">How to Support Assistive Technologies</a>.</blockquote><h3><a name="api">The Label API</a></h3><blockquote>The following tables list the commonly used<code>JLabel</code> constructors and methods.Other methods you're likely to callare defined by the    <code>Component</code>    and    <code>JComponent</code> classes.They include <code>setFont</code>,<code>setForeground</code>,<code>setBorder</code>,<code>setOpaque</code>, and<code>setBackground</code>.See <a href="jcomponent.html">The JComponent Class</a>for details.The API for using labels falls into three categories:<ul><li><a href="#contentsapi">Setting or Getting the Label's Contents</a><li><a href="#looksapi">Fine Tuning the Label's Appearance</a><li><a href="#accessapi">Supporting Accessibility</a></ul><blockquote><hr><strong>Note:</strong>&nbsp;In the following API, don't confuse label alignment withX and Y alignment.X and Y alignment are used by layout managersand can affect the way any component &#151; not just a label &#151;is sized or positioned.Label alignment, on the other hand,has no effect on a label's size or position.It simply determines where,inside the label's painting area,the label's contents are positioned.In the usual case,the label's painting area is exactly the size neededto paint the label,and thus label alignment is irrelevant.For more information about X and Y alignment, see<a class="TutorialLink" target="_top" href="../layout/box.html">How to Use BoxLayout</a>.<hr></blockquote><p><table border=1><caption><a name="contentsapi">Setting or Getting the Label's Contents</a></caption><tr><th>Method or Constructor</th><th>Purpose</th></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel(javax.swing.Icon)">JLabel(Icon)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel(javax.swing.Icon, int)">JLabel(Icon, int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel(java.lang.String)">JLabel(String)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel(java.lang.String, javax.swing.Icon, int)">JLabel(String, Icon, int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel(java.lang.String, int)">JLabel(String, int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel()">JLabel()</a>    </td>    <td>Create a <code>JLabel</code> instance,    initializing it to have the specified text/image/alignment.    The <code>int</code> argument    specifies the horizontal alignment    of the label's contents within its drawing area.    The horizontal alignment must be one of the following constants    defined in the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingConstants.html"><code>SwingConstants</code></a>    interface (which <code>JLabel</code> implements):    <code>LEFT</code>, <code>CENTER</code>, <code>RIGHT</code>,    <code>LEADING</code>, or <code>TRAILING</code>.    For ease of localization,    we strongly encourage you to use    <code>LEADING</code> and <code>TRAILING</code>,    rather than <code>LEFT</code> and <code>RIGHT</code>.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setText(java.lang.String)">void setText(String)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getText()">String getText()</a>    </td>    <td>Set or get the text displayed by the label.You can use HTML tags to format the text,as described in <a href="html.html">Using HTML in Swing Components</a>    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setIcon(javax.swing.Icon)">void setIcon(Icon)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getIcon()">Icon getIcon()</a>    </td>    <td>Set or get the image displayed by the label.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setDisplayedMnemonic(char)">void setDisplayedMnemonic(char)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getDisplayedMnemonic()">char getDisplayedMnemonic()</a>    </td>    <td>Set or get the letter        that should look like a keyboard alternative.        This is handy when a label describes a        component (such as a text field)        that has a keyboard alternative        but can't display it.    If the labelFor property is also set    (using <code>setLabelFor</code>),    then when the user activates the mnemonic,    the keyboard focus is transferred to the    component specified by the labelFor property.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setDisplayedMnemonicIndex(int)">void setDisplayedMnemonicIndex(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getDisplayedMnemonicIndex()">int getDisplayedMnemonicIndex()</a>    </td>    <td>Set or get a hint as to which character in the text should be decorated        to represent the mnemonic.  This is handy when you have two instances        of the same character and wish to decorate the second instance.        For example, <code>setDisplayedMnemonicIndex(5)</code>        decorates the character that's at position 5        (that is, the 6th character in the text).        Not all look and feels may support this feature.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setDisabledIcon(javax.swing.Icon)">void setDisabledIcon(Icon)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getDisabledIcon()">Icon getDisabledIcon()</a>    </td>    <td>Set or get the image displayed by the label        when it's disabled.        If you don't specify a disabled image,        then the look-and-feel creates one        by manipulating the default image.    </td>  </tr></table><p><table border=1><caption><a name="looksapi">Fine Tuning the Label's Appearance</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/JLabel.html#setHorizontalAlignment(int)">void setHorizontalAlignment(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setVerticalAlignment(int)">void setVerticalAlignment(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getHorizontalAlignment()">int getHorizontalAlignment()</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getVerticalAlignment()">int getVerticalAlignment()</a>    </td>    <td>Set or get where in the label its contents should be placed.    The<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingConstants.html"><code>SwingConstants</code></a>    interface defines five possible values for horizontal alignment:    <code>LEFT</code>,    <code>CENTER</code> (the default for image-only labels),    <code>RIGHT</code>,    <code>LEADING</code> (the default for text-only labels),    <code>TRAILING</code>.    For vertical alignment:    <code>TOP</code>,    <code>CENTER</code> (the default), and    <code>BOTTOM</code>.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setHorizontalTextPosition(int)">void setHorizontalTextPosition(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setVerticalTextPosition(int)">void setVerticalTextPosition(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getHorizontalTextPosition()">int getHorizontalTextPosition()</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getVerticalTextPosition()">int getVerticalTextPosition()</a></td><td>Set or get where the button's text should be placed,    relative to the button's image.    The<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingConstants.html"><code>SwingConstants</code></a>    interface defines five possible values    for horizontal position:    <code>LEADING</code>,    <code>LEFT</code>,    <code>CENTER</code>,     <code>RIGHT</code>, and     <code>TRAILING</code> (the default).    For vertical position:    <code>TOP</code>,    <code>CENTER</code> (the default), and    <code>BOTTOM</code>.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setIconTextGap(int)">void setIconTextGap(int)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getIconTextGap()">int getIconTextGap()</a>    </td>    <td>Set or get the number of pixels        between the label's text and its image.    </td>  </tr></table><p><table border=1><caption><a name="accessapi">Supporting Accessibility</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/JLabel.html#setLabelFor(java.awt.Component)">void setLabelFor(Component)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#getLabelFor()">Component getLabelFor()</a>    </td>    <td>Set or get which component the label describes.    </td>  </tr></table></blockquote><h3><a name="eg">Examples that Use Labels</a></h3><blockquote>The following table lists some of the manyexamples that use labels.<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#LabelDemo"><code>LabelDemo</code></a></td><td> This section</td><td> Shows how to specify horizontal and vertical alignment,     as well as aligning a label's text and image.</td></tr><tr><td> <a href="examples/index.html#HtmlDemo"><code>HtmlDemo</code></a></td><td> <a href="html.html">Using HTML in Swing Components</a></td><td> Lets you experiment with specifying HTML text     for a label.</td></tr><tr><td> <a href="../layout/examples/index.html#BoxAlignmentDemo"><code>BoxAlignmentDemo</code></a></td><td> <a href="../layout/box.html#alignment">Fixing Alignment Problems</a></td><td> Demonstrates a possible alignment problem     when using a label in a vertical box layout.     Shows how to solve the problem.</td></tr><tr><td> <a href="examples/index.html#DialogDemo"><code>DialogDemo</code></a></td><td> <a href="dialog.html">How to Use Dialogs</a></td><td> Uses a changeable label to display instructions     and provide feedback.</td></tr><tr><td> <a href="examples/index.html#SplitPaneDemo"><code>SplitPaneDemo</code></a></td><td> <a href="splitpane.html">How to Use Split Panes</a> and     <a href="list.html">How to Use Lists</a>.</td><td> Displays an image using a label inside of a scroll pane.</td></tr><tr><td> <a href="examples/index.html#SliderDemo2"><code>SliderDemo2</code></a></td><td> <a href="slider.html">How to Use Sliders</a></td><td> Uses <code>JLabel</code> to provide labels for a slider.</td></tr><tr><td> <a href="examples/index.html#TableDialogEditDemo"><code>TableDialogEditDemo</code></a></td><td> <a href="table.html">How to Use Tables</a></td><td> Implements a label subclass,     <code>ColorRenderer</code>,     to display colors in table cells.</td></tr><tr><td> <a href="examples/index.html#FormattedTextFieldDemo"><code>FormattedTextFieldDemo</code></a></td><td> <a href="formattedtextfield.html">How to Use Formatted Text Fields</a></td><td> Has four rows,     each containing a label and the formatted text field it describes.</td></tr><tr><td> <a href="examples/index.html#TextComponentDemo"><code>TextComponentDemo</code></a></td><td> <a href="generaltext.html">Text Component Features</a></td><td> <code>TextComponentDemo</code> has an inner class     (<code>CaretListenerLabel</code>)     that extends <code>JLabel</code>     to provide a label that listens for events,     updating itself based on the events.</td></tr><tr><td> <a href="examples/index.html#ColorChooserDemo"><code>ColorChooserDemo</code></a></td><td> <a href="colorchooser.html">How to Use Color Choosers</a></td><td> Uses an opaque label to display the currently chosen color     against a fixed-color background.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=internalframe.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=layeredpane.html>Next &raquo;</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 Internal Frames        <br><b>Next page:</b> How to Use Layered Panes    </div>    </body></html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -