A First BreezySwing Program

The following GUI-based application computes a personÕs income tax, based on a mythical tax code having a flat tax rate of 15%. 


The user can enter the three inputs in any order, and back out of an entry by editing it, before clicking the button to compute and display the result.  Because the window stays alive after the Compute button is pressed, the user can subsequently adjust one input and leave the other previous inputs alone to compute and view a different result.  Unlike terminal- and dialog-based applications, which guide the user through a determinate sequence of steps, a GUI-based application puts the user in charge of deciding which actions to perform. To give the kind of control to the user just mentioned, a GUI-based program must do four basic things:

1. Lay out and pop up a window with the appropriate data fields, buttons, and other controls.

2. Wait for the user to perform an action, such as pressing a button or selecting text in a field.

3. Detect a userÕs action (also called an event).  

4. Respond appropriately to each type of user action. We call this type of programming event-driven, because the programÕs behavior is driven by user events.  The good news is that the runtime system handles the second and third of the four tasks – waiting for and detecting user events – mentioned above.  The other two tasks, laying out the window and responding to user events, are a matter of writing code belonging to a window class.  The GUI-based tax calculator program shows clearly where these two tasks are performed.


import javax.swing.*;

import BreezySwing.*;


public class TaxCodeDemo extends GBFrame{


    // Set up the widgets by adding them to rows and columns in the window's grid

    JLabel incomeLabel           = addLabel ("Income",           1,1,1,1);

    DoubleField incomeField      = addDoubleField (0.0,          1,2,1,1);

    JLabel dependentsLabel       = addLabel ("Dependents",       2,1,1,1);

    IntegerField dependentsField = addIntegerField (0,           2,2,1,1);

    JLabel exemptionLabel        = addLabel ("Exemption amount", 3,1,1,1);

    DoubleField exemptionField   = addDoubleField (0.0,          3,2,1,1);

    JButton convertButton        = addButton ("Compute",         4,1,2,1);

    JLabel taxLabel              = addLabel ("Total tax",        5,1,1,1);

    DoubleField taxField         = addDoubleField (0.0,          5,2,1,1);


   // The event handler method for the button to compute the tax     

   public void buttonClicked(JButton buttonObj){

        double income = incomeField.getNumber();

        int numDependents = dependentsField.getNumber();

        double exemptionAmount = exemptionField.getNumber();

        double tax = (income - numDependents * exemptionAmount) * .15;





   // Create and display the window when the app launches

   public static void main(String[] args){

      JFrame frm = new TaxCodeDemo();

      frm.setTitle("Tax Calculator");

      frm.setSize (300, 200);

      frm.setVisible (true);




DonÕt worry if you donÕt understand all of the details of this program.  The rest of the tutorial will guide you through such matters as the overall structure of a GUI program, laying out each type of widget in a window, and creating code to respond to user events.


Back to tutorial Next topic: The breezy skeleton