java 如何增加字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15953489/
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
How to increase font size
提问by Sohaib
I have a little problem which seems silly to me but I cant find a solution to it on the internet. I have a code in which a font is initialized using a ttf file as follows.
我有一个对我来说似乎很愚蠢的小问题,但我无法在互联网上找到解决方案。我有一个代码,其中使用 ttf 文件初始化字体,如下所示。
InputStream is = new BufferedInputStream(new FileInputStream(
"dundalkh.ttf"));
Font f = Font.createFont(Font.TRUETYPE_FONT, is);
Now I use the Graphics Component to draw a String on the screen. Before drawing the string I use the following line:
现在我使用图形组件在屏幕上绘制一个字符串。在绘制字符串之前,我使用以下行:
g.setFont(f);
g.drawString("Title",300,30);
However the size of my font is very small. When I used the f.getSize() method it prints 1. How should I increase the font size?
但是我的字体非常小。当我使用 f.getSize() 方法时,它会打印 1. 我应该如何增加字体大小?
回答by MasNotsram
You need to pull a second Font object from the inital one using the deriveFont method:
您需要使用deriveFont 方法从初始对象中拉出第二个Font 对象:
Font f = Font.createFont(Font.TRUETYPE_FONT, is);
Font newFont = f.deriveFont(10f);
Here is the doc entry for it:
这是它的文档条目:
http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont(float)
http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont(float)
回答by Denis Tulskiy
Font class has several deriveFontmethods to create fonts with different parameters, particularly font.deriveFont(12f)will create a font with size 12.
Font 类有几种deriveFont方法可以创建不同参数的字体,特别是font.deriveFont(12f)会创建一个大小为 12 的字体。
EDIT: make sure to use a float number for size, because there is deriveFont(int style) which sets font style, not size.
编辑:确保使用浮点数作为大小,因为有设置字体样式而不是大小的deriveFont(int style)。
回答by Sajeev
try this
试试这个
Font font = new Font(Font.TRUETYPE_FONT, Font.BOLD, 30);
txt.setFont(font);

