The Java Program: ImageButton.java
1 import java.awt.Window;
2 import java.awt.event.WindowEvent;
3 import java.awt.event.WindowAdapter;
4
5 import javax.swing.JButton;
6 import javax.swing.JFrame;
7 import javax.swing.ImageIcon;
8
9
10 public class ImageButton {
11
12 public static void main (String [] args) {
13
14 final ImageIcon icon = new ImageIcon ("cs-logo.gif");
15 final JButton ib = new JButton ("Press", icon);
16 ib.setVerticalTextPosition (JButton.CENTER);
17 ib.setHorizontalTextPosition (JButton.LEFT);
18 ib.setMnemonic('p'); // Alt-p will press button
19
20 final JFrame frame = new JFrame ("Image Button");
21 frame.getContentPane().add (ib);
22 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Requires Java 1.3
23 frame.pack();
24 frame.setVisible (true);
25 }
26 }