Please send questions to st10@humboldt.edu .
/**
 * TryImageButton1
 * 
 * simple example application of a JButton containing an image.
 *
 * note: this depends on image file train.gif being in the same
 * directory as this application's class file. And note that
 * BlueJ cannot run it directly, because of its current directory
 * *not* being the directory where the source code is --- compile
 * in BlueJ, then run from the DOS prompt (after changing to the
 * proper directory, of course)
 *
 * modified by: Sharon M. Tuttle
 * last modified: 2-18-01
 **/

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

public class TryImageButton1
{
        public static void main(String args[])
        {
                TryImageButton1Frame mainFrame = new TryImageButton1Frame();
        
                mainFrame.setTitle("A Button with an Image");
                mainFrame.show();

                // I'll set up size and window-closing in class 
                // TryImageButton1Frame...
        }
}

class TryImageButton1Frame extends JFrame 
{
	JButton			imageButton;
	boolean			nowRed;
	JLabel			description;
	JPanel			buttonPanel, labelPanel;

         // added to make applet more displayable
        Font            biggerFont;

        // content pane for this frame
        Container       myContentPane;
				
	// JButtons and JLabels can contain Icons. Icon is actually an
	// interface, and ImageIcon is (from the Java 1.2.2 
	// API) "An implementation of the Icon interface that 
	// paints Icons from Images."
	ImageIcon 		myImageIcon;        

        // constructor for this JFrame subclass
        public TryImageButton1Frame()
        {
                // this will be instantiated as an inner listener
                // class for the image button
                ActionListener toggListener;        

                // get content pane for this JFrame
                myContentPane = getContentPane();

                // set up window-closing for the instance of this
                // TryImageButton1Frame, with an anonymous inner class
                addWindowListener(new WindowAdapter()
                        {
                                public void windowClosing(WindowEvent e)
                                {
                                        System.exit(0);
                                }
                        });

		// I'm going to use the content pane's default
		// BorderLayout this time...

		// now, I have placed a .gif file in the same
		// directory as my TryImageButton1.class file.

		// so, this instantiates my IconImage instance
		// using a .gif file in the same directory as
		// the .class file. (BEWARE --- you cannot run this
		// successfully within BlueJ! Compile it in BlueJ,
		// run it from the DOS prompt...)
		myImageIcon = new ImageIcon("train.gif");

		// now, one of JButton's constructors can take an
		// Icon as a parameter...
		imageButton = new JButton(myImageIcon);
		imageButton.setBackground(Color.red);
		nowRed = true;
		toggListener = new ToggleListener();
		imageButton.addActionListener(toggListener);

		buttonPanel = new JPanel();
		buttonPanel.add(imageButton);
		myContentPane.add(buttonPanel, "Center");

		// here's a little label under the imageButton ---
		// note: did you know that you can set the
		// horizontal alignment within a JLabel?
		description = new JLabel("a button with an image",
					  SwingConstants.CENTER);
		biggerFont = new Font("Serif", Font.PLAIN, 30); 
		description.setFont(biggerFont);

		labelPanel = new JPanel();
		labelPanel.add(description);
		myContentPane.add(labelPanel, "South");

                // set the size for the instance of this TryImageButton1Frame
                //setSize(400, 500);
                pack();     // size the frame based
							// on the components within
        }

        // this special listener handles toggling the imageButton's
        // background color
        private class ToggleListener implements ActionListener
        {
                public void actionPerformed(ActionEvent event)
                {
			// toggle from current color
                        if (nowRed)
                        {
                                imageButton.setBackground(Color.yellow);
                                nowRed = false;
                        }
                        else
                        {
                                imageButton.setBackground(Color.red);
                                nowRed = true;
                        }
                }
        }
}