Two Skeletal Forms of BreezySwing Programs

The First Skeletal Form

The simplest form of a GUI-based program, such as the tax calculator shown earlier, is shown in this template:

 

import javax.swing.*;

import BreezySwing.*;

 

Other imports

 

public class ApplicationName extends GBFrame{

        

    Add window components to the window

    Definitions of event handler methods

}

 

public static void main(String[] args){

    JFrame frm = new ApplicationName();

 

    Set the window attributes

 

    frm.setVisible(true);

}

         

 

A GUI application window is always represented as a class that extends GBFrame The code within this class adds the GUI components, such as labels, fields, and buttons, to the window.  The event handler methods, also defined within this class, provide the responses of the application to user events.  The main method always creates an instance of the application window class, sets its attributes, and pops up the window.   The window then waits for user events.

The Second Skeletal Form 

More sophisticated GUI applications call for other things to be done at program startup. For example, the program might have to set the attributes of a window component, such as its text font and color, after that component is created. The program might also manage other data, in the form of instance variables, as well. In these applications, it is best to place the setup code in a constructor. The constructor looks like another Java method, but must have the name of its class and have no return type. The constructor is placed within the window class, below the instance variable declarations, as shown in the following template:

 

import javax.swing.*;

import BreezySwing.*;

 

Other imports

 

public class ApplicationName extends GBFrame{

 

    Declare the instance variables, including those for the window components

         

    public ApplicationName(){

 

        Add window components to the window

 

        Initialize other instance variables

    }

 

    Definitions of event handler methods

}

 

public static void main(String[] args){

    JFrame frm = new ApplicationName();

 

    Set the window attributes

 

    frm.setVisible(true);

}

 

Here is a new version of the tax calculator program that uses this second template:

 

import javax.swing.*;

import BreezySwing.*;

 

public class TaxCodeDemoV2 extends GBFrame{

 

    // Declare the instance variables for the window components

    JLabel incomeLabel;

    DoubleField incomeField;

    JLabel dependentsLabel;

    IntegerField dependentsField;

    JLabel exemptionLabel;

    DoubleField exemptionField;

    JButton convertButton;

    JLabel taxLabel;

    DoubleField taxField;

 

   // The constructor, which adds the window components to the window    

   public TaxCodeDemoV2(){

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

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

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

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

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

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

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

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

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

        taxField.setPrecision(2);

    }

   // 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;

        taxField.setNumber(tax);

    }

 

   // Create and display the window when the app launches

   public static void main(String[] args){

      JFrame frm = new TaxCodeDemoV2();

      frm.setTitle("Tax Calculator");

      frm.setSize (300, 200);

      frm.setVisible (true);

   }

}

 

Note that the window components are now added to the window in the constructor, and that the precision of the tax field is also set there, instead of in the buttonClicked method. We will use both templates as needed in the remaining examples in this tutorial.

 

Back to tutorial Next topic: The application window and its attributes