Java Code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonArr extends JApplet 
implements ActionListener 
{
     JLabel label;
     JButton[ ] mybuttons = 
    {
            new JButton( "one" ),
            new JButton( "two" ),
            new JButton( "three" ),
            new JButton( "four" )
    };
    
    public void init( )
    {
        setLayout( new FlowLayout( )  );
        label = new JLabel( "Hello" );
        add( label ) ;
        
        for( int x=0; x < mybuttons.length; x++ )
        {
            mybuttons[x].addActionListener(this);
            add( mybuttons[x] );
        }
    }
    public void actionPerformed( ActionEvent ae )
    {
        Object obj = ae.getSource( );
        if( obj == mybuttons[0] )
        {
            label.setText( "first button" );
        }
        else if ( obj == mybuttons[1] )
        {
            label.setText( "second button" );
        }
        else if( obj == mybuttons[2] )
        {
            label.setText( "third" );
        }
        else if( obj == mybuttons[3] )
        {
            label.setText( "fourth" );
        }
    }
}


©2006 by E.S.Boese. All Rights Reserved