This is the first swing example

import java.awt.*;
import java.applet.*;
import javax.swing.*; // Using swing
import java.awt.event.*; // using listeners

public class FirstSwing extends JPanel {
    // this is an application: need main
    public static void main(String[] args) {

	// setting up the frame, give it a title
	JFrame fr = new JFrame("First swing example");
	fr.setSize(300,100);
	fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	// get the frame's content pane:
	Container cp = fr.getContentPane();

	// create and initialize a FirstSwing object
	FirstSwing fs = new FirstSwing();
	fs.init();

	// add the object to the frame
	cp.add(fs);
	
	// display the frame (after the object is added)
	fr.setVisible(true);
    }

    // the method initializes the FirstSwing object
    public void init() {
	JLabel label = new JLabel("Hi There!");
	add(label);
    }  
}

This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.