Java Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButImg extends JApplet implements ActionListener
{
  JButton[ ] buttonArray = new JButton[3];

  public void init( )    
  {
	setupButton( 0, "About Me" ); // set up each of the buttons by calling the setupButton method
	setupButton( 1, "Resume" );   // send it the number of the button in the buttonArray array and
	setupButton( 2, "Hobbies" );   // text to display on the button
  }
  public void setupButton( int buttonNum, String name )
  {	
   	Image img = getImage(getCodeBase( ), "buttongr.gif" ); // get the main image for the button
	ImageIcon ic = new ImageIcon( img );
	buttonArray[buttonNum] = new JButton( name, ic );  	// create the button
								// place the text on top of the image
	buttonArray[buttonNum].setHorizontalTextPosition( SwingConstants.CENTER );
								// turn the border off around the button
	buttonArray[buttonNum].setBorderPainted(false);
					// no spacing between the contents of the button and the edge
	buttonArray[buttonNum].setMargin( new Insets(0,0,0,0) ); 
	buttonArray[buttonNum].setForeground( Color.white ); 	// set the font color to white
	buttonArray[buttonNum].addActionListener( this ); 	// listen for clicks
	// when the user pressed the button, have the icon color change
	buttonArray[buttonNum].setPressedIcon( 
					new ImageIcon( getImage(getCodeBase( ), "buttonbrite.gif" ) ) );
	// when the user selects the button, have the icon color change
	buttonArray[buttonNum].setSelectedIcon( 
					new ImageIcon( getImage(getCodeBase( ), "buttongray.gif" ) ) );
	// when the user rolls the mouse over the button, have the icon color change
	buttonArray[buttonNum].setRolloverIcon( 
					new ImageIcon( getImage(getCodeBase( ), "buttonred.gif" ) ) );
	// when the user rolls the mouse over a SELECTED button, have the icon color change
	buttonArray[buttonNum].setRolloverSelectedIcon( 
					new ImageIcon( getImage(getCodeBase( ), "buttondim.gif" ) ) );
	// don't draw the regular button background
 	buttonArray[buttonNum].setContentAreaFilled(false); 
 	buttonArray[buttonNum].setFocusPainted(false); 
	add(buttonArray[buttonNum]); 	// add to applet
  }
  public void actionPerformed( ActionEvent ae )
  { 
	Object obj = ae.getSource( );
	if ( obj instanceof JButton )
 	{
		for ( int i=0; i<buttonArray.length; i++ )     // de-select the button that is currently selected
			if ( buttonArray[i].isSelected( ) )
				buttonArray[i].setSelected( false );
		JButton button = (JButton) obj;    // set the button that was just clicked to selected mode
		button.setSelected( true );
	}
  }
}


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