Java Reference
In-Depth Information
Identify Code
Identify the code elements shown in Figure 6-48 using the appropriate phrase
from the following list.
literal assigned to action command
variable containing action command
variable containing selected text
WindowListener event
variable containing clipboard contents
constructed menu bar
menu bar command variable
Edit menu command variable
1
18
public
Calculator
()
19
{
20
// create an instance of the menu
21
MenuBar
mnuBar =
new
MenuBar
()
;
22
setMenuBar
(
mnuBar
)
;
23
24
Menu
mnuEdit =
new
Menu
(
"Edit"
,
true
)
;
25
mnuBar.add
(
mnuEdit
)
;
26
MenuItem
mnuEditClear =
new
MenuItem
(
"Clear"
)
;
27
mnuEdit.add
(
mnuEditClear
)
;
28
mnuEdit.insertSeparator
(
1
)
;
29
MenuItem
mnuEditCopy =
new
MenuItem
(
"Copy"
)
;
30
mnuEdit.add
(
mnuEditCopy
)
;
31
MenuItem
mnuEditPaste =
new
MenuItem
(
"Paste"
)
;
32
mnuEdit.add
(
mnuEditPaste
)
;
33
34
35
2
mnuEditCopy.addActionListener
(
this
)
;
36
mnuEditPaste.addActionListener
(
t
his
)
;
3
37
38
mnuEditCopy.setActionCommand
(
"Copy"
)
;
39
mnuEdi
tPaste.setActionCommand
(
"Paste"
)
;
4
40
41
addWindowListener
(
42
new
WindowAdapter
()
43
{
44
public void
windowClosing
(
WindowEvent
e
)
5
45
{
46
System
.exit
(
0
)
;
47
}
48
}
49
)
;
50
51
}
// end of constructor method
52
53
public void
actionPerformed
(
ActionEvent
e
)
54
{
55
//test for menu item clicks
56
String
arg = e.getActionCommand
()
;
6
57
58
if
(
arg ==
"Copy"
)
59
{
60
Clipboard
cb =
Toolkit
.getDefaultToolkit
()
.getSystemClipboard
()
;
61
StringSelection
contents =
new
StringSelection
(
lcd.getText
())
;
62
cb.setContents
(
contents,
null
)
;
63
}
7
8
64
65
if
(
arg ==
"Paste"
)
66
{
67
Clipboard
cb =
Toolkit
.getDefaultToolkit
()
.getSystemClipboard
()
;
68
Transferable
content = cb.getContents
(
this
)
;
69
try
70
{
71
String
s =
(
String
)
content.getTransferData
(
DataFlavor
.stringFlavor
)
;
72
lcd.setText
(
calcPattern.format
(
Double
.parseDouble
(
s
)))
;
73
}
74
catch
(
Throwable
exc
)
75
{
76
lcd.setText
(
""
)
;
77
}
78
}
FIGURE 6-48
Search WWH ::
Custom Search