Format Text in the Window
Learn how to create font, change font's settings, and align text in the window.
Creating a font
Set the font for the label.
- Create a new
Font
object calledfont
. - Add code exactly as shown below to use
font
as the font forlabel
.
...
public WizardOfYesNo() {
...
label.setText(answer);
Font font = new Font("Times New Roman", Font.PLAIN, 18);
label.setFont(font);
add(label);
...
By following the steps above and clicking on the “run” button, “YES” or “NO” will now be displayed in the new font in the output.
Changing the font settings
Let’s change the font’s style.
- Change the style of font to
BOLD
orITALIC
instead ofPLAIN
. BecauseBOLD
,ITALIC
, andPLAIN
are all static variables of Font, useFont.
in front of the variable name.
...
public WizardOfYesNo() {
...
label.setText(answer);
Font font = new Font("Times New Roman", Font._________, 18);
label.setFont(font);
add(label);
...
...
Get hands-on with 1200+ tech skills courses.