Peer vs. Swing Components

I'm not going to talk much about Swing in this course, but most of what I teach you about the standard AWT components carries over to their Swing equivalents with only trivial changes. For example, here's yet another way to write an applet that says "Cub Scouts!" in 24 point, SansSerif, bold and blue and with a yellow background.

import java.awt.*;
import javax.swing.*;


public class SwingCubScout extends JApplet {

  public void init() {
  
     JLabel cubScouts = new JLabel("Cub Scouts!");
     cubScouts.setForeground(Color.blue);
     cubScouts.setBackground(Color.yellow);
     cubScouts.setFont(new Font("Sans", Font.BOLD, 24)); 
     this.getContentPane().add(cubScouts);
     
  }

}

This is almost identical to the AWT-based Cub Scout applet, except that I've imported the javax.swing package and changed Label to JLabel and Applet to JApplet.

Cub Scouts applet


Previous | Next | Top | Cafe au Lait

Copyright 1998, 1999, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 2, 2002