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

📄 enumcombo.java

📁 这个是我老师给的关于Java核心技术2的第10章的源代码。可以看看。
💻 JAVA
字号:
/**   @version 1.12 2004-09-15   @author Cay Horstmann*/import java.util.*;import javax.swing.*;/**   A combo box that lets users choose from among static field   values whose names are given in the constructor.*/public class EnumCombo extends JComboBox{    /**      Constructs an EnumCombo.      @param cl a class      @param labels an array of static field names of cl   */   public EnumCombo(Class cl, String[] labels)   {        for (int i = 0; i < labels.length; i++)      {           String label = labels[i];         String name = label.toUpperCase().replace(' ', '_');         int value = 0;         try         {              java.lang.reflect.Field f = cl.getField(name);            value = f.getInt(cl);         }         catch (Exception e)         {              label = "(" + label + ")";         }         table.put(label, value);         addItem(label);      }      setSelectedItem(labels[0]);   }   /**      Returns the value of the field that the user selected.      @return the static field value   */   public int getValue()   {        return table.get(getSelectedItem());   }   private Map<String, Integer> table = new TreeMap<String, Integer>();}

⌨️ 快捷键说明

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