Creating a font

Set the font for the label.

  1. Create a new Font object called font.
  2. Add code exactly as shown below to use font as the font for label.
...
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.

  1. Change the style of font to BOLD or ITALIC instead of PLAIN. Because BOLD, ITALIC, and PLAIN are all static variables of Font, use Font. 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.