import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** Prorgram to demonstrate using a variety of Swing's built-in
* message boxes.<p>
*
* Invocations:<p>
*
* <pre>
*     java DemoMessageBox
* </pre>
*
* <center><img src="DemoMessageBox.gif"></center><p>
*
* <b>Source code notes:</b><br>
*
* The error message box also beeps.  This is accomplished by adding the
* following line of code:<p>
*
* <pre>
*      Toolkit.getDefaultToolkit().beep();
* </pre>
*
* @see <a href="DemoMessageBox.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoMessageBox
{
   public static void main(String[] args)
   {
      // use look and feel for my system (Win32)
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoMessageBoxFrame frame = new DemoMessageBoxFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoMessageBox");
      frame.pack();
      frame.show();
   }
}

class DemoMessageBoxFrame extends JFrame implements ActionListener
{
   private JButton error;
   private JButton information;
   private JButton warning;
   private JButton question;
   private JButton plain;

   // constructor

   DemoMessageBoxFrame()
   {
      // -------------------------------
      // create and configure components
      // -------------------------------

      error = new JButton("Error");
      information = new JButton("Information");
      warning = new JButton("Warning");
      question = new JButton("Question");
      plain = new JButton("Plain");

      // -------------
      // add listeners
      // -------------

      error.addActionListener(this);
      information.addActionListener(this);
      warning.addActionListener(this);
      question.addActionListener(this);
      plain.addActionListener(this);

      // ------------------
      // arrange components
      // ------------------

      // put components in a panel

      JPanel buttonPanel = new JPanel();
      buttonPanel.add(error);
      buttonPanel.add(information);
      buttonPanel.add(warning);
      buttonPanel.add(question);
      buttonPanel.add(plain);
      buttonPanel.setBorder(BorderFactory.createEmptyBorder(40, 20, 40, 20));

      // create a container for this (extended) JFrame's content pane

      Container c = this.getContentPane();

      // add the panel to the container

      c.add(buttonPanel, "Center");
   }

   // -------------------------------
   // implement ActionListener method
   // -------------------------------

   public void actionPerformed(ActionEvent ae)
   {
      Object source = ae.getSource();
      if (source == error)
      {
         Toolkit.getDefaultToolkit().beep();        
         JOptionPane.showMessageDialog(this,
            "This is an 'error' message!",
            "Error",
            JOptionPane.ERROR_MESSAGE);
      }
      if (source == information)
         JOptionPane.showMessageDialog(this,
            "This is an 'information' message!",
            "Information",
            JOptionPane.INFORMATION_MESSAGE);
      if (source == warning)
         JOptionPane.showMessageDialog(this,
            "This is an 'warning' message!",
            "Warning",
            JOptionPane.WARNING_MESSAGE);
      if (source == question)
         JOptionPane.showMessageDialog(this,
            "This is a 'question' message!",
            "Question",
            JOptionPane.QUESTION_MESSAGE);
      if (source == plain)
         JOptionPane.showMessageDialog(this,
            "This is a 'plain' message!",
            "Plain",
            JOptionPane.PLAIN_MESSAGE);
   }
}

