Java Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFrameEvents extends JApplet
implements ActionListener
{
JFrame frame;
JPanel buttonspane;
JButton Bots, Swazi, Thai;
Image botsImg, zebraImg, thaiImg;
ImageIcon icon;
JLabel label;
public void init( )
{
setLayout( new BorderLayout( ) );
setupImages( );
setupButtons( );
}
public void setupButtons( )
{
Bots = new JButton( "<HTML>Botswana<BR>animals" );
Swazi = new JButton( "Swaziland Zebra" );
Thai = new JButton( "Thailand" );
Bots.addActionListener( this ); // add listeners
Swazi.addActionListener( this );
Thai.addActionListener( this );
buttonspane = new JPanel( new GridLayout(3,1) );
buttonspane.add( Bots );
buttonspane.add( Swazi );
buttonspane.add( Thai );
add( buttonspane, BorderLayout.CENTER );
}
public void setupImages( )
{
botsImg = getImage( getCodeBase( ), "Botswana.jpg" );
zebraImg = getImage( getCodeBase( ), "Swazi_Zebra.jpg" );
thaiImg = getImage( getCodeBase( ), "Thailand.jpg" );
icon = new ImageIcon( botsImg ); // start with the pc image
label = new JLabel( icon );
}
public void actionPerformed( ActionEvent ae )
{
Object src = ae.getSource( );
//1. Create the frame.
frame = new JFrame("FrameDemo");
//2. Set layout manager
frame.setLayout( new BorderLayout( ) );
//3. Create components and put them in the frame.
if( src == Bots )
{
icon.setImage( botsImg );
label.setIcon( icon );
}
else if ( src == Swazi )
{
icon.setImage( zebraImg );
label.setIcon( icon );
}
else if ( src == Thai )
{
icon.setImage( thaiImg );
label.setIcon( icon );
}
frame.add(label, BorderLayout.CENTER);
//4. Size the frame.
frame.pack( );
//5. Show it.
frame.setVisible(true);
}
}
©2006 by E.S.Boese. All Rights Reserved