Java 文本格式加粗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22847148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Text Formatting bold
提问by YellowSoloCup
I am having a hard time understanding how to bold the text in my GUI program. The program shows the initial value of my calculator program to be 0.0 but I need to be able to make it bold and set it to 14 font. Is there any easy way to do this?
我很难理解如何在我的 GUI 程序中加粗文本。该程序显示我的计算器程序的初始值为 0.0,但我需要能够将其设为粗体并将其设置为 14 字体。有什么简单的方法可以做到这一点吗?
JPanel x = new JPanel(new BorderLayout());
JTextField z = new JTextField();
z.setEditable(false);
z.setText("0.0");
x.add(field, BorderLayout.NORTH);
采纳答案by whiskeyspider
Try this:
尝试这个:
z.setFont(z.getFont().deriveFont(Font.BOLD, 14f));
deriveFont()
has the advantage of being able to base your new font on the existing one. This will maintain the font characteristics that you don't mean to change.
deriveFont()
具有能够基于现有字体的新字体的优势。这将保持您不想更改的字体特征。
回答by Salah
You can simply change you JTextField
font by doing the following:
您可以JTextField
通过执行以下操作简单地更改字体:
f.setFont(new Font("Tahoma", Font.BOLD, 14));// Tahoma is an example, you could use any forn you want.