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.
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.
The program (class) has 5 sections:
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.
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.)
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.