【发布时间】:2022-01-04 12:58:22
【问题描述】:
我试图在 IntelliJ IDEA 中使用 java.io.InputStream 读取 .ttc 文件,但失败了。
这是我的代码:
InputStream inputStream = getClass().getResourceAsStream("Dependencies\\msjh.ttc");
Font font;
try
{
if (inputStream == null)
throw new IOException();
font = Font.createFont(Font.TRUETYPE_FONT, inputStream).deriveFont(Font.PLAIN);
}
catch (IOException | FontFormatException exception)
{
font = new Font("Microsoft JhengHei UI", Font.PLAIN, 16);
}
无论我如何尝试,条件if (inputStream == null)总是为真,并且会抛出IOException。
但是类似的设置窗口图标的方法是有效的:
Frame frame = new Frame("Window");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Dependencies\\icon.png"));
这是我文件中的路径:
IdeaProjects\Project\Dependencies\msjh.ttc (font file)
IdeaProjects\Project\Dependencies\icon.png (image file)
IdeaProjects\Project\src\bin_gen\Main.java (source code)
还有一个虚拟机选项:-Dfile.encoding=MS950
那个 .ttc 文件是从 C:\Windows\Fonts\Microsoft JhengHei UI 复制的。我正在尝试这个,因为font = new Font("Microsoft JhengHei UI", Font.PLAIN, 16); 似乎不起作用(窗口上显示的字体仍然是默认字体)。
【问题讨论】:
标签: java fonts awt inputstream embedded-resource