Biblioteca Java - Blame information for rev 32
Subversion Repositories:
(root)/Courses and labs samples/ISP/Exemple_ISP_Cluj_2015/GuiDemo/src/guidemo/ButtonAndTextField2.java
Rev | Author | Line No. | Line |
---|---|---|---|
32 | mihai | 1 | /* |
2 | * To change this license header, choose License Headers in Project Properties. | ||
3 | * To change this template file, choose Tools | Templates | ||
4 | * and open the template in the editor. | ||
5 | */ | ||
6 | package guidemo; | ||
7 | |||
8 | import java.awt.FlowLayout; | ||
9 | import java.awt.event.ActionEvent; | ||
10 | import java.awt.event.ActionListener; | ||
11 | |||
12 | import javax.swing.*; | ||
13 | import java.util.*; | ||
14 | |||
15 | public class ButtonAndTextField2 extends JFrame{ | ||
16 | |||
17 | HashMap accounts = new HashMap(); | ||
18 | |||
19 | JLabel user,pwd; | ||
20 | JTextField tUser,tPwd; | ||
21 | JTextArea tArea; | ||
22 | JButton bLoghin; | ||
23 | |||
24 | ButtonAndTextField2(){ | ||
25 | |||
26 | accounts.put("user1", "pwd1"); | ||
27 | accounts.put("user2", "pwd2"); | ||
28 | accounts.put("user3", "pwd3"); | ||
29 | |||
30 | setTitle("Test login"); | ||
31 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
32 | init(); | ||
33 | setSize(200,300); | ||
34 | setVisible(true); | ||
35 | } | ||
36 | |||
37 | public void init(){ | ||
38 | |||
39 | this.setLayout(null); | ||
40 | int width=80;int height = 20; | ||
41 | |||
42 | user = new JLabel("User "); | ||
43 | user.setBounds(10, 50, width, height); | ||
44 | |||
45 | pwd = new JLabel("Pasword "); | ||
46 | pwd.setBounds(10, 100,width, height); | ||
47 | |||
48 | tUser = new JTextField(); | ||
49 | tUser.setBounds(70,50,width, height); | ||
50 | |||
51 | tPwd = new JTextField(); | ||
52 | tPwd.setBounds(70,100,width, height); | ||
53 | |||
54 | bLoghin = new JButton("Loghin"); | ||
55 | bLoghin.setBounds(10,150,width, height); | ||
56 | |||
57 | bLoghin.addActionListener(new TratareButonLoghin()); | ||
58 | |||
59 | tArea = new JTextArea(); | ||
60 | tArea.setBounds(10,180,150,80); | ||
61 | |||
62 | add(user);add(pwd);add(tUser);add(tPwd);add(bLoghin); | ||
63 | add(tArea); | ||
64 | |||
65 | } | ||
66 | |||
67 | public static void main(String[] args) { | ||
68 | new ButtonAndTextField2(); | ||
69 | } | ||
70 | |||
71 | class TratareButonLoghin implements ActionListener{ | ||
72 | |||
73 | public void actionPerformed(ActionEvent e) { | ||
74 | |||
75 | String usr = ButtonAndTextField2.this.tUser.getText(); | ||
76 | String pwd = ButtonAndTextField2.this.tPwd.getText(); | ||
77 | |||
78 | if(ButtonAndTextField2.this.accounts.containsKey(usr)){ | ||
79 | String correctPwd = (String)ButtonAndTextField2.this.accounts.get(usr); | ||
80 | if(correctPwd.equals(pwd)){ | ||
81 | //user and password correct | ||
82 | ButtonAndTextField2.this.tArea.append("Valid loghin\n"); | ||
83 | } | ||
84 | else{ | ||
85 | //invalid password | ||
86 | ButtonAndTextField2.this.tArea.append("Invalid password\n"); | ||
87 | } | ||
88 | }else{ | ||
89 | //user not found | ||
90 | ButtonAndTextField2.this.tArea.append("User not found\n"); | ||
91 | } | ||
92 | |||
93 | } | ||
94 | } | ||
95 | } |