英文:
I am adding a font that is a little bit odd to a JLabel and it's not displaying as it should
问题
我正在使用Swing工作在一个小项目上,我尝试将字体添加到一个JLabel
中,这个字体有点奇怪,它叫做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("欢迎来到尤里卡");
title.setFont(titleFont);
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setForeground(Color.decode("#FFFFFF"));
title.setBounds(228, 125, 354, 50);
private JLabel username;
username = new JLabel("登录");
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),但字体不是我正在使用的那个。
1: https://fontmeme.com/fonts/you-murderer-font/
英文:
So I am working on a small project using Swing and I am trying to add a font to a JLabel
, the font is a little bit weird it's called you murderer bb, I already am using a font that I added and it works fine, but when I do the exact same thing to this one well... it just displays a regular font.
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());
so the username
is working fine and displaying the correct font but the title
is just displaying a bigger font (i set the size of it to 40) but the font is not the one I am using
1: https://fontmeme.com/fonts/you-murderer-font/
答案1
得分: 1
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = titleFont.deriveFont(40f); // <- use the font just created!
结果(第一个单词的拼写已更正):
<details>
<summary>英文:</summary>
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = font.deriveFont(40f);
Should be:
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = titleFont.deriveFont(40f); // <- use the font just created!
Result (once 1st word spelling changed):
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/MTmht.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论