开发者

Font settings for strings in java

开发者 https://www.devze.com 2023-02-12 12:04 出处:网络
开发者_JAVA技巧Can I set font properties for a string in java.Font properties are set on the Font object in the GUI object that you are using (JLabel, etc), not on the String itself.
开发者_JAVA技巧

Can I set font properties for a string in java.


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


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.


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?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消