Biblioteca Java - Blame information for rev 32
Subversion Repositories:
(root)/Courses and labs samples/ISP/Exemple_ISP_Cluj_2015/GuiDemo/src/guidemo/ButtonAndTextField.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 ButtonAndTextField extends JFrame { | ||
16 | |||
17 | HashMap accounts = new HashMap(); | ||
18 | |||
19 | JLabel user, pwd; | ||
20 | JTextField tUser; | ||
21 | JPasswordField tPwd; | ||
22 | JButton bLoghin; | ||
23 | |||
24 | ButtonAndTextField() { | ||
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, 250); | ||
34 | |||
35 | setVisible(true); | ||
36 | } | ||
37 | |||
38 | public void init() { | ||
39 | |||
40 | this.setLayout(null); | ||
41 | int width = 80; | ||
42 | int height = 20; | ||
43 | |||
44 | user = new JLabel("User "); | ||
45 | user.setBounds(10, 50, width, height); | ||
46 | |||
47 | pwd = new JLabel("Pasword "); | ||
48 | pwd.setBounds(10, 100, width, height); | ||
49 | |||
50 | tUser = new JTextField(); | ||
51 | tUser.setBounds(70, 50, width, height); | ||
52 | |||
53 | tPwd = new JPasswordField(); | ||
54 | tPwd.setBounds(70, 100, width, height); | ||
55 | |||
56 | bLoghin = new JButton("Loghin"); | ||
57 | bLoghin.setBounds(10, 150, width, height); | ||
58 | |||
59 | bLoghin.addActionListener(new ActionListener() { | ||
60 | public void actionPerformed(ActionEvent e) { | ||
61 | String user = tUser.getText(); | ||
62 | String pwd = new String(tPwd.getPassword()); | ||
63 | if (!accounts.containsKey(user)) { | ||
64 | System.out.println("User not found!"); | ||
65 | } else { | ||
66 | String p = (String) accounts.get(user); | ||
67 | if (p.equals(pwd)) { | ||
68 | System.out.println("Success!"); | ||
69 | } else { | ||
70 | System.out.println("Failed!"); | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | }); | ||
75 | |||
76 | add(user); | ||
77 | add(pwd); | ||
78 | add(tUser); | ||
79 | add(tPwd); | ||
80 | add(bLoghin); | ||
81 | |||
82 | } | ||
83 | |||
84 | |||
85 | public static void main(String[] args) { | ||
86 | new ButtonAndTextField(); | ||
87 | } | ||
88 | |||
89 | } |