How to set String font size, style in Java using Font class?

Suppose I have the string "Hello World". I want to change the style of this line to a BOLD font and set the size of all characters from 12 to 18 [pt]. After that, I want to use this line in JLabeland JButton. How can i do this?

+5
source share
2 answers

Take a look here http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont%28float%29

JComponent has a setFont () method. You will control the font there, not the String.

For instance,

JButton b = new JButton();
b.setFont(b.getFont().deriveFont(18.0f));
+13
source

Font myFont = new Font("Serif", Font.BOLD, 12);, then use the setFont method for your components, for example

JButton b = new JButton("Hello World");
b.setFont(myFont);
+20
source

All Articles