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

This is taken directly from, 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() {

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

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