Java字体问题

发布于 2024-12-05 04:57:09 字数 154 浏览 0 评论 0原文

假设我有一个 Swing JComponent,并且我为该 JComponent 的文本设置了一个 Font。我构建项目并创建项目的 .jar 文件。现在,如果我从另一台未安装字体的计算机上运行这个 jar 文件,会发生什么? jar 会自动安装字体还是我需要进行某种检查才能做到这一点?谢谢。

Say I have a Swing JComponent and I set a Font for the text of that JComponent. I build the project and create a .jar file of my project. Now, If I run this jar file from another computer where the Font is not install, what will be happen? Will the jar automatically install the Font or I need to make some kind of checking to do that? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-12-12 04:57:09

如果您需要使用该特定字体,则必须在运行 jar 文件之前确保它已安装在计算机上,或者自行加载。没有任何东西会自动为您安装字体。您可以使用以下代码检索可用字体的列表:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String [] fonts = ge.getAvailableFontFamilyNames();

或者,您可以使用自己加载和注册字体

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font f = Font.createFont(Font.TRUETYPE_FONT, new File(pathToYourTTFFile));
ge.registerFont(f);

您应该检查 registerFont 的返回代码并捕获/处理 抛出的异常创建字体

If you need to use that specific font, you'll have to make sure it's installed on the machine before your jar file is run, or load it yourself. There's nothing that will automatically install the font for you. You can retrieve the list of available fonts using this code:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String [] fonts = ge.getAvailableFontFamilyNames();

Alternatively, you can load and register the font yourself using

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font f = Font.createFont(Font.TRUETYPE_FONT, new File(pathToYourTTFFile));
ge.registerFont(f);

You should check the return code of registerFont and catch/deal with the exceptions thrown by createFont.

颜漓半夏 2024-12-12 04:57:09

不,没有任何自动方法。您必须手动安装字体,但是您可以使用以下方式获取可用字体:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();

No there isn't any automatic method. You have to install font manually however you may get the available fonts using,

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文