1+ import java .awt .FlowLayout ;
2+ import java .awt .event .ActionListener ;
3+ import java .awt .event .ActionEvent ;
4+ import javax .swing .JFrame ; //basic window
5+ import javax .swing .JTextField ;
6+ import javax .swing .JPasswordField ;
7+ import javax .swing .JOptionPane ;
8+ public class duna9 extends JFrame {
9+ private JTextField item1 ;
10+ private JTextField item2 ;
11+ private JTextField item3 ;
12+ private JPasswordField passwordField ;
13+ public duna9 (){
14+ super ("The title" );
15+ setLayout (new FlowLayout ());
16+ item1 = new JTextField (10 );
17+ add (item1 );
18+
19+ item2 = new JTextField ("Enter text here :" );
20+ add (item2 );
21+
22+ item3 = new JTextField ("uneditable" ,20 );
23+ item3 .setEditable (false );
24+ add (item3 );
25+
26+ passwordField = new JPasswordField ("mypass" );
27+ add (passwordField );
28+
29+ thehandler handler = new thehandler ();
30+ item1 .addActionListener (handler );
31+ item2 .addActionListener (handler );
32+ item3 .addActionListener (handler );
33+ passwordField .addActionListener (handler );
34+ }
35+ //methods is gonna be called whenever an event occurs
36+ private class thehandler implements ActionListener {
37+ public void actionPerformed (ActionEvent event ){
38+ String string = "" ;
39+ if (event .getSource ()==item1 )
40+ string =String .format ("field 1 : %s" ,event .getActionCommand ());
41+ else if (event .getSource ()==item2 )
42+ string =String .format ("field 2 : %s" ,event .getActionCommand ());
43+ else if (event .getSource ()==item3 )
44+ string =String .format ("field 3 : %s" ,event .getActionCommand ());
45+ else if (event .getSource ()==passwordField )
46+ string =String .format ("password field is : %s" ,event .getActionCommand ());
47+
48+ JOptionPane .showMessageDialog (null ,string );
49+ }
50+ }
51+
52+
53+ }
0 commit comments