gpt4 book ai didi

java - Java 字体枚举

转载 作者:行者123 更新时间:2023-12-02 05:33:33 24 4
gpt4 key购买 nike

我对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com