Java基础知识:Java实现Map集合二级联动3

* Returns an image stored in the file at the specified path

  * @param path String The path to the image file

  * @return Image The image stored in the file at the specified path

  */

  public static Image getImage(String path) {

  return getImage("default", path); //$NON-NLS-1$

  }

  /**

  * Returns an image stored in the file at the specified path

  * @param section String The storage section in the cache

  * @param path String The path to the image file

  * @return Image The image stored in the file at the specified path

  */

  public static Image getImage(String section, String path) {

  String key = section + '|' + SwingResourceManager.class.getName() + '|' + path;

  Image image = m_ClassImageMap.get(key);

  if (image == null) {

  try {

  FileInputStream fis = new FileInputStream(path);

  image = getImage(fis);

  m_ClassImageMap.put(key, image);

  fis.close();

  } catch (IOException e) {

  return null;

  }

  }

  return image;

  }

  /**

  * Clear cached images in specified section

  * @param section the section do clear

  */

  public static void clearImages(String section) {

  for (Iterator I = m_ClassImageMap.keySet().iterator(); I.hasNext();) {

  String key = I.next();

  if (!key.startsWith(section + '|'))

  continue;

  Image image = m_ClassImageMap.get(key);

  image.flush();

  I.remove();

  }

  }

  /**

  * Returns an icon stored in the file at the specified path relative to the specified class

  * @param clazz Class The class relative to which to find the icon

  * @param path String The path to the icon file

  * @return Icon The icon stored in the file at the specified path

  */

  public static ImageIcon getIcon(Class clazz, String path) {

  return getIcon(getImage(clazz, path));

  }

  /**

  * Returns an icon stored in the file at the specified path

  * @param path String The path to the icon file

  * @return Icon The icon stored in the file at the specified path

  */

  public static ImageIcon getIcon(String path) {

  return getIcon("default", path); //$NON-NLS-1$

  }

  /**

  * Returns an icon stored in the file at the specified path

  * @param section String The storage section in the cache

  * @param path String The path to the icon file

  * @return Icon The icon stored in the file at the specified path

  */

  public static ImageIcon getIcon(String section, String path) {

  return getIcon(getImage(section, path));

  }

  /**

  * Returns an icon based on the specified image

  * @param image Image The original image

  * @return Icon The icon based on the image

  */

  public static ImageIcon getIcon(Image image) {

  if (image == null)

  return null;

  return new ImageIcon(image);

  }

  }

  MainFrame.java

  import java.awt.EventQueue;

  import java.awt.event.ItemEvent;

  import java.awt.event.ItemListener;

  import java.util.Map;

  import java.util.Set;

  import javax.swing.DefaultComboBoxModel;

  import javax.swing.JButton;

  import javax.swing.JComboBox;

  import javax.swing.JFrame;

  import javax.swing.JLabel;

  import javax.swing.JPanel;

  import javax.swing.JTextField;

  import javax.swing.SwingConstants;

  import javax.swing.UIManager;

  import javax.swing.border.TitledBorder;

  public class MainFrame extends JFrame {

  /**

  *

  */

  private static final long serialVersionUID = -4595347311922711984L;

  private JTextField textField_3;

  private JTextField textField_1;

  private JComboBox comboBox_1;

  private JTextField textField;

  private JComboBox cityComboBox;

  private JComboBox comboBox;

  /**

  * Launch the application

  *

  * @param args

  */

  public static void main(String args[]) {

  EventQueue.invokeLater(new Runnable() {

  public void run() {

  try {

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

上一篇:转:Java反射教程


下一篇:java反射教程