-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path13.7.java
82 lines (68 loc) · 2.24 KB
/
13.7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Fig. 12.7: TextFieldTest.java
// Demonstrating the JTextField class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JTextField text1, text2, text3;
private JPasswordField password;
public Main()
{
super( "Testing JTextField and JPasswordField" );
Container c = getContentPane();
c.setLayout( new FlowLayout() );
// construct textfield with default sizing
text1 = new JTextField( 10 );
c.add( text1 );
// construct textfield with default text
text2 = new JTextField( "Enter text here" );
c.add( text2 );
// construct textfield with default text and
// 20 visible elements and no event handler
text3 = new JTextField( "Uneditable text field", 20 );
text3.setEditable( false );
c.add( text3 );
// construct textfield with default text
password = new JPasswordField( "Hidden text" );
c.add( password );
TextFieldHandler handler = new TextFieldHandler();
text1.addActionListener( handler );
text2.addActionListener( handler );
text3.addActionListener( handler );
password.addActionListener( handler );
setSize( 325, 100 );
show();
}
public static void main( String args[] )
{
Main app = new Main();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
// inner class for event handling
private class TextFieldHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
String s = "";
if ( e.getSource() == text1 )
s = "text1: " + e.getActionCommand();
else if ( e.getSource() == text2 )
s = "text2: " + e.getActionCommand();
else if ( e.getSource() == text3 )
s = "text3: " + e.getActionCommand();
else if ( e.getSource() == password ) {
JPasswordField pwd =
(JPasswordField) e.getSource();
s = "password: " +
new String( pwd.getPassword() );
}
JOptionPane.showMessageDialog( null, s );
}
}
}