Graphical programming
Is used in Windows applications as well as web-based applets.
Provides a Graphical User Interface (GUI). It incorporates the mouse for user input and more visual display features, such as color images, for output.
Typically displays multiple controls (menus, buttons, scroll bars, etc.) upon which the user may act
Puts the user in charge. While text-based programs rigidly lead the user via prompts and simple menus, graphical programs provide a wide range of choices and let the user determine the order of actions.
Is event-driven and far more complex internally than text-based programming. The logic path is difficult to follow.
Event-driven programs
Define, arrange, and display components upon which the user may act. Typical components include buttons, text boxes, check boxes, radio buttons, scroll bars, and menu bars. The variety is nearly infinite.
Ask the JVM to be notified whenever the user acts upon specified components (clicks a button, selects a menu item, etc.). Such actions are called events.
Contain methods to handle each event. These perform the bulk of application or applet processing. What they do depends upon the application or applet and which control the user activated.
Are difficult to read. Logic does not flow from top to bottom like a procedural program. Rather, it jumps from one event handler to another in response to user actions.
The Abstract Windows Toolkit (AWT)
Is contained in the java.awt package. It consists of classes for creating user interfaces and for painting graphics and images.
Helps create components. In AWT terminology, a user interface object (such as a button) is a component. The Component class is the root of all AWT components.
Helps organize and arrange components. A container is a component that can contain components and other containers. A container can also have a layout manager that controls the visual placement of components within the container. The AWT package contains several layout manager classes and an interface for building your own layout manager.
Helps identify and respond to component events. Some components fire events when a user interacts with the components. The AWTEvent class and its subclasses are used to represent the events that AWT components can fire.
Is complex. Refer to the help facility of your development environment or the official Sun API specification for details.
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:
The import statements make it easy to access the necessary packaged AWT code
By extending the Frame class, the App class inherits the features and look of a frame window
The WindowListener interface is implemented in order to be notified of certain window events (such as iconifying, deiconifying, and closing the window)
The ActionListener interface is implemented in order to be notified of certain action events to container components (such as a button being clicked)
The Button reference, b, is an instance variable of the class
The main() method is the first method executed. It instantiates the frame, sets its size, and makes it visible.
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.
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.
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.
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
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
4public static void main(String[] args) {
MyFrame x = new MyFrame("Here goes");
x.setSize(50,200);
}
the label "Hello World!" will display in a frame that is wider than it is tall
the label "Hello World!" will display in a frame that is taller than it is wide
the program will compile but nothing will display
a compile error will occur at line 2
a compile error will occur at line 3
Which of the following ends the processing of a Windows program written in Java?
System.exit(0);
dispose();
reaching the end of the main() method
the user clicking the close button for the window
windowClosing(this);
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
2zButton = new Button("YES");
add(zButton);
the button will have the label "YES"
the button component will be part of the frame's container
the actionPerformed() method will be automatically called when the user clicks the button
the button will remain in memory if the frame window is iconified
the button's text could be changed by executing the statement: zButton.setLabel("NO");
Which of the following methods is not required by the WindowListener interface?
windowOpened(WindowEvent e)
windowActivated(WindowEvent e)
windowResized(WindowEvent e)
windowDeiconified(WindowEvent e)
windowClosed(WindowEvent e)