⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comboboxrenderer.java

📁 用图形用户界面表示的图
💻 JAVA
字号:
package hartech.kids.grapher;

import java.awt.*;
import javax.swing.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Website: www.hartech.cn </p>
 * <p>Page: http://www.hartech.cn/blog/blogview.asp?logID=88 </p>
 *
 * <p>Date: 2006-09-09 </p>
 */

// 该类为了显示图标下拉菜单选项
public class ComboBoxRenderer
    extends JLabel implements ListCellRenderer {
  private Font uhOhFont;
  private ImageIcon[] images;

  public ComboBoxRenderer() {
    setOpaque(true);
    setHorizontalAlignment(CENTER);
    setVerticalAlignment(CENTER);

    images = new ImageIcon[Main.icon_num];
    for (int i = 0; i < Main.icon_num; i++) {
      images[i] = createImageIcon("images/smiles/" + i + ".gif");
    }
  }

  static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Main.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    }
    else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }

  /*
   * This method finds the image and text corresponding
   * to the selected value and returns the label, set up
   * to display the text and image.
   */
  public Component getListCellRendererComponent(
      JList list,
      Object value,
      int index,
      boolean isSelected,
      boolean cellHasFocus) {
    //Get the selected index. (The index param isn't
    //always valid, so just use the value.)
    int selectedIndex = ( (Integer) value).intValue();

    if (isSelected) {
      setBackground(list.getSelectionBackground());
      setForeground(list.getSelectionForeground());
    }
    else {
      setBackground(list.getBackground());
      setForeground(list.getForeground());
    }

    //Set the icon and text.  If icon was null, say so.
    ImageIcon icon = images[selectedIndex];
    setIcon(icon);

    return this;
  }

  //Set the font and text when no image was found.
  protected void setUhOhText(String uhOhText, Font normalFont) {
    if (uhOhFont == null) { //lazily create this font
      uhOhFont = normalFont.deriveFont(Font.ITALIC);
    }
    setFont(uhOhFont);
    setText(uhOhText);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -