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);
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.
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.
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.”
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With