Fonts in GUI's

We have been using the default font that Java provides.  Now we are going to experiment with changing the fonts.

x.setFont(new Font(font name as StringFont.stylesize));

x is an object like L2, or b1 or t3
font name as String 
is a font name like "Ariel"  or "MS San Serif"
Font.style 
is usually  Font.BOLD or Font.PLAIN  or  Font.ITALIC
size 
is an integer - larger number means larger size 

This sample shows several different fonts - see the lines in red. 

Font samples can be used on JButtons, JLabels and JTextfields.  
A good list of fonts can be found on Accessories/WordPad - it will show samples

Copy, paste and run it

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;

public class Lab3fonts extends JFrame 
  {
   JButton b1, b2;
   JLabel L1, L2;
   JTextField t1, t2;

   public Lab3fonts () 
   { 
    super("fonts");
    Container container = getContentPane(); 
    container.setLayout(new FlowLayout());
    setSize(400,200);         //Set Size of Window
    container.setBackground(Color.yellow);

    L1 = new JLabel("this is Ariel"); 
    L1.setForeground(Color.black);
    L1.setFont(new Font("Ariel",Font.BOLD,25));

    t1 = new JTextField("Century: "); 
    t1.setForeground(Color.black);
    t1.setBackground(Color.pink); 
    t1.setFont(new Font("Century",Font.BOLD,15));

    b1 = new JButton("Times New Roman"); 
    b1.setFocusPainted(false); 
    b1.setFont(new Font("Times New Roman",Font.ITALIC,20));
    b1.setForeground(Color.black);
    b1.setBackground(Color.yellow); 

    L2 = new JLabel("Comic Sans MS"); 
    L2.setForeground(Color.black);
    L2.setFont(new Font("Comic Sans MS",Font.PLAIN,20));

    t2 = new JTextField("Cirsiva: "); 
    t2.setForeground(Color.black);
    t2.setBackground(Color.pink); 
    t2.setFont(new Font("Monotype Corsiva",Font.BOLD,20));

    b2 = new JButton("Verdana"); 
    b2.setFocusPainted(false); 
    b2.setForeground(Color.black);
    b2.setBackground(Color.yellow); 
    b2.setFont(new Font("Verdana",Font.ITALIC,16));

    container.add(L1); 
    container.add(b1);
    container.add(t1); 
    container.add(L2);
    container.add(b2); 
    container.add(t2); 

    setVisible(true);
    }

   public static void main(String args[]) 
   {
    Lab3fonts prog = new Lab3fonts ();
    prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 }

This is an optional way to deal with fonts

If you have a font that you want to use many times like I did on my game on Lab3a, make a variable Font and add it to the variable section where the JButtons, etc are declared. Add these

Font mincho = new Font("MS mincho",Font.ITALIC,20);
Font arielBold = new Font("ariel", Font.BOLD, 15);

Replace the L1.setFont line with this:
          L1.setFont(mincho);

Replace the b1.setFont line with this:
       b1.setFont(arielBold);