java - Java 字体枚举

标签 java enums

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

相关文章:

java - 将 java servlet 作为独立进程运行

mysql - 使用 hibernate 将枚举存储在数据库中

php - MySQL 查询获取枚举值并将其写入下拉列表

java - 使用循环枚举枚举实例

.net - 将 Nullable Enum 设置为 $null - 这真的会导致 PSInvalidCastException 吗?

java - AS/400 命令调用 - JT400 (Java)

java - Hashmap put(),是不是一直在排序?

java - 动态类加载器

java - 条件 Spring 安全

具有枚举类类型的 C++ 方法