作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Java 还很陌生,但是学得很快。目前,我正在创建的一个将文档写入 PDF 的程序使用了多种字体。我想将它们声明为枚举(它们当前位于名为 Fonts 的类文件中,并声明为 public static final ,效果很好),但即使在阅读后,我似乎也无法弄清楚如何声明每个特定枚举等于什么 Font有关枚举的几篇文档。我知道枚举是更明智的方法,因此如果可能的话更愿意实现它。
确实经过一些指导。
我的字体类文件如下。
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.CMYKColor;
public class Fonts {
public static final Font REG16 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 16));
public static final Font REG13 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 13));
public static final Font BOLD13 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 13, Font.BOLD, BaseColor.BLACK));
public static final Font BOLD11 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 11, Font.BOLD, BaseColor.BLACK));
public static final Font BOLD10 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 10, Font.BOLD, BaseColor.BLACK));
public static final Font GREY11 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 11, BaseColor.GRAY));
public static final Font GREYBOLD10 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 10, Font.BOLDITALIC, BaseColor.GRAY));
public static final Font REG10 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 10));
public static final Font REG11 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 11));
public static final Font GREYBOLD17 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 17, Font.BOLDITALIC));
public static final Font WHITEBOLD38 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 38, Font.BOLD, BaseColor.WHITE));
public static final Font WHITEBOLD20 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 20, Font.BOLD, BaseColor.WHITE));
public static final Font WHITEBOLD10 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 10, Font.BOLD, BaseColor.WHITE)); // Declare fonts.
public static final Font BOLDITALIC11 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 11, Font.BOLDITALIC, BaseColor.BLACK));
public static final Font ORANGEBOLD12 = new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 12, Font.BOLDITALIC, new CMYKColor(0, 0.2f, 1f, 0)));
}
最佳答案
您可以为每个实例创建一个带有 Font
属性的enum
。
大致如下:
enum Fonts {
REG16(new Font(FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", 16))),
...
;
private Font f;
Fonts(Font f) {
this.f = f;
}
public Font getFont() {
return this.f;
}
}
关于java - Java 字体枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25283284/
我是一名优秀的程序员,十分优秀!