Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Text Formatting bold

I am having a hard time understanding how to bold the text in my GUI program. The program shows the initial value of my calculator program to be 0.0 but I need to be able to make it bold and set it to 14 font. Is there any easy way to do this?

JPanel x = new JPanel(new BorderLayout());
         JTextField z = new JTextField();
         z.setEditable(false);
         z.setText("0.0");
         x.add(field, BorderLayout.NORTH);
like image 485
YellowSoloCup Avatar asked Apr 03 '14 19:04

YellowSoloCup


People also ask

How do I make text bold in Java?

To make a text bold create a font bypassing FontWeight. BOLD or, FontWeight. EXTRA_BOLD as the value of the parameter weight and, to make a text italic pass FontPosture. ITALIC as the value of the parameter posture.

How do I bold text in a string?

The bold() method creates a string that embeds a string in a <b> element ( <b>str</b> ), which causes a string to be displayed as bold.

How do you write in bold code?

To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”

How do I print bold in system out Println?

You cannot print bold with Java System. out . It just streams to the standard output stream so, in principle, it is unformatted text only. However, some software packages interpret special character sequences (the so-called ANSI escape sequences) to allow formatting.


1 Answers

Try this:

z.setFont(z.getFont().deriveFont(Font.BOLD, 14f));

deriveFont() has the advantage of being able to base your new font on the existing one. This will maintain the font characteristics that you don't mean to change.

like image 166
martinez314 Avatar answered Sep 21 '22 18:09

martinez314