日期:2014-05-20  浏览次数:20706 次

怎么通过int看枚举类型
System.out.println((int)Font.BOLD);这样能看Font的BOLD的值
那我要反过来看1对应的Font的值怎么看
System.out.println((Font)1)不行呀

------解决方案--------------------
这是thinking in java上的一个例子:

public final class Month {
private String name;
private Month(String nm) { name = nm; }
public String toString() { return name; }
public static final Month
JAN = new Month( "January "),
FEB = new Month( "February "),
MAR = new Month( "March "),
APR = new Month( "April "),
MAY = new Month( "May "),
JUN = new Month( "June "),
JUL = new Month( "July "),
AUG = new Month( "August "),
SEP = new Month( "September "),
OCT = new Month( "October "),
NOV = new Month( "November "),
DEC = new Month( "December ");
public static final Month[] month = {
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC
};
public static final Month number(int ord) {
return month[ord - 1];
}
public static void main(String[] args) {
Month m = Month.JAN;
System.out.println(m);
m = Month.number(12);
System.out.println(m);
System.out.println(m == Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
}
} ///:~
------解决方案--------------------
System.out.println((int)Font.BOLD);
这句是把Font.BOLD转型成int,而不是Font,
所以你那句System.out.println((Font)1)是不对的,要转也是转成BOLD的类型
------解决方案--------------------
看不到的
因为1不仅仅对应的是Font,而且也没有这样的转换

源码是这样的,你可以看到BOLD对应1,CENTER_BASELINE也对应1
你觉得1对应的是BOLD还是CENTER_BASELINE呢?

/**
* The plain style constant.
*/
public static final int PLAIN = 0;

/**
* The bold style constant. This can be combined with the other style
* constants (except PLAIN) for mixed styles.
*/
public static final int BOLD = 1;

/**
* The italicized style constant. This can be combined with the other
* style constants (except PLAIN) for mixed styles.
*/
public static final int ITALIC = 2;

/**
* The baseline used in most Roman scripts when laying out text.
*/
public static final int ROMAN_BASELINE = 0;

/**
* The baseline used in ideographic scripts like Chinese, Japanese,
* and Korean when laying out text.
*/
public static final int CENTER_BASELINE = 1;

/**
* The baseline used in Devanigiri and similar scripts when laying
* out text.
*/
public static final int HANGING_BASELINE = 2;


------解决方案--------------------
在font里加一个方法

public Font int2Font(int f){
return ...
}
------解决方案--------------------
首先得确认你的BOLD是一个一般常量还是一个枚举值,这是有区别的.
Enum类型是jdk5.0以后加入的新特性,下面有我的部分实例代码

package net.oicp.sunflowerbbs;


enum Operate {
push(1), pop(3),clear(5);
private int value;
Operate( int arg1) {
value=arg1;
}
public int getValue() {
return value;
}
public Operate intToOperate(int a)
{
Operate result=null;
for(Operate o:Operate.values())