jb-->help: An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL or filename are preloaded using MediaTracker to monitor the loaded state of the imageMany Swing components, such as labels, buttons, and tabbed panes, can be decorated with an icon — a fixed-sized picture. An icon is an object that adheres to the Icon interface. Swing provides a particularly useful implementation of the Icon interface: ImageIcon, which paints an icon from a GIF, JPEG, or (as of 1.3) PNG image. sample:ImageIcon icon = createImageIcon("images/middle.gif", "a pretty but meaningless splat"); label1 = new JLabel("Image and Text", icon, JLabel.CENTER); ... label3 = new JLabel(icon); 自己试一试
这是jb中一个完整的文件: /*The code that follows creates the four arrow image icons and attaches them to the two buttons. */ import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.Vector; import java.util.StringTokenizer;/* * IconDemoApplet.java requires the following files: * SwingWorker.java * Photo.java * images/right.gif * images/left.gif * images/dimmedRight.gif * images/dimmedLeft.gif * * An applet tag for using it is in * http://java.sun.com/docs/books/tutorial/uiswing/misc/IconDemoApplet.atag * It refers to the following files: * images/stickerface.gif * images/lainesTongue.gif * images/kathyCosmo.gif * images/ewanPumpkin.gif */ public class IconDemoApplet extends JApplet implements ActionListener { Vector pictures; JButton previousButton; JButton nextButton; JLabel photographLabel; JLabel captionLabel; JLabel numberLabel; int current = 0; int widthOfWidest = 0; int heightOfTallest = 0; String imagedir = null; public void init() { //Parse the applet parameters. pictures = parseParameters(); //If the applet tag doesn't provide an "IMAGE0" parameter, //display an error message. if (pictures.size() == 0) { captionLabel = new JLabel("No images listed in applet tag."); captionLabel.setHorizontalAlignment(JLabel.CENTER); getContentPane().add(captionLabel); return; } //NOW CREATE THE GUI COMPONENTS //A label to identify XX of XX. numberLabel = new JLabel("Picture " + (current+1) + " of " + pictures.size()); numberLabel.setHorizontalAlignment(JLabel.LEFT); numberLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5)); //A label for the caption. final Photo first = (Photo)pictures.firstElement(); captionLabel = new JLabel(first.caption); captionLabel.setHorizontalAlignment(JLabel.CENTER); captionLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); //A label for displaying the photographs. photographLabel = new JLabel("Loading first image..."); photographLabel.setHorizontalAlignment(JLabel.CENTER); photographLabel.setVerticalAlignment(JLabel.CENTER); photographLabel.setVerticalTextPosition(JLabel.CENTER); photographLabel.setHorizontalTextPosition(JLabel.CENTER); photographLabel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLoweredBevelBorder(), BorderFactory.createEmptyBorder(5, 5, 5, 5) )); photographLabel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(0, 0, 10, 0), photographLabel.getBorder() ));
接上: //Set the preferred size for the picture, //with room for the borders. Insets i = photographLabel.getInsets(); photographLabel.setPreferredSize(new Dimension( widthOfWidest+i.left+i.right, heightOfTallest+i.bottom+i.top)); //Create the next and previous buttons. ImageIcon nextIcon = createAppletImageIcon("images/right.gif", "a right arrow"); ImageIcon dimmedNextIcon = createAppletImageIcon("images/dimmedRight.gif", "a dimmed right arrow"); ImageIcon previousIcon = createAppletImageIcon("images/left.gif", "a left arrow"); ImageIcon dimmedPreviousIcon = createAppletImageIcon("images/dimmedLeft.gif", "a dimmed left arrow"); nextButton = new JButton("Next Picture", nextIcon); nextButton.setDisabledIcon(dimmedNextIcon); nextButton.setVerticalTextPosition(AbstractButton.CENTER); nextButton.setHorizontalTextPosition(AbstractButton.LEFT); nextButton.setMnemonic(KeyEvent.VK_N); nextButton.setActionCommand("next"); nextButton.addActionListener(this); previousButton = new JButton("Previous Picture", previousIcon); previousButton.setDisabledIcon(dimmedPreviousIcon); previousButton.setVerticalTextPosition(AbstractButton.CENTER); previousButton.setHorizontalTextPosition(AbstractButton.RIGHT); previousButton.setMnemonic(KeyEvent.VK_P); previousButton.setActionCommand("previous"); previousButton.addActionListener(this); previousButton.setEnabled(false); //Lay out the GUI. GridBagLayout layout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); Container contentPane = getContentPane(); contentPane.setLayout(layout); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(numberLabel, c); contentPane.add(numberLabel); layout.setConstraints(captionLabel, c); contentPane.add(captionLabel); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; layout.setConstraints(photographLabel, c); contentPane.add(photographLabel); c.gridwidth = GridBagConstraints.RELATIVE; c.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(previousButton, c); contentPane.add(previousButton); c.gridwidth = GridBagConstraints.REMAINDER; layout.setConstraints(nextButton, c); contentPane.add(nextButton); //Start loading the image for the first photograph now. //The loadImage method uses a SwingWorker //to load the image in a separate thread. loadImage(imagedir + first.filename, current); } //User clicked either the next or the previous button. public void actionPerformed(ActionEvent e) { //Show loading message. photographLabel.setIcon(null); photographLabel.setText("Loading image..."); //Compute index of photograph to view. if (e.getActionCommand().equals("next")) { current += 1; if (!previousButton.isEnabled()) previousButton.setEnabled(true); if (current == pictures.size() - 1) nextButton.setEnabled(false); } else { current -= 1; if (!nextButton.isEnabled()) nextButton.setEnabled(true); if (current == 0) previousButton.setEnabled(false); } //Get the photo object. Photo pic = (Photo)pictures.elementAt(current); //Update the caption and number labels. captionLabel.setText(pic.caption); numberLabel.setText("Picture " + (current+1) + " of " + pictures.size()); //Update the photograph. ImageIcon icon = pic.getIcon(); if (icon == null) { //haven't viewed this photo before loadImage(imagedir + pic.filename, current); } else { updatePhotograph(current, pic); } }
An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL or filename are preloaded using MediaTracker to monitor the loaded state of the imageMany Swing components, such as labels, buttons, and tabbed panes, can be decorated with an icon — a fixed-sized picture. An icon is an object that adheres to the Icon interface. Swing provides a particularly useful implementation of the Icon interface: ImageIcon, which paints an icon from a GIF, JPEG, or (as of 1.3) PNG image. sample:ImageIcon icon = createImageIcon("images/middle.gif",
"a pretty but meaningless splat");
label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
...
label3 = new JLabel(icon);
自己试一试
/*The code that follows creates the four arrow image icons and attaches them to the two buttons. */
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Vector;
import java.util.StringTokenizer;/*
* IconDemoApplet.java requires the following files:
* SwingWorker.java
* Photo.java
* images/right.gif
* images/left.gif
* images/dimmedRight.gif
* images/dimmedLeft.gif
*
* An applet tag for using it is in
* http://java.sun.com/docs/books/tutorial/uiswing/misc/IconDemoApplet.atag
* It refers to the following files:
* images/stickerface.gif
* images/lainesTongue.gif
* images/kathyCosmo.gif
* images/ewanPumpkin.gif
*/
public class IconDemoApplet extends JApplet
implements ActionListener {
Vector pictures; JButton previousButton;
JButton nextButton;
JLabel photographLabel;
JLabel captionLabel;
JLabel numberLabel; int current = 0;
int widthOfWidest = 0;
int heightOfTallest = 0; String imagedir = null; public void init() {
//Parse the applet parameters.
pictures = parseParameters(); //If the applet tag doesn't provide an "IMAGE0" parameter,
//display an error message.
if (pictures.size() == 0) {
captionLabel = new JLabel("No images listed in applet tag.");
captionLabel.setHorizontalAlignment(JLabel.CENTER);
getContentPane().add(captionLabel);
return;
} //NOW CREATE THE GUI COMPONENTS //A label to identify XX of XX.
numberLabel = new JLabel("Picture " + (current+1) +
" of " + pictures.size());
numberLabel.setHorizontalAlignment(JLabel.LEFT);
numberLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5)); //A label for the caption.
final Photo first = (Photo)pictures.firstElement();
captionLabel = new JLabel(first.caption);
captionLabel.setHorizontalAlignment(JLabel.CENTER);
captionLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); //A label for displaying the photographs.
photographLabel = new JLabel("Loading first image...");
photographLabel.setHorizontalAlignment(JLabel.CENTER);
photographLabel.setVerticalAlignment(JLabel.CENTER);
photographLabel.setVerticalTextPosition(JLabel.CENTER);
photographLabel.setHorizontalTextPosition(JLabel.CENTER);
photographLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(),
BorderFactory.createEmptyBorder(5, 5, 5, 5)
));
photographLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0),
photographLabel.getBorder()
));
//Set the preferred size for the picture,
//with room for the borders.
Insets i = photographLabel.getInsets();
photographLabel.setPreferredSize(new Dimension(
widthOfWidest+i.left+i.right,
heightOfTallest+i.bottom+i.top)); //Create the next and previous buttons.
ImageIcon nextIcon = createAppletImageIcon("images/right.gif",
"a right arrow");
ImageIcon dimmedNextIcon = createAppletImageIcon("images/dimmedRight.gif",
"a dimmed right arrow");
ImageIcon previousIcon = createAppletImageIcon("images/left.gif",
"a left arrow");
ImageIcon dimmedPreviousIcon = createAppletImageIcon("images/dimmedLeft.gif",
"a dimmed left arrow"); nextButton = new JButton("Next Picture", nextIcon);
nextButton.setDisabledIcon(dimmedNextIcon);
nextButton.setVerticalTextPosition(AbstractButton.CENTER);
nextButton.setHorizontalTextPosition(AbstractButton.LEFT);
nextButton.setMnemonic(KeyEvent.VK_N);
nextButton.setActionCommand("next");
nextButton.addActionListener(this); previousButton = new JButton("Previous Picture", previousIcon);
previousButton.setDisabledIcon(dimmedPreviousIcon);
previousButton.setVerticalTextPosition(AbstractButton.CENTER);
previousButton.setHorizontalTextPosition(AbstractButton.RIGHT);
previousButton.setMnemonic(KeyEvent.VK_P);
previousButton.setActionCommand("previous");
previousButton.addActionListener(this);
previousButton.setEnabled(false); //Lay out the GUI.
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints(); Container contentPane = getContentPane();
contentPane.setLayout(layout); c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(numberLabel, c);
contentPane.add(numberLabel); layout.setConstraints(captionLabel, c);
contentPane.add(captionLabel); c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
layout.setConstraints(photographLabel, c);
contentPane.add(photographLabel); c.gridwidth = GridBagConstraints.RELATIVE;
c.fill = GridBagConstraints.HORIZONTAL;
layout.setConstraints(previousButton, c);
contentPane.add(previousButton); c.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(nextButton, c);
contentPane.add(nextButton); //Start loading the image for the first photograph now.
//The loadImage method uses a SwingWorker
//to load the image in a separate thread.
loadImage(imagedir + first.filename, current);
} //User clicked either the next or the previous button.
public void actionPerformed(ActionEvent e) {
//Show loading message.
photographLabel.setIcon(null);
photographLabel.setText("Loading image..."); //Compute index of photograph to view.
if (e.getActionCommand().equals("next")) {
current += 1;
if (!previousButton.isEnabled())
previousButton.setEnabled(true);
if (current == pictures.size() - 1)
nextButton.setEnabled(false);
} else {
current -= 1;
if (!nextButton.isEnabled())
nextButton.setEnabled(true);
if (current == 0)
previousButton.setEnabled(false);
} //Get the photo object.
Photo pic = (Photo)pictures.elementAt(current); //Update the caption and number labels.
captionLabel.setText(pic.caption);
numberLabel.setText("Picture " + (current+1) +
" of " + pictures.size()); //Update the photograph.
ImageIcon icon = pic.getIcon();
if (icon == null) { //haven't viewed this photo before
loadImage(imagedir + pic.filename, current);
} else {
updatePhotograph(current, pic);
}
}