我添加的字体对JLable有点奇怪,并且没有按原样显示

问题描述 投票:0回答:1

所以我正在使用Swing进行一个小项目,我正在尝试将字体添加到JLable,该字体有点奇怪,叫做you murderer bb,我已经在使用添加的字体了,可以正常工作,但是当我对这一点做完完全相同的事情时,它只是显示常规字体。

private Font font;
File fontFile = new File("resources\\fonts\\Nunito-Regular.ttf");

try {
    font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
    font = font.deriveFont(14f);
} catch (FontFormatException | IOException e) {
    e.printStackTrace();
}

private Font titleFont;
fontFile = new File("resources\\fonts\\youmurdererbb_reg.ttf");

try {
    titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
    titleFont = font.deriveFont(40f);
} catch (FontFormatException | IOException e) {
    e.printStackTrace();
}

private JLabel title;
title = new JLabel("Welcom To Eureka");
title.setFont(titleFont);
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setForeground(Color.decode("#FFFFFF"));
title.setBounds(228, 125, 354, 50);

private JLabel username;
username = new JLabel("Log In");
username.setFont(font);
username.setHorizontalAlignment(SwingConstants.CENTER);
username.setForeground(Color.decode("#BB86FC"));
username.setBounds(682, 80, 48, 20);
username.addMouseListener(new AppControler());

所以username可以正常工作并显示正确的字体,但是title只是显示更大的字体(我将其大小设置为40),但该字体不是我正在使用的字体

java swing fonts
1个回答
0
投票
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = font.deriveFont(40f);

应该是:

titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = titleFont.deriveFont(40f); // <- use the font just created!

结果(更改第一个单词的拼写):

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.