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 java.applet.*;
import javax.swing.*;


public class SwingCubScout extends Applet {

  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.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.


Previous | Next | Top
Last Modified October 21, 1999
Copyright 1998, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu