Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.

Similar presentations


Presentation on theme: "1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers."— Presentation transcript:

1 1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers  Adding Components to Windows.  Preview: Graphical User Interfaces (GUI) with AWT and Swing.

2 2 lecture 12Lecture 13 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 in your JavaCD for details of these methods etc): 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.

3 3 lecture 12Lecture 13 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(); }}

4 4 lecture 12Lecture 13 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 WindowAdapte r 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(); }

5 5 lecture 12Lecture 13 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(); }

6 6 lecture 12Lecture 13 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

7 7 lecture 12Lecture 13 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.

8 8 lecture 12Lecture 13 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:"); TextField tally = new TextField(10); int sum = 0; public CountButtonPushes() { super("A Container With Components"); setSize(500,500); setLayout(new FlowLayout()); add ( total ); add ( tally ); add ( button ); button.addActionListener(this); show(); } public void actionPerformed( ActionEvent e ) { sum = sum + 1; // add number to sum tally.setText(Integer.toString(sum)); } public static void main(String args [] ) { new CountButtonPushes(); }

9 9 lecture 12Lecture 13 Example 4 (cont.)  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.

10 10 lecture 12Lecture 13 Example 5: Enhancing Example 4. import java.awt.*;import java.awt.event.*; public class CountButtonPushesWithReseter extends Frame{ Button button = new Button("Press me"); Button reset = new Button("Reset total"); Label total = new Label( "Running total:"); TextField tally = new TextField(10); int sum = 0; public CountButtonPushesWithReseter() { super("A Container With Components"); setSize(500,500); setLayout(new FlowLayout()); add(total); add(tally); add (button); add (reset); button.addActionListener(new ButtonListener()); reset.addActionListener(new ResetListener()); show(); } class ButtonListener implements ActionListener{ public void actionPerformed ( ActionEvent e ) { sum = sum + 1; // add number to sum tally.setText(Integer.toString(sum)); }

11 11 lecture 12Lecture 13 Example 5: Enhancing Example 4 (cont.) 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(); }


Download ppt "1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers."

Similar presentations


Ads by Google