Biblioteca Java - Rev 32
Subversion Repositories:
(root)/Courses and labs samples/ISP/Exemple_ISP_Cluj_2015/GuiDemo/src/guidemo/ButtonAndTextField.java @ 36
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package guidemo;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class ButtonAndTextField extends JFrame {
HashMap accounts = new HashMap();
JLabel user, pwd;
JTextField tUser;
JPasswordField tPwd;
JButton bLoghin;
ButtonAndTextField() {
accounts.put("user1", "pwd1");
accounts.put("user2", "pwd2");
accounts.put("user3", "pwd3");
setTitle("Test login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
setSize(200, 250);
setVisible(true);
}
public void init() {
this.setLayout(null);
int width = 80;
int height = 20;
user = new JLabel("User ");
user.setBounds(10, 50, width, height);
pwd = new JLabel("Pasword ");
pwd.setBounds(10, 100, width, height);
tUser = new JTextField();
tUser.setBounds(70, 50, width, height);
tPwd = new JPasswordField();
tPwd.setBounds(70, 100, width, height);
bLoghin = new JButton("Loghin");
bLoghin.setBounds(10, 150, width, height);
bLoghin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = tUser.getText();
String pwd = new String(tPwd.getPassword());
if (!accounts.containsKey(user)) {
System.out.println("User not found!");
} else {
String p = (String) accounts.get(user);
if (p.equals(pwd)) {
System.out.println("Success!");
} else {
System.out.println("Failed!");
}
}
}
});
add(user);
add(pwd);
add(tUser);
add(tPwd);
add(bLoghin);
}
public static void main(String[] args) {
new ButtonAndTextField();
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package guidemo;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class ButtonAndTextField extends JFrame {
HashMap accounts = new HashMap();
JLabel user, pwd;
JTextField tUser;
JPasswordField tPwd;
JButton bLoghin;
ButtonAndTextField() {
accounts.put("user1", "pwd1");
accounts.put("user2", "pwd2");
accounts.put("user3", "pwd3");
setTitle("Test login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
setSize(200, 250);
setVisible(true);
}
public void init() {
this.setLayout(null);
int width = 80;
int height = 20;
user = new JLabel("User ");
user.setBounds(10, 50, width, height);
pwd = new JLabel("Pasword ");
pwd.setBounds(10, 100, width, height);
tUser = new JTextField();
tUser.setBounds(70, 50, width, height);
tPwd = new JPasswordField();
tPwd.setBounds(70, 100, width, height);
bLoghin = new JButton("Loghin");
bLoghin.setBounds(10, 150, width, height);
bLoghin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = tUser.getText();
String pwd = new String(tPwd.getPassword());
if (!accounts.containsKey(user)) {
System.out.println("User not found!");
} else {
String p = (String) accounts.get(user);
if (p.equals(pwd)) {
System.out.println("Success!");
} else {
System.out.println("Failed!");
}
}
}
});
add(user);
add(pwd);
add(tUser);
add(tPwd);
add(bLoghin);
}
public static void main(String[] args) {
new ButtonAndTextField();
}
}