您的位置:首页 > 编程语言 > Java开发

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

2015-12-03 15:01 627 查看
2. 定义获取省份的方法,创建一个Map集合,将上一步得到的映射集合赋值给它,使用Map集合的keySet()方法获取该集合中的所有键对象组成的Set 集合,即为省分集合,创建一个Object型一维数组,使用Set接口的toArray()方法将Set集合转换为数组,返回此数组作为省份选择下拉列表的参数。

  3. 使用JComboBox类的setModel()方法为省份下拉列表添加省份信息,参数即为上一步中的获取省份方法。

  4. 定义根据省份获取市/县的方法,创建一个Map集合,将步骤1中得到的映射集合赋值给它,使用Map集合的get()方法获取指定键的值,即为市/县集合,创建一个String[]型一维数组,将市/县集合赋值给该数组。

  5. 定义省份下拉列表的选项状态更改事件,在该事件中通过JComboBox类的getSelectedItem()方法获取选中的省份,默认为省份集合中的第一个值,然后使用JComboBox类的removeAllItems()方法清空市/县列表,根据选中的省份获取市/县数组,最后使用 JComboBox的setModel()方法重新添加市/县列表的值。

  代码如下:

  BackgroundPanel.java

  import java.awt.Graphics;

  import java.awt.Image;

  import javax.swing.JPanel;

  /**

  * 带背景的面板组件

  *

  * @author ZhongWei Lee

  */

  public class BackgroundPanel extends JPanel {

  /**

  *

  */

  private static final long serialVersionUID = 7758689434195492602L;

  /**

  * 背景图片

  */

  private Image image;

  /**

  * 构造方法

  */

  public BackgroundPanel() {

  super();

  setOpaque(false);

  setLayout(null);

  }

  /**

  * 设置图片的方法

  */

  public void setImage(Image image) {

  this.image = image;

  }

  @Override

  protected void paintComponent(Graphics g) {// 重写绘制组件外观

  if (image != null) {

  int width = getWidth();// 获取组件大小

  int height = getHeight();

  g.drawImage(image, 0, 0, width, height, this);// 绘制图片与组件大小相同

  }

  super.paintComponent(g);// 执行超类方法

  }

  }

  SwingResourceManager.java

  import java.awt.Image;

  import java.awt.Toolkit;

  import java.io.BufferedInputStream;

  import java.io.ByteArrayOutputStream;

  import java.io.FileInputStream;

  import java.io.IOException;

  import java.io.InputStream;

  import java.util.HashMap;

  import java.util.Iterator;

  import javax.swing.ImageIcon;

  /**

  * Utility class for managing resources such as colors, fonts, images, etc.

  *

  * This class may be freely distributed as part of any application or plugin.

  *

  * Copyright (c) 2003 - 2004, Instantiations, Inc.

  All Rights Reserved

  *

  * @author scheglov_ke

  */

  public class SwingResourceManager {

  /**

  * Maps image names to images

  */

  private static HashMap m_ClassImageMap = new HashMap();

  /**

  * Returns an image encoded by the specified input stream

  * @param is InputStream The input stream encoding the image data

  * @return Image The image encoded by the specified input stream

  */

  private static Image getImage(InputStream is) {

  try {

  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  byte buf[] = new byte[1024 * 4];

  while (true) {

  int n = is.read(buf);

  if (n == -1)

  break;

  baos.write(buf, 0, n);

  }

  baos.close();

  return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());

  } catch (Throwable e) {

  return null;

  }

  }

  /**

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

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

  * @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(Class clazz, String path) {

  String key = clazz.getName() + '|' + path;

  Image image = m_ClassImageMap.get(key);

  if (image == null) {

  if ((path.length() > 0) && (path.charAt(0) == '/')) {

  String newPath = path.substring(1, path.length());

  image = getImage(new BufferedInputStream(clazz.getClassLoader().getResourceAsStream(newPath)));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: