Java Reference
In-Depth Information
else if (mItem == copyI)
scratchpad = pageTA.getSelectedText();
else if (mItem == pasteI)
pageTA.insert(scratchpad, pageTA.getCaretPosition());
else if (mItem == selectI)
pageTA.selectAll();
}
public static void main(String args[])
{
TextEditor texted = new TextEditor();
}
}
Sample Run: A sample run of the program with menus is shown in Figure 12-21.
FIGURE 12-21 Sample run of the TextEditor program
Key and Mouse Events
In this chapter and in the preceding chapters, you learned how to handle action events
when the user clicks a button. Moreover, Chapter 11 noted that when the Enter key is
pressed in a text field, an action event is generated. Also, recall that when a mouse button
is pressed to click a button, in addition to generating an action event, a mouse event is
generated. Likewise, when the Enter key is pressed in a text field, in addition to the
action event, a key event is generated. Therefore, a GUI program can simultaneously
generate more than one event. This section includes various programs to show how to
handle key and mouse events.
Recall from Chapter 11 that key events are handled by the interface KeyListener ,
and mouse events are handled by the interface s MouseListener and
MouseMotionListener . The key and mouse events and the corresponding event hand-
lers were shown in Table 11-14 and are reproduced in Table 12-14.
1
2
 
Search WWH ::




Custom Search