[Image]

Stats

Inputs data items (numbers) from the user and displays them in a List box.  Accepts "bad data" but does not add it into the Total.

Swing App Structure

This is a complete (but highly simplified) Swing application.  It uses an AWT List component because that is simpler to implement than the JList component.. Java applications can mix AWT and Swing components.

Overall Structure

The program (class) has 5 sections:

  1. main
    Starts the program by making a copy of the class (instantiating), and making it visible
  2. components
    The components are created outside any methods.  Then they are usable (accessible) throughout the program.
  3. constructor
    The constructor adds the components to the window (Container), and connects their actions to the actionPerformed method.
  4. actionPerformed
    Responds to buttons being clicked (handles events).  Each button (JButton) normally executes one of the methods below.
  5. further methods
    As many methods as the program needs.  In this case, it needs two methods:  typeData and calcTotal.  It is possible to do all the programming inside the actionPerformed method, but this make a very messy program.

This overall structure is not a requirement - in fact, this is not really the "preferred" approach at all. The actionPerformed event handler is an "old-fashioned" approach.  But this structure is recommended for beginners as it is straightforward.  It also corresponds to a sensible "top-down" design strategy.


Important Commands

app.setVisible(true);
Necessary - otherwise the window will not appear.

app.addWindowListener....
...{
System.exit(
0);}

This very long set of commands makes the [x] at the top right of the window function properly (close).

Container pane = getContentPane();
... pane.add(bTypeData);
A bit different than AWT apps.  In Swing, the components are added to a ContentPane, not directly to the Frame.  The AWT approach is simpler but less flexible.

try{...} 
catch(Exception e){...}
 
Handles run-time errors.  The commands in the Try block are executed until an error occurs.  If an error occurs, it stops executing the try commands, then skips to the Catch block and executes those commands.  If no error occurs, the Catch block is not executed.  You can "wrap" any set of commands in a try..catch.. structure (but only inside a method.)


Comments

Swing is the modern GUI concept for Java applications.  AWT is the "old-fashioned" approach.  

AWT uses "native" components from the operating system.  That means Java executes native Windows .dll libraries to create Buttons and TextFields.  

Swing draws all the components itself.  This allows an app to control its own "look and feel" directly.  In AWT, you are stuck with the Windows (or Linux or Mac) standard appearance.

AWT means the look and feel will be familiar for users, matching the normal behavior of their OS.  Swing means the look and feel can be constant across different platforms, and the user can adjust it to suit their taste.

Both AWT and Swing have advantages.  For a beginning programmer, AWT is a bit simpler, so that might be the better choice until you gain more experience and skills.