Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

206 HOUR 15:Responding to User Input


26:
27: public voidkeyPressed(KeyEvent txt) {
28: // do nothing
29: }
30:
31: public voidkeyReleased(KeyEvent txt) {
32: // do nothing
33: }
34:
35: private voidsetLookAndFeel() {
36: try {
37: UIManager.setLookAndFeel(
38: “com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”
39: );
40: } catch(Exception exc) {
41: // ignore error
42: }
43: }
44:
45: public static voidmain(String[] arguments) {
46: KeyViewer frame = new KeyViewer();
47: }
48: }

When you run the application, it should resemble Figure 15.1.

LISTING 15.1 Continued

FIGURE 15.1
Handling keyboard eventsin a
program.


Enabling and Disabling Components
You might have seen a component in a program that appears shaded
instead of its normal appearance.
Shading indicates that users cannot do anything to the component because
it is disabled. Disabling and enabling components is accomplished with
the setEnabled()methodof the component. ABoolean value is sent as an
argument to the method, so setEnabled(true)enables a component for
use, and setEnabled(false)disables it.
The following statements create buttons with the labels Previous, Next,
and Finish and disable the first button:
JButton previousButton = new JButton(“Previous”);
JButton nextButton = new JButton(“Next”);
Free download pdf