19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

612 Chapter 16 Event-Driven <strong>Programming</strong><br />

✓Point✓ Check<br />

16.9 If class A is an inner class in class B, what is the .class file for A? If class B contains<br />

two anonymous inner classes, what are the .class file names for these two classes?<br />

16.10 What is wrong in the following code?<br />

import java.swing.*;<br />

import java.awt.*;<br />

public class Test extends JFrame {<br />

public Test() {<br />

JBut<strong>to</strong>n jbtOK = new JBut<strong>to</strong>n("OK");<br />

add(jbtOK);<br />

}<br />

}<br />

private class Listener<br />

implements ActionListener {<br />

public void actionPerform<br />

(ActionEvent e) {<br />

System.out.println<br />

(jbtOK.getActionCommand());<br />

}<br />

}<br />

/** Main method omitted */<br />

(a)<br />

import java.awt.event.*;<br />

import javax.swing.*;<br />

public class Test extends JFrame {<br />

public Test() {<br />

JBut<strong>to</strong>n jbtOK = new JBut<strong>to</strong>n("OK");<br />

add(jbtOK);<br />

jbtOK.addActionListener(<br />

new ActionListener() {<br />

public void actionPerformed<br />

(ActionEvent e) {<br />

System.out.println<br />

(jbtOK.getActionCommand());<br />

}<br />

} // Something missing here<br />

}<br />

}<br />

/** Main method omitted */<br />

(b)<br />

16.11 What is the difference between the setSize(width, height) method and the<br />

pack() method in JFrame?<br />

Key<br />

Point<br />

16.6 Alternative Ways of Defining Listener Classes<br />

Using an inner class or an anonymous inner class is preferred for defining listener<br />

classes.<br />

There are many other ways <strong>to</strong> define the listener classes. For example, you can rewrite Listing<br />

16.4 by creating just one listener, register the listener with the but<strong>to</strong>ns, and let the listener<br />

detect the event source—that is, which but<strong>to</strong>n fires the event—as shown in Listing 16.5.<br />

LISTING 16.5<br />

DetectSourceDemo.java<br />

1 import javax.swing.*;<br />

2 import java.awt.event.*;<br />

3<br />

4 public class DetectSourceDemo extends JFrame {<br />

5 // Create four but<strong>to</strong>ns<br />

6 private JBut<strong>to</strong>n jbtNew = new JBut<strong>to</strong>n("New");<br />

7 private JBut<strong>to</strong>n jbtOpen = new JBut<strong>to</strong>n("Open");<br />

8 private JBut<strong>to</strong>n jbtSave = new JBut<strong>to</strong>n("Save");<br />

9 private JBut<strong>to</strong>n jbtPrint = new JBut<strong>to</strong>n("Print");<br />

10<br />

11 public DetectSourceDemo() {<br />

12 // Create a panel <strong>to</strong> hold but<strong>to</strong>ns<br />

13 JPanel panel = new JPanel();<br />

14 panel.add(jbtNew);<br />

15 panel.add(jbtOpen);<br />

16 panel.add(jbtSave);<br />

17 panel.add(jbtPrint);<br />

18

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!