CIT 591 A Complete (Small) Swing Program
Fall 2004, David Matuszek

This is taken directly from SwingExamples.zip, which you should download and use (along with the Java API, of course).

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

import java.util.Random;


public class DiceRoller extends JFrame {
    static Random rand = new Random();
    javax.swing.JButton rollButton;
    JTextField result;


    public static void main(String[] args) {
        new DiceRoller().run();
    }


    void run() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        rollButton = new JButton("Roll 'em!");
        getContentPane().add(rollButton, BorderLayout.NORTH);
        result = new JTextField("You haven't rolled yet.");
        getContentPane().add(result, BorderLayout.SOUTH);
        pack();
        setVisible(true);

        rollButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int number = rand.nextInt(6) + 1;
                result.setText("You rolled a " + number);
        }});
    }
}