簡體   English   中英

在 java 上為可執行 JAR 文件添加自定義字體文件

[英]Add custom font file on java for executable JAR file

我正在嘗試將自定義字體添加到我的 jswing 應用程序中,但我找不到如何正確上傳 .ttf 文件,以便在創建可執行 jar 文件后可以使用它。

問題在這里, new File(getClass().getResourceAsStream("/"+"cs_regular.ttf"))

謝謝你!

try {
         GraphicsEnvironment ge = 
             GraphicsEnvironment.getLocalGraphicsEnvironment();
         ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(getClass().getResourceAsStream("/"+"cs_regular.ttf"))));
    } catch (FontFormatException|IOException e) {
         pr("Error_107");
    }

It is not quite easy to create java.io.File from stream, you can create Font from stream directly. 看看這個答案。 https://stackoverflow.com/a/40862139/10999348

這是我現在的工作代碼

InputStream stream =ClassLoader.getSystemClassLoader().getResourceAsStream("cs_regular.ttf");
    try {
        csfont = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(48f);
    } catch (FontFormatException|IOException e) {
        //handle exeption
    } 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(csfont);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM