Download public void windowClosing(WindowEvent we)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Graphical User Interfaces (Part II)
Overview
 Handling Window Events.
 Event Adapters Revisited.
 Introduction to Components and Containers
 Adding Components to Windows.
 Coming Next: GUI Programming (Part III).
GUI Programming (Part II).
1
Handling Window Events




In Lecture 11 we considered simple examples of handling keyboard and mouse
events. We will now consider handling window events.
As in the examples in Lecture 11, we need to implement the WindowListener
interface in order to handle window events.
The WindowListener interface defines seven methods (see the Java API
documentation for details of these methods):
void windowActivated(WindowEvent we);
void windowClosed(WindowEvent we);
void windowClosing(WindowEvent we);
void windowDeactivated(WindowEvent we);
void windowDeiconified(WindowEvent we);
void windowIconified(WindowEvent we);
void windowOpened(WindowEvent we);
In the following example, we are only interested in responding to the
windowclosing event so that we ensure that the window closes on a
windowclosing event.
GUI Programming (Part II).
2
Example 1: Handling Window Events
import java.awt.*;import java.awt.event.*;
public class ClosingMyWindow extends Frame implements WindowListener{
public ClosingMyWindow() {
super("An Empty Frame You Can Close!");
setSize(400,400);
addWindowListener(this);
show();
}
public void windowClosing(WindowEvent we) {
System.exit(0);
}
public void windowClosed(WindowEvent we) {}
public void windowIconified(WindowEvent we) {}
public void windowDeiconified(WindowEvent we) {}
public void windowActivated(WindowEvent we) {}
public void windowDeactivated(WindowEvent we) {}
public void windowOpened(WindowEvent we) {}
public static void main(String args [] ) {
new ClosingMyWindow();
}}
GUI Programming (Part II).
3
Example 2: Using Event Adapters



The above program has an obvious disadvantage because only the
windowClosing() handler has a meaningful body and all the other remaining
six methods are stubs.
As we have discussed in Lecture 9, this is an ideal place to make use of event
adapters to make our programming easier.
Here is an equivalent program using the WindowAdapter class:
import java.awt.*;import java.awt.event.*;
class MyWindowListener extends
WindowAdapter{
public void windowClosing(WindowEvent we) {
System.exit(0);
}}
class ClosingWindowUsingAdapter extends Frame{
public ClosingWindowUsingAdapter() {
super("An Empty Frame You Can Close!");
setSize(400,400);
addWindowListener(new MyWindowListener());
show();
}
public static void main(String args [] ) {
new
ClosingWindowUsingAdapter();
}
}
GUI Programming (Part II).
4
Example 3: Window Closing with Inner Classes

The following program is yet another version of the window program. The
essence of this version is to demonstrate the use of anonymous inner classes.
import java.awt.*;
import java.awt.event.*;
public class AnonymousInnerClass extends Frame {
public AnonymousInnerClass() {
super("An Empty Frame You Can Close!");
setSize(400,400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) { System.exit(0); }
});
show();
}
public static void main(String args [] ) {
new AnonymousInnerClass();
}
}
GUI Programming (Part II).
5
Introduction to Components and Containers





Before considering the next examples, we briefly introduce components and containers.
A component is an object having a graphical representation that can be displayed on the
screen and that can interact with the user. Examples of components are the buttons,
checkboxes, and scrollbars of a typical graphical user interface.
A container is a special component that can hold other components. Examples of
Containers are applets, panels and frames.
The functionality of most GUI components is derived from the Component and
Container classes in the inheritance hierarchy below.
Exercise: Study the methods of the following classes in the Java 2 SDK documentation.
java.lang.Object
|
java.awt.Component
|
java.awt.Container
|
java.awt.Window
|
java.awt.Frame
java.lang.Object
|
java.awt.Component
|
java.awt.Container
|
java.awt.Panel
|
java.awt.Applet
GUI Programming (Part II).
6
Adding Components to Windows
In the next examples, we introduce other graphics components and add them to our
Frame windows. To make the example simple, we will only add three types of
components: buttons, text fields and labels
A Button generates action events when pushed. There are many different kinds of
button (see the Java 2 SDK documentation for details).
For a class to respond to an action event, it must implement the single method in the
ActionListener interface.
A TextField is a single-line area in which text can be entered by the user from the
keyboard or text can simply be displayed.
When the user types data into a TextField and presses the Enter key, an action event
occurs.
A Label is used to provide text instructions or information on a GUI. Labels are
defined in the classes Label and JLabel.
GUI Programming (Part II).
7
Example 4: Counting Number of Button Clicks
import java.awt.*; import java.awt.event.*;
public class CountButtonPushes extends Frame
implements ActionListener {
Button
button = new Button("Press me");
Label
total = new Label( "Running total:");
public void actionPerformed( ActionEvent
e){
sum = sum + 1; // add number to sum
tally.setText(Integer.toString(sum));
TextField tally = new TextField(10);
int sum
= 0;
}
public CountButtonPushes() {
public static void main(String args [] ) {
super("A Container With Components");
new CountButtonPushes();
setSize(500,500);
}
setLayout(new FlowLayout());
add(total);
}
add(tally);
add (button);
button.addActionListener(this);
show();
}
GUI Programming (Part II).
8
Example 4 (Cont’d)
In applications with Frames and Applets, components are attached to the content
pane, which is a Container.
Frequently used methods which originate in class Container are add() and setLayout()
as used in Example 4.
The add() method is used for adding components to the content pane.
The setLayout() method is used to specify the layout manager that helps a
Container to automatically position and size its components. (We will discuss layout
managers in a little more detail in coming lecture)
Suppose we wished to add a reset button to this example so that when the reset button
is clicked, the counter is reset to zero.
Then we must change actionPerformed() since it needs to react differently
depending on which button (button or reset) is pushed.
Notice that both button and reset are going to register as listeners to the action
event but behave (i.e., implement the ActionListener interface) differently.
The modified program is shown next.
GUI Programming (Part II).
9
Example 5: Enhancing Example 4.
import java.awt.*;import java.awt.event.*;
button.addActionListener(new ButtonListener());
public class CountButtonPushesWithReseter
extends Frame{
reset.addActionListener(new ResetListener());
Button button = new Button("Press me");
Button reset = new Button("Reset total");
show();
}
class ButtonListener implements ActionListener{
Label total = new Label( "Running total:");
public void actionPerformed( ActionEvent e ) {
TextField tally = new TextField(10);
int sum
sum = sum + 1; // add number to sum
= 0;
tally.setText(Integer.toString(sum));
public CountButtonPushesWithReseter() {
super("A Container With Components");
}
}
setSize(500,500);
setLayout(new FlowLayout());
add(total);
add(tally);
add (button);
add (reset);
GUI Programming (Part II).
10
Example 5: Enhancing Example 4 (Cont’d)
class ResetListener implements ActionListener{
public void actionPerformed( ActionEvent e ) {
sum = 0; // Reset Sum to 0
tally.setText(Integer.toString(sum));
}
}
public static void main(String args [] ) {
new CountButtonPushesWithReseter();
}
}
GUI Programming (Part II).
11