Java Reference
In-Depth Information
52
jbtAddPswd.addActionListener
(
this
)
;
53
jbtChangePswd.addActionListener
(
this
)
;
54
jbtUsePswd.addActionListener
(
this
)
;
55
56
JPanel pswdPanel=
new
JPanel
(
new
BorderLayout
(
10,10
))
;
57
pswdPanel.add
(
label1,
BorderLayout
.WEST
)
;
58
pswdPanel.add
(
pswdField,
BorderLayout
.EAST
)
;
59
60
JPanel newPswdPanel=
new
JPanel
(
new
BorderLayout
(
19,19
))
;
61
newPswdPanel.add
(
label2,
BorderLayout
.WEST
)
;
62
newPswdPanel.add
(
newPswdField,
BorderLayout
.EAST
)
;
63
64
JPanel buttonPanel=
new
JPanel
(
new
FlowLayout
())
;
65
buttonPanel.add
(
jbtAddPswd
)
;
66
buttonPanel.add
(
jbtChangePswd
)
;
67
buttonPanel.add
(
jbtUsePswd
)
;
68
69
JPanel contentPanel=
new
JPanel
(
new
FlowLayout
())
;
70
contentPanel.add
(
pswdPanel
)
;
71
contentPanel.add
(
newPswdPanel
)
;
72
contentPanel.add
(
buttonPanel
)
;
73
setContentPane
(
contentPanel
)
;
74
75
// Enable Enter key for each JButton so user can tab to button
76
// and press the Enter key, rather than click button with mouse
77
InputMap map;
78
map = jbtAddPswd.getInputMap
()
;
79
if
(
map !=
null
){
80
map.put
(
KeyStroke.getKeyStroke
(
KeyEvent
.VK_ENTER,0,
false
)
,
"pressed"
)
;
81
map.put
(
KeyStroke.getKeyStroke
(
KeyEvent
.VK_ENTER,0,
true
)
,
"released"
)
;
82
}
83
map = jbtChangePswd.getInputMap
()
;
84
if
(
map !=
null
){
85
map.put
(
KeyStroke.getKeyStroke
(
KeyEvent
.VK_ENTER,0,
false
)
,
"pressed"
)
;
86
map.put
(
KeyStroke.getKeyStroke
(
KeyEvent
.VK_ENTER,0,
true
)
,
"released"
)
;
87
}
88
map = jbtUsePswd.getInputMap
()
;
89
if
(
map !=
null
){
90
map.put
(
KeyStroke.getKeyStroke
(
KeyEvent
.VK_ENTER,0,
false
)
,
"pressed"
)
;
91
map.put
(
KeyStroke.getKeyStroke
(
KeyEvent
.VK_ENTER,0,
true
)
,
"released"
)
;
92
}
93
}
94
95
public void
centerOnScreen
(
int
width,
int
height
)
96
{
97
int
top, left, x, y;
98
99
// Get the screen dimension
100
Dimension
screenSize =
Toolkit
.getDefaultToolkit
()
.getScreenSize
()
;
101
102
// Determine the location for the top left corner of the frame
103
x =
(
screenSize.width - width
)
/2;
104
y =
(
screenSize.height - height
)
/2;
105
top =
(
x < 0
)
? 0 : x;
106
left =
(
y < 0
)
? 0 : y;
107
108
// Set the frame to the specified location
109
this
.setLocation
(
top, left
)
;
110
}
111
112
public void
actionPerformed
(
ActionEvent
e
)
113
{
114
String
msg;
115
String
title;
116
int
optionType;
117
try
118
{
119
if
(
e.getSource
()
== jbtUsePswd
)
// user clicked Use password
120
{
121
pswd =
new
String
(
pswdField.getPassword
())
;
// get current pswd entered
122
password.validate
(
pswd
)
;
123
FIGURE 9-43
(continued)
Search WWH ::
Custom Search