本帖最后由 hdsjsql 于 2014-2-23 17:50 编辑
简单实现了下,只申请了网易和搜狐的账号,分别两个- import java.awt.Button;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- public class LoginTest{
- private Frame f;
- private Label lab1,lab2;
- private TextField t1,t2;
- private Button but1,but2;
- private TextArea ta;
-
- private String[] info;
- Map<String,Map<String,String>> map;
-
-
- LoginTest(){
- setInfo();
- init();
- f.setVisible(true);
- }
-
- //保存账号信息
- public void setInfo(){
- map=
- new TreeMap<String,Map<String,String>>();
- Map<String,String> blog1 = new TreeMap<String,String>();
- Map<String,String> blog2 = new TreeMap<String,String>();
-
-
- blog1.put("login2-test@sohu.com","123654");
- blog1.put("login3-test@sohu.com","123654");
- blog2.put("login1_test@163.com","a123654");
- blog2.put("login2_test@163.com","a123654");
- map.put("http://blog.sohu.com",blog1);
- map.put("http://blog.163.com",blog2);
-
- }
- //获取账号信息,如果账号正确,则将网址,账号,密码存入数组中
- public void getInfo(String str){
- info = new String[3];
- Set<String> keySet = map.keySet();
- Iterator<String> it = keySet.iterator();
- while(it.hasNext())
- {
-
- String addr = it.next();
- Map<String,String> blog = map.get(addr);
- Set<String> s = blog.keySet();
- Iterator<String> ite = s.iterator();
- while(ite.hasNext()){
- String Account = ite.next();
- String Password = blog.get(Account);
- if(str.equals(Account)){
- info[0] = addr;
- info[1] = Account;
- info[2] = Password;
- }
-
- }
- }
- }
-
-
-
- public void init(){
- f = new Frame("登陆系统");
- f.setLayout(new FlowLayout());
- f.setBounds(300,200,600,650);
- lab1=new Label("账号:");
- lab1.setBounds(40, 100, 50, 20);
- f.add(lab1);
- t1=new TextField(15);
- t1.setBounds(100, 100, 00, 20);
- f.add(t1);
-
- lab2=new Label("密 码 :");
- lab2.setBounds(40, 150, 50, 20);
- f.add(lab2);
- t2=new TextField(15);
- t2.setBounds(100, 150, 100, 20);
- //使在密码框中输入的信息显示为"*"号
- t2.setEchoChar('*');
- f.add(t2);
-
- but1 = new Button("确定");
- but1.setBounds(50,200,50,20);
- f.add(but1);
-
-
- ta = new TextArea(25,70);
- f.add(ta);
-
- Event();
-
- }
- public void Event(){
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
-
- but1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- test();
- }
-
- });
- }
-
- public void test(){
- //ta先清空,以免保存之前内容
- ta.setText("");
- String Account = t1.getText();
- String Password = t2.getText();
-
- getInfo(Account);
- for(String s:info){
- System.out.println(s);
- }
- // 账号错误 ,info不会被初始化
- if(info[0]==null)
- ta.append("账号错误");
- else if(Password.equals(info[2])){
- ta.append("账号正确,密码正确\r\n");
- ta.append("登陆网站:"+info[0]+"\r\n"+"账号:"
- +info[1]+"\r\n密码:"+info[2]);
- }
- else{
- ta.append("密码不正确\r\n");
- ta.append("登陆网站:"+info[0]+"\r\n"+"账号:"
- +info[1]+"\r\n密码:"+info[2]+"\r\n");
- }
-
- info=null;
- }
-
- public static void main(String[] args) {
- new LoginTest();
- }
- }
复制代码
|