// ButtonsDemo.java // Demos basics of command buttons and event handling // APPLET CODE="ButtonsDemo" HEIGHT=250 WIDTH=400 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonsDemo extends JApplet implements ActionListener { Container c; JLabel l; JTextField tf; JButton b1, b2; public void init( ) { c = getContentPane( ); c.setLayout( new FlowLayout( ) ); l = new JLabel( "Meaningless Result" ); c.add( l ); tf = new JTextField( 25 ); c.add( tf ); // create buttons Icon java1 = new ImageIcon( "java.gif" ); Icon danger1 = new ImageIcon( "danger.gif" ); b1 = new JButton( "Java", java1 ); b1.setToolTipText( "It's dangerous to click this button!" ); b1.setRolloverIcon( danger1 ); c.add( b1 ); Icon lu1 = new ImageIcon( "lu.gif" ); Icon doc1 = new ImageIcon( "calvinanm.gif" ); b2 = new JButton( "Lehigh", lu1 ); b2.setToolTipText( "Calvin gives a 'thumbs up' to Lehigh" ); b2.setRolloverIcon( doc1 ); c.add( b2 ); // register the applet as the action listener b1.addActionListener( this ); b2.addActionListener( this ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource( ) == b1 ) tf.setText( "You pressed Java" ); else tf.setText( "You pressed Lehigh" ); } }