java中字符串的字体设置

发布于 2024-10-18 11:36:12 字数 28 浏览 1 评论 0原文

我可以在java中设置字符串的字体属性吗?

Can I set font properties for a string in java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私野 2024-10-25 11:36:12

字体属性是在您正在使用的 GUI 对象(JLabel 等)中的 Font 对象上设置的,而不是在 String 本身上设置的。

编辑:

如果您想向控制台添加格式,则必须将格式嵌入 String 本身。为了使输出变为粗体,我需要执行以下操作:

 System.out.println((char)27 +"[1m testing bold");

(char) 27 是转义序列,[ 后面跟着一组 < code>; 不同格式类型的分隔值(见下文),后跟 m。你需要尝试一下这个。在我的 Mac 上,命令提示符继续以粗体显示,因为我在退出之前没有重置为正常状态 ([0m)。

顺便说一句,此信息是从此处提取的。
一些属性:

0 Normal (clear all)
1 bold
2 dim
4 underline
5 blink
7 reverse
8 blank
9 overstrike
22 normal intensity (cancel bold and blank)
24 underline off
25 blink off
27 reverse off
28 blank off
29 overstrike off
30 black
31 red
32 green
33 yellow
34 blue
35 magenta
36 cyan
37 white
40 black background
41 red background
42 green background

Font properties are set on the Font object in the GUI object that you are using (JLabel, etc), not on the String itself.

EDIT:

If you want to add formatting to your console, you will have to embed the formatting within the String itself. In order to get my output to be bold, I needed to do the following:

 System.out.println((char)27 +"[1m testing bold");

The (char) 27 is an escape sequence, the [ is followed by a set of ; separated values for different formatting types (see below), followed by an m. You will need to play around with this. On my Mac, the command prompt continued in bold as I didnt reset to normal ([0m) before I exited.

This info, by the way, was lifted from here.
some attributes:

0 Normal (clear all)
1 bold
2 dim
4 underline
5 blink
7 reverse
8 blank
9 overstrike
22 normal intensity (cancel bold and blank)
24 underline off
25 blink off
27 reverse off
28 blank off
29 overstrike off
30 black
31 red
32 green
33 yellow
34 blue
35 magenta
36 cyan
37 white
40 black background
41 red background
42 green background
烈酒灼喉 2024-10-25 11:36:12

字符串没有字体,因为它与任何显示它的方式完全分开。
字体与您用来向用户呈现字符串的用户界面组件相关,如何设置它取决于这些用户界面组件。

A String doesn't have a font, as it's completely separate from any way to display it.
Fonts are related to the user interface components you're using to present the String to your users, how to set it would depend on those user interface components.

白首有我共你 2024-10-25 11:36:12

我找到了另一种方法来逃避“\033” 这里

另外,我想要关于我在这里找到的“akf属性图表”的官方属性列表 - 在“SGR(选择图形再现)参数”表中引用

我想补充一点,这些在 Eclipse 控制台输出中都不适合我,我认为这是不可能的?

I found another way to escape "\033" here

Also, I wanted the official attributes list in regards to "akf's attribute chart which I found here - referenced in the "SGR (Select Graphic Rendition) parameters" table

I would like to add that neither of these worked for me in eclipse console output. I take it that is impossible?

莳間冲淡了誓言ζ 2024-10-25 11:36:12

您可以使用AttributedString。以下是一些示例: JavaDocExamples

Font font = new Font("LucidaSans", Font.PLAIN, 14);
AttributedString messageAS = new AttributedString(textMessage);
...
messageAS.addAttribute(TextAttribute.FONT, font);
AttributedCharacterIterator messageIterator = messageAS.getIterator();
FontRenderContext messageFRC = graphics2D.getFontRenderContext();
LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);

You can use AttributedString. Here is some examples: JavaDocExamples

Font font = new Font("LucidaSans", Font.PLAIN, 14);
AttributedString messageAS = new AttributedString(textMessage);
...
messageAS.addAttribute(TextAttribute.FONT, font);
AttributedCharacterIterator messageIterator = messageAS.getIterator();
FontRenderContext messageFRC = graphics2D.getFontRenderContext();
LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文