// Swing application structure
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

 public class Stats extends JFrame implements ActionListener
 {
   //--- main - starts the program running -----------
   public static void main(String[] args)     
   {
     Stats app = new Stats();         // Create the application (window)

     app.setVisible(true);            // Don't forget to make it visible
     
     app.addWindowListener            // enable [x] for closing app
     new WindowAdapter()        
       public void windowClosing(WindowEvent evt
         System.exit(0)}
       }
     );
   }

   //--- Create Components - Buttons, TextFields, etc ---
   JButton    bTypeData = new JButton("Type Data");
   List       lNumbers  = new List(10);
   JButton    bTotal    = new JButton("Total");
   JButton    bClear    = new JButton("Clear");
   JButton    bQuit     = new JButton("Exit Program");

   //--- Constructor - add components to ContentPane ----
   public Stats()
   {
     super("Stats");                     // construct Frame, set Title
     setSize(150,300);                   // set size of window

     Container pane = getContentPane();  // access display area
     pane.setLayout(new FlowLayout());   // assign Layout manager

     pane.add(bTypeData);                // add components to display
     pane.add(lNumbers);
     pane.add(bTotal);
     pane.add(bClear);
     pane.add(bQuit);

     bTypeData.addActionListener(this);  // Register component actions
     bTotal.addActionListener(this);     //  to have events handled by
     bClear.addActionListener(this);     //  this actionPerformed method
     bQuit.addActionListener(this)
   }

   //--- Responds to Buttons and other actions (events) ----
   public void actionPerformed(ActionEvent evt)
   {  
      Object source = evt.getSource();  // find which button was clicked

      if (source == bQuit)              // Use if..else if...
      System.exit(0)}               //  to choose the correct action
      else if (source == bTypeData)     //  matching the source Button 
      typeData()}
      else if (source == bTotal)
      calcTotal()}
      else if (source == bClear)
      lNumbers.removeAll()}
   }

   //--- method for entering data into List ------------
   public void typeData()
   {
      String data = "";
      do 
      data = JOptionPane.showInputDialog(this,"Data (x to stop)?");
        if (!data.substring(0,1).equals("x"))     // don't add "x" to List
        lNumbers.add(data)}
      while (!data.substring(0,1).equals("x"))// stop when "x" is typed
   }

   //--- method for calculating Total of data in List --
   public void calcTotal()
   {
       double total = 0;                          
       for (int c = 0; c < lNumbers.getItemCount(); c = c+1)
       {                                           // count through List
          String data = lNumbers.getItem(c);       // get data item
          try                                      // Trap conversion error
          double d = Double.parseDouble(data);   //  in case the user
            total = total + d;                     //  typed a non-number
          }
          catch(Exception e)                       // announce bad data
          JOptionPane.showMessageDialog(this, e.toString());
          }
       }
       JOptionPane.showMessageDialog(this,"Total = " + total);
   }
 }