📄 login.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Login extends JFrame implements ActionListener{
JLabel j1,j2;
JPasswordField jf_passwd;
JTextField jf_user;
JButton jb_ok,jb_exit;
int count=0;
boolean isLaw=false;
public Login(){
j1=new JLabel("user:");
j2=new JLabel("password:");
jf_user=new JTextField();
jf_passwd=new JPasswordField('*');
jb_ok=new JButton("OK");
jb_exit=new JButton("EXIT");
jb_ok.addActionListener(this);
jb_exit.addActionListener(this);
Container c=this.getContentPane();
c.setLayout(new GridLayout(3,2));
c.add(j1);c.add(jf_user);
c.add(j2);c.add(jf_passwd);
c.add(jb_ok);c.add(jb_exit);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("please login first!");
setSize(300,100);
//pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("OK")){
try{
String user=jf_user.getText();
String passwd=MD5.md5(jf_passwd.getText());
File file=new File("code.caa");
BufferedReader bf=new BufferedReader(new FileReader(file));
String line;
isLaw=false;
while((line=bf.readLine())!=null){
int pos=line.indexOf(":");
String sub=line.substring(0, pos);
//System.out.println(sub);
String sub2=line.substring(pos+1, line.length());
//System.out.println(sub2);
if(sub.equalsIgnoreCase(user)){
if(sub2.equalsIgnoreCase(passwd)){
isLaw=true;
break;
}
}
}
if(isLaw){
JOptionPane.showMessageDialog(this, "成功登陆!");
new DBControler();
this.dispose();
}
else{
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
count++;
if(count>=3){
JOptionPane.showMessageDialog(this, "三次尝试错误,系统将退出!");
System.exit(0);
}
}
//JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}catch(IOException e1){
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
else {
if(e.getActionCommand().equals("EXIT"))
System.exit(0);
}
}
public static void main(String[] args) {
new Login();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -