import java.util.*;
import javax.swing.*;


/**
 * A simple example of a GUI application 
 *
 * Note: This example contains a common mistake made by beginning
 * programmers.  Specifically, it manipulates GUI elements
 * outside of the event dispatch thread
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class BadRandomMessageApplication
{
    // The pseudo-random number generator
    private static Random         rng = new Random();    

    // 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."
    };
    
    
    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws Exception
    {
       JFrame                    window;       
       JLabel                    label;       
       JPanel                    contentPane;
       String                    s;
       
       // Select a message at random
       s = createRandomMessage();

       // Construct the "window"
       window = new JFrame();
       window.setSize(600,400);
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
       
       // Get the container for all content
       contentPane = (JPanel)window.getContentPane();
       contentPane.setLayout(null);  
     
       // Add a component to the container
       label = new JLabel(s, SwingConstants.CENTER);
       label.setBounds(50,50,500,100);       
       contentPane.add(label);    

       // Make the "window" visible
       window.setVisible(true);
    }
    
    
    /**
     * "Create" a message at random
     *
     * @return   The message
     */
    private static String createRandomMessage()
    {
       return  MESSAGES[rng.nextInt(MESSAGES.length)];
    }
}
