我正在将一种有点奇怪的字体添加到一个JLabel中,但它的显示效果不正常。

huangapple go评论82阅读模式
英文:

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!

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

我正在将一种有点奇怪的字体添加到一个JLabel中,但它的显示效果不正常。


<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); // &lt;- 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>



huangapple
  • 本文由 发表于 2020年5月19日 19:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61889982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定