简体   繁体   English

如何使drawString()文本变为粗体?

[英]How do I make drawString() text bold?

I have a drawString() method in my paintComponent method. 我的paintComponent方法中有一个drawString()方法。 Is there a way to make the text drawn by the drawString() bold ? 有没有办法使drawString()绘制的文本变为粗体 Also, is there a way to make the text bigger? 另外,有没有办法让文字更大? I would like to avoid using JLabel s, unless it is absolutely necessary. 我想避免使用JLabel ,除非绝对必要。

According to documentation of drawString : 根据drawString文档:

Draws the text given by the specified string, using this graphics context's current font and color . 使用此图形上下文的当前字体和颜色绘制由指定字符串给出的文本。 The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system 最左边的字符的基线位于此图形上下文的坐标系中的位置(x,y)

Indeed, Graphics class has the setFont(Font font) method available: 实际上, Graphics类有setFont(Font font)方法可用:

g.setFont(new Font("default", Font.BOLD, 16));

您必须在绘制文本之前设置字体。

g.setFont(font);

There are methods: setFont(Font) - Method in class java.awt.Component Sets the font of this component. 有方法:setFont(Font) - 类java.awt.Component中的方法设置此组件的字体。 setFont(Font) - Method in class java.awt.Container Sets the font of this container. setFont(Font) - 类java.awt.Container中的方法设置此容器的字体。 setFont(Font) - Method in class java.awt.Graphics Sets this graphics context's font to the specified font. setFont(Font) - 类java.awt.Graphics中的方法将此图形上下文的字体设置为指定的字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM