gpt4 book ai didi

java - 将字体导入 GUI

转载 作者:行者123 更新时间:2023-11-29 09:56:19 25 4
gpt4 key购买 nike

除了 swing 似乎附带的基本 5 字体之外,我正在尝试更改我的 GUI 的字体。如何导入字体并在我的代码中实际使用它们?

最佳答案

默认情况下通常有 5 个以上可用,但它们会因系统而异。这个答案检查了现有的字体,以及如何加载和注册新字体。

它使用 Download Free Fonts 提供的“Airacobra Condensed”字体(通过热链接 URL 获得)。应用 Jar 中的字体。也可以通过 URL 访问。

Registered Font

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class LoadFont {
public static void main(String[] args) throws Exception {
// This font is < 35Kb.
URL fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
JList fonts = new JList( ge.getAvailableFontFamilyNames() );
JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
}
}

好吧,这很有趣,但这种字体实际上是什么样子的?

Display Font

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class DisplayFont {
public static void main(String[] args) throws Exception {
URL fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
font = font.deriveFont(Font.PLAIN,20);
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);

JLabel l = new JLabel(
"The quick brown fox jumped over the lazy dog. 0123456789");
l.setFont(font);
JOptionPane.showMessageDialog(null, l);
}
}

关于java - 将字体导入 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10417907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com