texticons.java

来自「java class文件介绍」· Java 代码 · 共 62 行

JAVA
62
字号
/* (swing1.1) */
package zb.swing.icons;

import java.util.*;

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
import javax.swing.plaf.metal.MetalIconFactory.*;

/**
 * @version 1.0 01/12/99
 */
public class TextIcons
    extends MetalIconFactory.TreeLeafIcon {

  protected String label;
  private static Hashtable labels;

  protected TextIcons() {
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    super.paintIcon(c, g, x, y);
    if (label != null) {
      FontMetrics fm = g.getFontMetrics();

      int offsetX = (getIconWidth() - fm.stringWidth(label)) / 2;
      int offsetY = (getIconHeight() - fm.getHeight()) / 2 - 2;

      g.drawString(label, x + offsetX,
                   y + offsetY + fm.getHeight());
    }
  }

  public static Icon getIcon(String str) {
    if (labels == null) {
      labels = new Hashtable();
      setDefaultSet();
    }
    TextIcons icon = new TextIcons();
    icon.label = (String) labels.get(str);
    return icon;
  }

  public static void setLabelSet(String ext, String label) {
    if (labels == null) {
      labels = new Hashtable();
      setDefaultSet();
    }
    labels.put(ext, label);
  }

  private static void setDefaultSet() {
    labels.put("c", "C");
    labels.put("java", "J");
    labels.put("html", "H");
    labels.put("htm", "H");
  }

}

⌨️ 快捷键说明

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