Java Code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CoffeeClubEvents extends JApplet implements ActionListener
{
	JButton home, join, faq, contact;
	Image img;
	JLabel title, logo, copyright, ntbkImg, ntbkDesc, infoSheets, welcomeMsg;
    JLabel basket;
	Color tanColor = new Color( 204, 153, 51 );
	Color darkColor = new Color( 51, 17, 0 );
	Color bkgrdColor = new Color ( 17, 8, 0 );
	JPanel leftside, top, center, welcome, separator, freeNotebook, faqPanel;

	public void init( )
	{  
		setLayout( new BorderLayout( ) );
		doTitle( );
		doLeftSide( );
		doBottom( );
		doCenter( );
		setupFAQPanel( );
	}
	public void setupButton( JButton b )    
	{
		b.setContentAreaFilled( false );
		b.setBorderPainted( false );
		b.setFocusable( false );
		b.setForeground( tanColor );
		b.addActionListener( this );
		leftside.add( b );
	}
	public void doTitle( )
	{
		img = getImage( getCodeBase( ), "coffeeLogoMetal.png" );
		logo = new JLabel( new ImageIcon(img) );
		img = getImage( getCodeBase( ), "logoName.png" );
		title = new JLabel( new ImageIcon(img) );
		top = new JPanel( new FlowLayout( ) );
		top.add( logo );
		top.add( title );
		top.setBackground ( tanColor );
		add( top, BorderLayout.NORTH );
	}
	public void doLeftSide( )
	{
		// left side menu
		leftside = new JPanel( new GridLayout(5,1 ) );
		leftside.setBackground ( darkColor );
		home = new JButton( "Home" );
		join = new JButton( "Join the Club" );
		faq = new JButton( "FAQ" );
		contact = new JButton( "Contact Us" );
		setupButton( home );
		setupButton( join );
		setupButton( faq );
		setupButton( contact );
		basket=new JLabel(new ImageIcon(getImage(getCodeBase( ),"coffeeBasket.png")));
		leftside.add( basket );
		add( leftside, BorderLayout.WEST );
	}
	public void doBottom( )
	{ 						// String can go across multiple lines: use + to append
		copyright = new JLabel( "<HTML>(c) 2008 by CoffeeClubOfTheWorld.com."
 			+ "  All Rights Reserved.", JLabel.CENTER );   
		copyright.setForeground( tanColor );
		copyright.setOpaque(true);
		copyright.setBackground( darkColor );
		add( copyright, BorderLayout.SOUTH );
	}
	public void doCenter( )
	{
		center = new JPanel ( new BorderLayout( ) );
		center.setBackground( tanColor );
		// add dark separator as blank colored panel in NORTH of center panel
		separator = new JPanel( );
		separator.setBackground ( darkColor );
		separator.setPreferredSize( new Dimension( 10, 20 ) );
		center.add( separator, BorderLayout.NORTH );

		welcome = new JPanel( new FlowLayout( ) );
		welcome.setOpaque( false );
		welcomeMsg = new JLabel( "<html><center>" 
 			+ "<H2>Welcome to our Coffee of the Month Club! </h2>"
			+ "<HR WIDTH=80%><I>Our coffee club is not just gourmet coffee – "
 			+ "<BR>it's an immersion of experience. <HR WIDTH=80%><BR>"
 			+ "We personally select gourmet coffees from around the world. <BR>"
			+ "</CENTER>Each month, a freshly roasted coffee is sent to your door "
			+ "<BR>along with an info page about the coffee and other goodies."
			+ "<BR><BR>Each month we send you:"
			+ "<UL><LI>A 12-oz. bag of fresh coffee beans "
			+ "<LI>Information sheets about the coffee and region "
			+ "<LI>A regional gift each month (e.g., spices, nuts, chocolates, ...)"
			+ "</UL><BR><B>FREE SHIPPING!</B><BR> <BR> ");	
		welcomeMsg.setForeground( darkColor );
		welcome.add( welcomeMsg );
		// create the bottom free notebook offer
		freeNotebook = new JPanel( new GridLayout( 1, 3 ) );
		freeNotebook.setOpaque(false);
		ntbkImg=new JLabel(new ImageIcon(getImage(getCodeBase( ), "ntbk.png")));
		ntbkDesc = new JLabel(  "<HTML><CENTER>Buy NOW and receive<BR> a FREE "
 				+ "<BR>leather binder <BR>for collecting the fact sheets!" );
		infoSheets = new JLabel(new ImageIcon(getImage(getCodeBase( ), 
 							"InfoSheets.png")));
		freeNotebook.add( ntbkImg );
		freeNotebook.add( ntbkDesc );
		freeNotebook.add( infoSheets );
		welcome.add( freeNotebook, BorderLayout.SOUTH );
		center.add( welcome, BorderLayout.CENTER );
		add( center, BorderLayout.CENTER );
	}
	public void setupFAQPanel( )
	{
		/* put this into a method so that it is done only once, and then we need
	     only one line inside the if statement of the actionPerformed method */
		JLabel faqText = new JLabel( "<HTML><H1>FAQ</H1>"
 			+ "<H2>Can I get decaffeinated coffee?</H2>" 
			+ "No. Our selections are all caffeinated."
			+ "<H2>Can I get ground coffee instead of the beans?</H2>"
			+ "No. Coffee will stay fresher as whole beans than if preground. "
			+ "<BR>This gives you the best flavors to experience."
			+ "<H2>Can I choose which coffees I get?</H2>"
			+ "No. Each month we send the same coffee package to all members."
			+ "<H2>How do I store the coffee?</H2>"
			+ "Coffee should be kept in a cool place in an airtight container. "
 			+ "<BR>Do not freeze your coffee!");
		faqPanel = new JPanel( new FlowLayout( ) );
		faqPanel.setOpaque(false);
		faqPanel.add( faqText );
	}
	public void actionPerformed( ActionEvent ae )
	{
		Object src = ae.getSource( );
		center.removeAll( );
		// add dark separator as a blank colored panel in NORTH of center panel
		separator = new JPanel( );
		separator.setBackground ( darkColor );
		separator.setPreferredSize( new Dimension( 10, 20 ) );
		center.add( separator, BorderLayout.NORTH );
		if ( src == home )
		{
			center.add( welcome, BorderLayout.CENTER );
		}
		else if ( src == faq )
		{
			center.add( faqPanel, BorderLayout.CENTER );
		}
		center.repaint( );
		validate( );
		repaint( );
	}
}

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