不多说 登陆按钮代码
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
* @ClassName: LoginListener
* @Description: 登陆 监听
* @date: 2020-06-04
* class
* @Copyright: qq 1694284882
*/
class LoginListener implements ActionListener
{
JTextField userId;
JPasswordField passwd;
public void setUserId(JTextField userId) {
this.userId = userId;
}
public void setPasswd(JPasswordField passwd) {
this.passwd = passwd;
}
/* static Map<Integer,String> usegroup= new HashMap<Integer,String>( );
static { // 方法在静态块执行
usegroup.put(1694284882,"556665");
usegroup.put(1059112238,"556665");
Iterator<Integer> setid = usegroup.keySet().iterator(); // 账户集合
Iterator<String> setpass = usegroup.values().iterator(); // 密码集合
while (setid.hasNext())
{
System.out.println( setid.next() );
}
}
while (setid.hasNext())
{
System.out.println( setid.next() );
} */
@Override
public void actionPerformed(ActionEvent e)
{
new Thread(new Runnable()
{
@Override
public void run()
{ // map集合
Map<String,String> usegroup= new HashMap<String,String>( );
// 方法在静态块执行
usegroup.put("1694284882","556665");
usegroup.put("1059112238","556665");
Iterator<String> setid = usegroup.keySet().iterator(); // 账户集合
Iterator<String> setpass = usegroup.values().iterator(); // 密码集合
// 获取文本框内容 比较 返回结果
String userIdString = userId.getText().trim();//
String userPasswordString = String.valueOf(passwd.getPassword()).trim();
//isbool(setid,userIdString);
while (setid.hasNext())
{
boolean a = setid.next().equals(userIdString);
if(a == true)
{
while (setpass.hasNext())
{
boolean b = setpass.next().equals(userPasswordString);
if(b == true)
{
// 创建线程接入聊天端口
//new Thread(new ChatThread(userIdString)).start();
//new MainInterface(userIdString);
JFrame n1=new JFrame("成功");
n1.setBounds(550,350,100,150);
n1.setVisible(true);
}
else
{
JFrame n1=new JFrame("密码错误");
n1.setBounds(550,350,100,150);
n1.setVisible(true);
passwd.setForeground(Color.BLACK);
passwd.setText("");
}
}
}
else
{
JFrame n1=new JFrame("账号错误");
n1.setBounds(550,350,100,150);
n1.setVisible(true);
userId.setText("");
userId.setForeground(Color.BLACK);
}
}
}
}).start();
}
}
用到的是 Map集合存贮账号密码 唯一对应关系
代码不完善 希望大神们能给意见补充!!
瑟瑟发抖