Java Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SlideShowUsingButtons extends JApplet implements ActionListener
{
Image[ ] photos; // array of all your images
ImageIcon icon; // images need to be in an ImageIcon
JLabel imgLabel; // and stored into a JLabel
int currentIndex = 0; // which image index currently displayed
JButton prev, next;
public void init( )
{
setLayout( new FlowLayout( ) );
photos = new Image[4]; // load the images into memory
photos[0] = getImage( getCodeBase( ), "Cambodia.jpg" );
photos[1] = getImage( getCodeBase( ), "Czech.jpg" );
photos[2] = getImage( getCodeBase( ), "NewZealand.jpg" );
photos[3] = getImage( getCodeBase( ), "Poland.jpg" );
icon = new ImageIcon( );
imgLabel = new JLabel( );
imgLabel.setHorizontalAlignment( JLabel.CENTER ); // center all images
prev = new JButton( "Prev" );
next = new JButton( "Next" );
setupIcon( 0 ); // initialize setup to first display first image (index 0)
add( prev ); // add components to applet
add( imgLabel );
add(next );
prev.addActionListener(this); // add listeners
next.addActionListener(this);
}
public void setupIcon( int index ) // method changes the image in the label
{ // & modifies next/prev buttons to be enabled/disabled
currentIndex = index;
icon.setImage( photos[index] );
imgLabel.setIcon(icon );
prev.setEnabled( true ); // default both buttons to enabled
next.setEnabled( true );
if( index == 0 )
prev.setEnabled( false );
if( index == photos.length-1 )
next.setEnabled( false );
}
public void actionPerformed( ActionEvent ae )
{
Object obj = ae.getSource( );
if ( obj == next )
setupIcon( currentIndex+1 ); // otherwise just increment the index
else
setupIcon( currentIndex-1 ); // otherwise just increment the index
repaint( ); // Show the change.
}
}
©2006 by E.S.Boese. All Rights Reserved