import java.awt.event.*;
import java.util.*;
import javax.swing.*;


/**
 * A simple example of a GUI application that responds
 * to events (in this case, events generated by a button)
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
//[declaration.
public class      BadInteractiveRandomMessageSwingApplication
       implements ActionListener, Runnable
//]declaration.
{
    // Attributes
    private JLabel                label;       

    // The pseudo-random number generator
    private static Random         rng = new Random();    

//[constants.
    // String "constants"
    private static final String   CHANGE = "Change";    
//]constants.

    // The messages
    private static final String[] MESSAGES = 
    {
       "What a great example!",
       "This class is great.",
       "I can't wait to do the programming assignments.",
       "I wish lectures lasted for 5 hours.",
       "I've never had a better Professor."
    };


//[main.
    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws Exception
    {
       SwingUtilities.invokeAndWait(
                  new BadInteractiveRandomMessageSwingApplication());
    }
//]main.

//[actionPerformed.
    /**
     * Handle actionPerformed messages
     * (required by ActionListener)
     *
     * @param event   The ActionEvent that generated the message
     */
    public void actionPerformed(ActionEvent event)
    {
       String      actionCommand;
       
       actionCommand = event.getActionCommand();
       if (actionCommand.equals(CHANGE))
       {
          label.setText(createRandomMessage());          
       }
    }
//]actionPerformed.    

    /**
     * "Create" a message at random
     *
     * @return   The message
     */
    private static String createRandomMessage()
    {
       return  MESSAGES[rng.nextInt(MESSAGES.length)];
    }
    
    /**
     * The code to be executed in the event dispatch thread
     * (required by Runnable)
     */
    public void run()
    {
       JButton                   button;       
       JFrame                    window;     
       JPanel                    contentPane;
       String                    s;
       
       // Select a message at random
       s = createRandomMessage();

       // Construct the "window"
//[jframe.
       window = new JFrame();
       window.setSize(600,400);
//]jframe.
       window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       
       
       // Get the container for all content
//[contentPane0.
       contentPane = (JPanel)window.getContentPane();
//]contentPane0.
//[contentPane1.
       contentPane.setLayout(null);  
//]contentPane1.
       
       // Add the message component to the container
//[label0.
       label = new JLabel(s, SwingConstants.CENTER);
//]label0.
//[label1.
       label.setBounds(50,50,500,100);       
       contentPane.add(label);    
//]label1.

       // Add the button to the container
//[button0.
       button = new JButton(CHANGE);
//]button0.
//[button1.
       button.setBounds(450,300,100,50);
       contentPane.add(button);
//]button1.
//[button2.
       button.addActionListener(this);       
//]button2.

       // Make the "window" visible
       window.setVisible(true);
    }
}
