/** * Sample #3: Same as #2, but add images to the button. */ import java.awt.event.*; import javax.swing.*; public class Swing3 { public static void main( String [] args ) { JFrame f = new JFrame( "Swing Test" ); f.addWindowListener( new WindowAdapter( ) { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); JPanel p = new JPanel( ); JButton qb = new JButton( new ImageIcon( "cow.gif" ) ); JButton pb = new JButton( new ImageIcon( "tiger.gif" ) ); final JTextField tf = new JTextField( 20 ); tf.setEditable( false ); p.add( qb ); p.add( pb ); p.add( tf ); f.setContentPane( p ); f.setSize( 300, 100 ); f.setVisible( true ); qb.setMnemonic( 'q' ); qb.setToolTipText( "press to quit" ); pb.setMnemonic( 'a' ); pb.setToolTipText( "press to add *" ); qb.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent e ) { System.exit( 0 ); } } ); pb.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent e ) { tf.setText( tf.getText( ) + "*" ); } } ); } }