JumpStart Notes for Assignment 11.


CSCI A202 - Introduction to Programming (II)

Lecture 24: The Abstract Windowing Toolkit

Please explore the links embedded in this code with careful observation.

tucotuco.cs.indiana.edu% ls -l
total 10
-rw-r--r--   1 dgerman  students    1911 Apr 11 16:50 Application.java
-rw-r--r--   1 dgerman  students     279 Apr 11 16:17 CentigradeListener.java
-rw-r--r--   1 dgerman  students     279 Apr 11 16:17 FahrenheitListener.java
-rw-r--r--   1 dgerman  students     481 Apr 11 16:21 OurWindowListener.java
tucotuco.cs.indiana.edu% more *.java
::::::::::::::
Application.java
::::::::::::::
import java.awt.*;
 
public class Application extends Frame {
 
  private Label     fahrenheitLabel; 
  private TextField fahrenheitField; 
 
  private Label     centigradeLabel; 
  private TextField centigradeField; 
 
  private Button    fahrenheitButton; 
  private Button    centigradeButton; 
 
  public Application() {
    fahrenheitLabel  = new Label("Degrees Fahrenheit"); 
    fahrenheitField  = new TextField("212", 6); 
 
    centigradeLabel  = new Label("Degrees Centigrade"); 
    centigradeField  = new TextField("100", 6); 
 
    fahrenheitButton = new Button("Compute Fahrenheit"); 
    centigradeButton = new Button("Compute Centigrade"); 
 
    FlowLayout layout = new FlowLayout(); 
    setLayout (layout); 
 
    add(fahrenheitLabel); 
    add(fahrenheitField); 
    add(centigradeLabel); 
    add(centigradeField); 
    add(fahrenheitButton); 
    add(centigradeButton); 
 
    fahrenheitButton.
      addActionListener(
        new FahrenheitListener(
          this)); 
 
    centigradeButton.
      addActionListener(
        new CentigradeListener(
          this)); 
 
    addWindowListener(
      new OurWindowListener()); 
 
  } // end of constructor Application() 
 
  public void computeFahrenheit() {

    String str = centigradeField.getText().trim(); 
    int centigrade = (new Integer(str)).intValue(); 
    int fahrenheit = centigrade * 9 / 5 + 32; 
    fahrenheitField.setText("" + fahrenheit); 

  } // end of computeFahrenheit()  
 
  public void computeCentigrade() {
 
    String str = fahrenheitField.getText().trim(); 
    int fahrenheit = (new Integer(str)).intValue(); 
    int centigrade = (fahrenheit - 32) * 5 / 9; 
    centigradeField.setText("" + centigrade); 
 
  } // end of computeCentigrade()  

  public static void main(String[] args) {
 
    Frame frm = new Application(); 
    frm.setSize(250, 150); 
    frm.setVisible(true); 
 
  } // end of main(String[] args) method
} 
::::::::::::::
CentigradeListener.java
::::::::::::::
import java.awt.event.*;
 
public class CentigradeListener implements ActionListener {
 
  private Application app; 
 
  public CentigradeListener(Application app) { 
    this.app = app; 
  } 
 
  public void actionPerformed(ActionEvent e) {
    app.computeCentigrade(); 
  } 
 
} 
::::::::::::::
FahrenheitListener.java
::::::::::::::
import java.awt.event.*;
 
public class FahrenheitListener implements ActionListener {
 
  private Application app; 
 
  public FahrenheitListener(Application app) { 
    this.app = app; 
  } 
 
  public void actionPerformed(ActionEvent e) {
    app.computeFahrenheit(); 
  } 
 
::::::::::::::
OurWindowListener.java
::::::::::::::
import java.awt.event.*;
 
public class OurWindowListener implements WindowListener {
 
  public void windowClosing (WindowEvent e) {
    System.exit(0); 
  } 

  // so why do we need the empty definitions below? 
 
  public void windowActivated   (WindowEvent e) { } 
  public void windowClosed      (WindowEvent e) { } 
  public void windowDeactivated (WindowEvent e) { } 
  public void windowDeiconified (WindowEvent e) { } 
  public void windowIconified   (WindowEvent e) { } 
  public void windowOpened      (WindowEvent e) { } 
 
}