Introduction to graphical programming and the java.awt package

 

Graphical programming

 

Event-driven programs

 

The Abstract Windows Toolkit (AWT)

 

Anatomy of a small windows program

The following is the source code of a small Java program to create and display a window having a single button:

import java.awt.*;
import java.awt.event.*;

public class App extends Frame implements WindowListener, ActionListener {

  Button b;

  public static void main(String[] args) {
    App myWindow = new App("My first window");
    myWindow.setSize(250,75);
    myWindow.setVisible(true);
  }

  public App(String title) {
    super(title);
    setLayout(new FlowLayout());
    addWindowListener(this);
    b = new Button("Click me");
    add(b);
    b.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
    b.setLabel("Ouch!!");
  }

  public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
  }

  public void windowOpened(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
}

Notes:

  1. The import statements make it easy to access the necessary packaged AWT code

  2. By extending the Frame class, the App class inherits the features and look of a frame window

  3. The WindowListener interface is implemented in order to be notified of certain window events (such as iconifying, deiconifying, and closing the window)

  4. The ActionListener interface is implemented in order to be notified of certain action events to container components (such as a button being clicked)

  5. The Button reference, b, is an instance variable of the class

  6. The main() method is the first method executed. It instantiates the frame, sets its size, and makes it visible.

  7. The class constructor receives the frame's title as a parameter which it passes through to the superclass constructor. It then sets the layout manager of the frame to FlowLayout, registers a WindowListener so the frame will be notified of window events, instantiates the button and adds it to the frame, and registers an ActionListener for the button so the frame will be notified when the user clicks the button.

  8. The actionPerformed() method is required by the ActionListener interface. It is automatically called when the user clicks the button and receives an ActionEvent object as a parameter. In this sample, it handles the event by changing the label of the button.

  9. The windowClosing() method is required by the WindowListener interface. It is automatically called when the user closes the frame and receives a WindowEvent object as a parameter. In this sample, it disposes of frame resources and terminates the JVM.

  10. The remaining methods are required by the WindowListener interface. In this sample, they do nothing.

 

Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.

 

Review questions

  1. Assume a class named MyFrame properly extends Frame and has a constructor that accepts the frame's title as a parameter and sets the label component "Hello World!" within the frame's container. What will result from an attempt to compile and execute the following main() method?

1
2
3
4
public static void main(String[] args) {
  MyFrame x = new MyFrame("Here goes");
  x.setSize(50,200);
}
  1. the label "Hello World!" will display in a frame that is wider than it is tall

  2. the label "Hello World!" will display in a frame that is taller than it is wide

  3. the program will compile but nothing will display

  4. a compile error will occur at line 2

  5. a compile error will occur at line 3

  1. Which of the following ends the processing of a Windows program written in Java? 

  1. System.exit(0);

  2. dispose();

  3. reaching the end of the main() method

  4. the user clicking the close button for the window 

  5. windowClosing(this);

  1. If zButton has been properly defined as the reference for a Button object, what will happen during program execution if the button is instantiated using only the following code?  (choose four)

1
2
zButton = new Button("YES");
add(zButton);
  1. the button will have the label "YES"

  2. the button component will be part of the frame's container

  3. the actionPerformed() method will be automatically called when the user clicks the button

  4. the button will remain in memory if the frame window is iconified

  5. the button's text could be changed by executing the statement:   zButton.setLabel("NO");

  1. Which of the following methods is not required by the WindowListener interface?

  1. windowOpened(WindowEvent e)

  2. windowActivated(WindowEvent e)

  3. windowResized(WindowEvent e)

  4. windowDeiconified(WindowEvent e)

  5. windowClosed(WindowEvent e)