Java Code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ClearPanelEx extends JApplet implements ActionListener
{
	JLabel title;
	JButton productInfo, aboutUs;
	JPanel  mainPanel;		// use this JPanel for everything in BorderLayout.CENTER
	JPanel productPanel, aboutUsPanel;
	public void init( )
	{	
		setLayout( new BorderLayout( ) );
		title = new JLabel("<HTML><B><FONT COLOR=RED>Waxing For You!", JLabel.CENTER);
		add( title, BorderLayout.NORTH );
		setupButtons( );
		setupProductPanel( );
		setupAboutUsPanel( );
		mainPanel = new JPanel( new BorderLayout( ) );
		JLabel tmpMsg = new JLabel( "<HTML><CENTER>Welcome<BR><BR> to our<BR><BR>Home!", 
												JLabel.CENTER);
		tmpMsg.setFont( new Font( "Serif", Font.BOLD, 18 ) );
		mainPanel.add(tmpMsg, BorderLayout.CENTER);
		add( mainPanel, BorderLayout.CENTER );
	}
	public void setupAboutUsPanel( )
	{
		aboutUsPanel = new JPanel( new GridLayout( 2, 1 ) );
		Image img = getImage( getCodeBase( ), "Tuning.jpg" );
		ImageIcon icon = new ImageIcon( img );
		aboutUsPanel.add( new JLabel( icon ) );
		JLabel text = new JLabel( "<HTML><CENTER><B>About Us</B><BR><BR> We are a family-"
			+ "run business <BR>that has been waxing since 1997. <BR> We take pride" 
			+ " in our work <BR>and guarantee that you will love our work!", JLabel.CENTER );
		aboutUsPanel.add( text );
	}
	public void setupProductPanel( )
	{
		productPanel = new JPanel( new BorderLayout( ) );
		JLabel prodinfo = new JLabel( "<HTML><CENTER><B>Product Info</B><BR> <BR>"
			+ "We use only high-quality wax.<BR> We layer each wax over hours<BR>"
			+ " to ensure that it soaks in and sticks.<BR>Guaranteed! ", JLabel.CENTER );
		productPanel.add( prodinfo, BorderLayout.CENTER );
	}
	public void setupButtons( )
	{
		productInfo = new JButton( "Info" );
		aboutUs = new JButton( "About Us" );
		productInfo.addActionListener( this );
		aboutUs.addActionListener( this );
		JPanel west = new JPanel( new GridLayout( 2,1 ) );
		west.add( productInfo );
		west.add( aboutUs );
		add( west, BorderLayout.WEST );
	}
	public void actionPerformed( ActionEvent ae )
	{
		Object src = ae.getSource( );
		if ( src == productInfo )
		{
			mainPanel.removeAll( );
			mainPanel.add( productPanel, BorderLayout.CENTER );
		}
		else if ( src == aboutUs )
		{
			mainPanel.removeAll( );
			mainPanel.add( aboutUsPanel, BorderLayout.CENTER );
		}
		mainPanel.validate( );
		mainPanel.repaint( );
	}
}

Back to Code Examples
©2006- by E.S.Boese. All Rights Reserved