gpt4 book ai didi

java - 具有不同自定义字体的 JButton 文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:13 26 4
gpt4 key购买 nike

现在,我知道,可以在 JButton 文本中实现不同的字体系列。如下图

JButton button = new JButton("<html><font face=Arial>Hello </font><font face=Verdana>World</font></html>");

看起来像这样。用 Arial 表示“Hello”,用 Verdana 表示“World”。

enter image description here

但是,如果我想让一个词具有我使用 Font.createFont() 方法创建的字体怎么办?我想,像这样的事情会奏效。

Font myFont = createMyFont();
JButton button = new JButton("<html><font face=MyFont>Hello </font>World</html>");

这个问题的意义在于我正在创建一个多语言软件,它在一个 JButton 中有两种字体。

所以,我希望我的 JButton 是这样的:

enter image description here

但是,它是这样的:

enter image description here

最佳答案

注册自定义字体:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
ge.registerFont(myFont);

之后,提供文件名,例如:

      URL fontUrl;
try {
fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf"); // dummy font

Font myFont = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
myFont = myFont.deriveFont(Font.PLAIN,20);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(myFont);


button.setText("<html><font face='Airacobra Condensed'>Hello </font>World</html>");

} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

文件名 可以通过右键单击字体文件,选择属性 -> 详细信息在 Windows 上获取,这就是标题。示例:FontAwesome Regular。

enter image description here

关于java - 具有不同自定义字体的 JButton 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21050003/

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