📄 icon.html
字号:
<IMG SRC="../../figures/uiswing/components/left.gif" WIDTH="20" HEIGHT="22" ALT="A simple left arrow"><IMG SRC="../../figures/uiswing/components/right.gif" WIDTH="20" HEIGHT="22" ALT="A simple right arrow"></blockquote>You can see a picture of <code>ButtonDemo</code> in<a class="TutorialLink" target="_top" href="../components/button.html#abstractbutton">How to Use the Common Button API</a>.Its source code is in<a class="SourceLink" target="_blank" href="../components/examples/ButtonDemo.java"><code>ButtonDemo.java</code></a>.<code>ButtonDemo</code> uses the following codeto load the arrows from GIF filesand put the arrows into buttons:<blockquote><pre>ImageIcon leftButtonIcon = createImageIcon("images/right.gif", "an arrow pointing right");...ImageIcon rightButtonIcon = createImageIcon("images/left.gif", "an arrow pointing left");b1 = new JButton("Disable middle button", leftButtonIcon);...b3 = new JButton("Enable middle button", rightButtonIcon);</pre></blockquote>Here is the new code,which uses a custom icon class named <code>ArrowIcon</code>.Only the bold lines have changed.You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/misc/examples/CustomIconDemo.jnlp">run CustomIconDemo</a> using<a href=http://java.sun.com/products/javawebstart>Java Web Start</a>.Or, to compile and run the example yourself,consult the<a href="examples/index.html#CustomIconDemo">example index</a>.<blockquote><pre><b>Icon leftButtonIcon = new ArrowIcon(SwingConstants.TRAILING);</b>...<b>Icon rightButtonIcon = new ArrowIcon(SwingConstants.LEADING);</b>b1 = new JButton("Disable middle button", leftButtonIcon);...b3 = new JButton("Enable middle button", rightButtonIcon);</pre></blockquote>You can find the implementation of the custom icon class in<a class="SourceLink" target="_blank" href="examples/ArrowIcon.java"><code>ArrowIcon.java</code></a>.Here are the interesting parts of its code:<blockquote><pre>class ArrowIcon implements Icon, SwingConstants { public ArrowIcon(int direction) { if (direction == LEADING) { xPoints[0] = width; yPoints[0] = -1; xPoints[1] = width; yPoints[1] = height; xPoints[2] = 0; yPoints[2] = height/2; xPoints[3] = 0; yPoints[3] = height/2 - 1; } else /* direction == TRAILING */ { xPoints[0] = 0; yPoints[0] = -1; xPoints[1] = 0; yPoints[1] = height; xPoints[2] = width; yPoints[2] = height/2; xPoints[3] = width; yPoints[3] = height/2 - 1; } } ... public void paintIcon(Component c, Graphics g, int x, int y) { if (c.isEnabled()) { g.setColor(c.setForeground()); } else { g.setColor(Color.gray); } g.translate(x, y); g.fillPolygon(xPoints, yPoints, xPoints.length); g.translate(-x, -y); //Restore Graphics object }}</pre></blockquote>Note that the icon sets the current color.If you don't do this, then the icon's painting might not be visible.For more information about painting, see<a class="TutorialLink" target="_top" href="../painting/index.html">Performing Custom Painting</a>.<p>Using a custom icon to paint the arrows has a few implications:<ul><li> Because the icon's appearance is determined dynamically, the icon painting code can use any information — component and application state, for example — to determine what to paint.<li> Because we specified a non-<code>ImageIcon</code> icon for a button, the button doesn't bother to calculate the dimmed (disabled) version of the icon. Instead, the button lets the icon paint its disabled self. This can reduce computation time and save space that would otherwise be used to hold the dimmed image.<li> Depending on the platform and the type of image, we might get a performance boost with custom icons, since painting simple shapes can sometimes be faster than copying images.<li> Instead of loading all the GIF files for the arrows (left and right, and perhaps dimmed left and dimmed right), we load a single class file (<code>ArrowIcon</code>). The performance implications of this depend on factors such as the platform, the size of the files, and the overhead for loading each type of file. </ul></blockquote><h3><a name=api>The Image Icon API</a></h3><blockquote>The following tables list the commonly used<code>ImageIcon</code> constructors and methods.Note that <code>ImageIcon</code> is not a descendentof <code>JComponent</code>or even of <code>Component</code>.<p>The API for using image icons falls into these categories:<ul><li><a href=#contents>Setting, Getting, and Painting the Image Icon's Image</a><li><a href=#info>Setting or Getting Information about the Image Icon</a><li><a href=#loadstatus>Watching the Image Icon's Image Load</a></ul><table border=1><caption><a name=contents>Setting, Getting, and Painting the Image Icon's Image</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/ImageIcon.html#ImageIcon()">ImageIcon()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(byte[])">ImageIcon(byte[])</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(byte[], java.lang.String)">ImageIcon(byte[], String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.awt.Image)">ImageIcon(Image)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.awt.Image, java.lang.String)">ImageIcon(Image, String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.lang.String)">ImageIcon(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.lang.String, java.lang.String)">ImageIcon(String, String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.net.URL)">ImageIcon(URL)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.net.URL, java.lang.String)">ImageIcon(URL, String)</a></td><td>Create an <code>ImageIcon</code> instance, initializing it to contain the specified image. The first argument indicates the source — image, byte array, filename, or URL — from which the image icon's image should be loaded. The source must be in a format supported by the <code>java.awt.Image</code> class: namely GIF, JPEG, or (as of 1.3) PNG. The second argument, when present, provides a description for the image. The description may also be set via <code>setDescription</code> and provides useful textual information for <a href=../misc/access.html>assistive technologies</a>.</tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#setImage(java.awt.Image)">void setImage(Image)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#getImage()">Image getImage()</a></td><td>Set or get the image displayed by the image icon.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#paintIcon(java.awt.Component, java.awt.Graphics, int, int)">void paintIcon(Component, Graphics, int, int)</a></td><td>Paint the image icon's image in the specified graphics context. You would override this only if you're implementing a custom icon that performs its own painting. The <code>Component</code> object is used as an image observer. You can rely on the default behavior provided by <code>Component</code> class, and pass in any component. The two <code>int</code> arguments specify the top-left corner where the icon is painted.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)">URL getResource(String)</a> <br>in (<em>java.lang.ClassLoader</em>)</td><td>Find the resource with the given name. For more information,see <a href=#getresource>Loading Images Using getResource</a>.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)">InputStream getResourceAsStream(String)</a> <br>in (<em>java.lang.ClassLoader</em>)</td><td>Find the resource with the given name and return an input streamfor reading the resource. For more information, see the<a href=#applet>Loading Images Into Applets</a> discussion.</td></tr></table><p><table border=1><caption><a name=info>Setting or Getting Information about the Image Icon</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/ImageIcon.html#setDescription(java.lang.String)">void setDescription(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#getDescription()">String getDescription()</a></td><td>Set or get a description of the image. This description is intended for use by <a href=../misc/access.html>assistive technologies</a>.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#getIconWidth()">int getIconWidth()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#getIconHeight()">int getIconHeight()</a></td><td>Get the width or height of the image icon in pixels.</td></tr></table><p><table border=1><caption><a name=loadstatus>Watching the Image Icon's Image Load</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/ImageIcon.html#setImageObserver(java.awt.image.ImageObserver)">void setImageObserver(ImageObserver)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#getImageObserver()">ImageObserver getImageObserver()</a></td><td>Set or get an image observer for the image icon.</td></tr><tr><td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ImageIcon.html#getImageLoadStatus()">int getImageLoadStatus()</a></td><td>Get the loading status of the image icon's image. The values returned by this method are defined by <code>MediaTracker</code>.</td></tr></table></blockquote><h3><a name=eg>Examples that Use Icons</a></h3><blockquote>The following table listsjust a few of the many examples that use <code>ImageIcon</code>.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td> <a href="../components/examples/index.html#LabelDemo"><code>LabelDemo</code></a></td><td> This section and<br> <a href="../components/label.html">How to Use Labels</a></td><td> Demonstrates using icons in an application's label, with and without accompanying text.</td></tr><tr><td> <a href="examples/index.html#IconDemoApplet"> <code>IconDemoApplet</code></a></td><td> This section</td><td> An applet. Uses a label to show large images; uses buttons that have both images and text.</td></tr><tr><td> <a href="examples/index.html#CustomIconDemo"> <code>CustomIconDemo</code></a></td><td> This section</td><td> Uses a custom icon class implemented by <a class="SourceLink" target="_blank" href="examples/ArrowIcon.java"><code>ArrowIcon.java</code></a>.</td></tr><tr><td> <a href="../components/examples/index.html#TumbleItem"> <code>TumbleItem</code></a></td><td> <a href="../components/applet.html">How to Make Applets</a></td><td> Uses image icons in an animation. Shows how to call <code>ImageIcon</code>'s <code>paintIcon</code> method.</td></tr><tr><td> <a href="../components/examples/index.html#ButtonDemo"><code>ButtonDemo</code></a></td><td> <a href="../components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></td><td> Shows how to use icons in an application's buttons.</td></tr><tr><td> <a href="../components/examples/index.html#CheckBoxDemo"><code>CheckBoxDemo</code></a></td><td> <a href="../components/button.html#checkbox">How to Use Check Boxes</a></td><td> Uses multiple GIF images.</td></tr><tr><td> <a href="../components/examples/index.html#TabbedPaneDemo"><code>TabbedPaneDemo</code></a></td><td> <a href="../components/tabbedpane.html">How to Use Tabbed Panes</a></td><td> Demonstrates adding icons to tabs in a tabbed pane.</td></tr><tr><td> <a href="../components/examples/index.html#DialogDemo"><code>DialogDemo</code></a></td><td> <a href="../components/dialog.html">How to Make Dialogs</a></td><td> Shows how to use standard icons in dialogs.</td></tr><tr><td> <a href="../components/examples/index.html#TreeIconDemo"><code>TreeIconDemo</code></a></td><td> <a href="../components/tree.html">How to Use Trees</a></td><td> Shows how to change the icons displayed by a tree's nodes.</td></tr><tr> <td> <a href="../misc/examples/index.html#ActionDemo"><code>ActionDemo</code></a></td><td> <a href="../misc/action.html">How to Use Actions</a></td><td> Shows how to specify the icon in a tool-bar button or menu item using an <code>Action</code>.</td></tr><tr> <td> <a href="../components/examples/index.html#FileChooserDemo2"><code>FileChooserDemo2</code></a></td><td> <a href="../components/filechooser.html">How to Use File Choosers</a></td><td> Uses a <code>PNG</code> image. Shows how to implement an image previewer and an image filter in a file chooser.</td></tr></table></blockquote> </blockquote> <div class=NavBit> <a target=_top href=tree.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=border.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 Trees <br><b>Next page:</b> How to Use Borders </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -