A good answer might be:

  • How many GUI components are there?
    • Eight! Each one will have to be added to the frame.
  • Which of the components will need to register a listener?
    • Only two of the components:
      • The "close-window" button of the frame, and the
      • The "Do It" button.
    • The user can type text into a TextField even if it does not have a listener. getText() will still work.

GUI Components of the Program

The complete application (when we finish with it) is 82 lines long. All this to implement a one-line formula for percentage fat! For short applications, this is typical. But for longer applications the percentage of code for the GUI part is about 40%.

Let us build the program one piece at a time. Look at the GUI at the left and decide what should go into the program. Click the buttons to see if you are correct.


import
import
public class percentFat extends Frame implements
{
Label title = new Label(" ");
Label fatLabel = new Label(" ");
Label calLabel = new Label(" ");
Label perLabel = new Label(" ");
TextField inFat = new TextField( 7 ); TextField inCal = new TextField( 7 ); TextField outPer = new TextField( 7 );
doit = new Button("Do It!");
int calories ; // input from the user: total calories per serving int fatGrams ; // input from the user: grams of fat per serving double percent ; // result: percent of calories from fat percentFat() // constructor { . . . . . . }

QUESTION 12:

Click!

Click Here after you have answered the question