Java Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class RanQuoteAL extends JApplet
implements ActionListener
{
JButton next;
JLabel label;
Random random = new Random( );
ArrayList<String> quotes;
public void init( )
{
setLayout( new BorderLayout( ) );
quotes = new ArrayList<String>( );
quotes.add( "What's up doc?" );
quotes.add( "Have you any Grey Poupon?" );
quotes.add( "Can you hear me now?" );
quotes.add( "<HTML><CENTER>What is the average ground speed <BR> of an unladen swallow?" );
quotes.add( "As you wish" );
next = new JButton( "Next!" );
next.addActionListener( this );
label = new JLabel( "Good morning!", JLabel.CENTER );
label.setFont( new Font( "Serif", Font.BOLD, 16 ) );
add( next, BorderLayout.NORTH );
add( label, BorderLayout.CENTER );
}
public void actionPerformed( ActionEvent ae )
{
Object obj = ae.getSource( );
if ( obj == next )
{
if ( quotes.size( ) > 0 )
{
int index = random.nextInt( quotes.size( ) );
label.setText( quotes.get( index ) );
quotes.remove( index );
}
else
label.setText( "I have nothing more to say." ); // ran out of quotes
}
}
}
©2008 by E.S.Boese. All Rights Reserved