import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ComboBoxDemo extends JPanel {
    JLabel picture;    public ComboBoxDemo() {
        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };        // Create the combo box, select the pig
        JComboBox petList = new JComboBox(petStrings);
        petList.setSelectedIndex(4);
        petList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                String petName = (String)cb.getSelectedItem();
                picture.setIcon(new ImageIcon("images/" + petName + ".gif"));
            }
        });        // Set up the picture
        picture = new JLabel(new ImageIcon("images/" +
                                   petStrings[petList.getSelectedIndex()] +
                                   ".gif"));
        picture.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));        // The preferred size is hard-coded to be the width of the
        // widest image and the height of the tallest image + the border.
        // A real program would compute this.
        picture.setPreferredSize(new Dimension(177, 122+10));        // Layout the demo
        setLayout(new BorderLayout());
        add(petList, BorderLayout.NORTH);
        add(picture, BorderLayout.SOUTH);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }    public static void main(String s[]) {
        JFrame frame = new JFrame("ComboBoxDemo");        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
 
        frame.setContentPane(new ComboBoxDemo());
        frame.pack();
        frame.setVisible(true);
    }
}

解决方案 »

  1.   

    String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };这5个图片你随便找5个图标就是了
    注意路径
      

  2.   

    靠给你一个做好的,看懂了你以后就会用了/**
     * File PropertyManager.java
     * Date 2003-1-20
     * Time 13:55:58
     * 
     */
    package org.jfml.util;import java.io.*;
    import java.util.*;public class PropertyManager
    {
    private static final String PROPERTIES_FILE_PATH =
    "D:\\Eclipse 2.1\\workspace\\OOC\\config\\enviroment\\client.properties";
    private static PropertyManager propertyManager = new PropertyManager(); private Properties properties; public static PropertyManager getInstance()
    {
    if (null == propertyManager)
    {
    propertyManager = new PropertyManager();
    } return propertyManager;
    } private PropertyManager()
    {
    this.properties = new Properties(); try
    {
    this.load(PROPERTIES_FILE_PATH);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    } private void load(InputStream inStream) throws IOException
    {
    properties.load(inStream);
    } private void load(String path) throws IOException
    {
    this.load(new FileInputStream(path));
    } public String getProperty(String key)
    {
    return properties.getProperty(key);
    } public boolean getBooleanProperty(String key)
    {
    String value = this.getProperty(key); if (null == value)
    {
    return false;
    }
    else
    {
    if (value.equalsIgnoreCase("true"))
    {
    return true;
    }
    } return false;
    } public Enumeration propertyNames()
    {
    return properties.propertyNames();
    } public static void main(String[] args)
    {
    PropertyManager pm = PropertyManager.getInstance(); try
    {
    //pm.load("D:\\Eclipse 2.1\\workspace\\OOC\\config\\enviroment\\test.properties");
    System.out.println(Boolean.valueOf(pm.getBooleanProperty("Chinese")));
    System.out.println(pm.getProperty("Chinese"));
    }
    catch (Exception e)
    {
    // XXX: handle exception
    }
    }
    }