Page 725 - Introduction to Programming with Java: A Problem Solving Approach
P. 725
{
}
break;
} while (response == JOptionPane.CANCEL_OPTION);
System.out.println("Hello " + name);
} // end main
} // end class UncertainHello
14. [after §16.14] By calling setEnabled(false), you can disable a button and give it a muted appearance and make its listener unresponsive to clicks on it. Modify Figure 16.12’s program so that the factorial button is initially disabled. Enable it only after the user enters a character in the xBox text box. To enable it, create a key listener for the xBox text box, and have the key listener’s keyTyped event handler call setEnabled(true). Use the following key listener code skeleton:
private class KeyListener extends KeyAdapter
public void keyTyped(KeyEvent e)
{
}
...
} // end class KeyListener
Noteextends KeyAdapterintheaboveclassheading.Anadapterclassimplementsaninterfaceby
providing an empty-bodied method for each method in the interface. In this case, the KeyAdapter API
class implements the KeyListener API interface.
Apago PDF Enhancer
15. [after §16.16] To set a JFrame’s background color, what method should you call right before calling setBackground?
16. [after §16.17] What do the letters in “awt” stand for?
Review Question Solutions
1. A listener is an object that waits for events to occur.
2. An event handler is a method that responds to an event.
3. setDefaultCloseOperation(EXIT_ON_CLOSE);
4. The superclass for objects that contain other objects is the Container class.
5. Many J-prefixed components are defined in the javax.swing package.
6. JLabel hello = new JLabel("Hello World!");
7. JTextField input = new JTextField(10);
8. component.addActionListener(responder);
9. For a class to handle an event, add this to the right side of the class’s heading: implements ActionListener
10. The heading of the method specified by the ActionListener interface is: public void actionPerformed(ActionEvent e)
Review Question Solutions 691