简体   繁体   English

如何在 Java 中使用自定义字体?

[英]How can I use a custom font in Java?

I wrote a program in Java that uses a special font that by default doesn't exist on any operating system.我在 Java 中编写了一个程序,它使用一种默认情况下在任何操作系统上都不存在的特殊字体。

Is it possible in Java to add this special font to the operation system? Java是否可以在操作系统中添加这种特殊字体? For example, in Windows, to copy this font to the special Fonts folder.比如Windows,把这个字体复制到Fonts这个特殊的文件夹中。

If it is possible, how?如果可能的话,怎么做?

If you include a font file (otf, ttf, etc.) in your package, you can use the font in your application via the method described here: 如果您在程序包中包含字体文件(otf,ttf等),则可以通过此处描述的方法在应用程序中使用该字体:

Oracle Java SE 6: java.awt.Font Oracle Java SE 6:java.awt.Font

There is a tutorial available from Oracle that shows this example: Oracle 提供了一个显示该示例的教程

try {
     GraphicsEnvironment ge = 
         GraphicsEnvironment.getLocalGraphicsEnvironment();
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));
} catch (IOException|FontFormatException e) {
     //Handle exception
}

I would probably wrap this up in some sort of resource loader though as to not reload the file from the package every time you want to use it. 我可能会将其包装在某种资源加载器中,尽管这样每次使用时都不会从包中重新加载文件。

An answer more closely related to your original question would be to install the font as part of your application's installation process. 与您的原始问题更紧密相关的答案是在应用程序安装过程中安装字体。 That process will depend on the installation method you choose. 该过程将取决于您选择的安装方法。 If it's not a desktop app you'll have to look into the links provided. 如果不是桌面应用程序,则必须查看提供的链接。

Here is how I did it! 这是我的方法!

//create the font

try {
    //create the font to use. Specify the size!
    Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("Fonts\\custom_font.ttf")).deriveFont(12f);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //register the font
    ge.registerFont(customFont);
} catch (IOException e) {
    e.printStackTrace();
} catch(FontFormatException e) {
    e.printStackTrace();
}

//use the font
yourSwingComponent.setFont(customFont);

From the Java tutorial , you need to create a new font and register it in the graphics environment: Java教程中 ,您需要创建一个新字体并将其注册到图形环境中:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));

After this step is done, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors. 完成此步骤后,该字体在对getAvailableFontFamilyNames()调用中可用,并且可以在字体构造函数中使用。

If you want to use the font to draw with graphics2d or similar, this works: 如果要使用该字体与graphics2d或类似图形一起绘制,则可以这样做:

InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream("roboto-bold.ttf")
Font font = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(48f)

Configuration:配置:

final GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
final List<String> AVAILABLE_FONT_FAMILY_NAMES = Arrays.asList(GE.getAvailableFontFamilyNames());
try {
    final List<File> LIST = Arrays.asList(
        new File("font/JetBrainsMono/JetBrainsMono-Thin.ttf"),
        new File("font/JetBrainsMono/JetBrainsMono-Light.ttf"),
        new File("font/Roboto/Roboto-Light.ttf"),
        new File("font/Roboto/Roboto-Regular.ttf"),
        new File("font/Roboto/Roboto-Medium.ttf")
     );
     for (File LIST_ITEM : LIST) {
         if (LIST_ITEM.exists()) {
             Font FONT = Font.createFont(Font.TRUETYPE_FONT, LIST_ITEM);
             if (!AVAILABLE_FONT_FAMILY_NAMES.contains(FONT.getFontName())){ 
                 GE.registerFont(FONT);
             }
         }
     }
} catch (FontFormatException | IOException exception) {
    JOptionPane.showMessageDialog(null, exception.getMessage());
}

Usage example:使用示例:

JLabel label1 = new JLabel("TEXT1");
label1.setFont(new Font("Roboto Medium", Font.PLAIN, 13));

JLabel label2 = new JLabel("TEXT2");
label2.setFont(new Font("Roboto", Font.PLAIN, 13));

Note : Only existing fonts that are not available in the system are registered.注意:仅注册系统中不可用的现有 fonts。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM