Java Reference
In-Depth Information
JPassword fields
automatically
replace mask
characters with
asterisks
PasswordDemo
program
FIGURE 9-42
1
/**
2
* Class PasswordDemo used to test class Password
3
*
4
* @author Michael Mick
5
* @version 1.0
6
*/
7
import
javax.swing.*;
8
import
java.awt.*;
9
import
java.awt.event.*;
10
11
public class
PasswordDemo
12
{
13
public static void
main
(
String
[]
argv
)
14
{
15
int
width = 400;
16
int
height = 130;
17
final
demoFrame f =
new
demoFrame
(
"PasswordDemo"
)
;
18
19
f.pack
()
;
20
f.setDefaultCloseOperation
(
JFrame.EXIT_ON_CLOSE
)
;
21
f.setSize
(
width, height
)
;
22
f.centerOnScreen
(
width, height
)
;
23
f.setVisible
(
true
)
;
24
}
25
}
26
27
class
demoFrame
extends
JFrame
implements
ActionListener
28
{
29
Password password =
null
;
30
String
pswd, newPswd;
31
32
JPasswordField pswdField;
33
JPasswordField newPswdField;
34
JButton jbtAddPswd, jbtChangePswd, jbtUsePswd;
35
36
public
demoFrame
(
String
title
)
37
{
38
super
(
title
)
;
// call super (JFrame) constructor
39
40
JLabel label1 =
new
JLabel
(
"Current Password:"
)
;
41
pswdField =
new
JPasswordField
(
20
)
;
42
pswdField.setEchoChar
(
'*'
)
;
43
44
JLabel label2 =
new
JLabel
(
"New Password:"
, JLabel.RIGHT
)
;
45
newPswdField =
new
JPasswordField
(
20
)
;
46
newPswdField.setEchoChar
(
'*'
)
;
47
48
jbtAddPswd =
new
JButton
(
"Add password"
)
;
49
jbtChangePswd =
new
JButton
(
"Change password"
)
;
50
jbtUsePswd =
new
JButton
(
"Use password"
)
;
51
FIGURE 9-43
(continued)
Search WWH ::
Custom Search