Login.java
/*
* 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 Dr;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
/**
*
* @author C065
*/
public class Login extends javax.swing.JFrame {
private javax.swing.JButton jb1;
private javax.swing.JLabel jl1;
private javax.swing.JLabel jl2;
private javax.swing.JLabel JL;
private javax.swing.JTextField dt1;
private javax.swing.JPasswordField dt2;
JFrame f = null;
public Login() {
initComponents();
File file = new File("C://Login_user.txt");
try{
if(!file.exists())
file.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
f = new JFrame("学生基本信息管理系统");
JL = new javax.swing.JLabel();
jb1 = new javax.swing.JButton();
dt1 = new javax.swing.JTextField();
dt2 = new javax.swing.JPasswordField();
jl1 = new javax.swing.JLabel();
jl2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JL.setFont(new java.awt.Font("宋体", 1, 18)); // NOI18N
JL.setText("欢迎进入学生基本信息系统");
jb1.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
jb1.setText("登录");
jb1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jb1actionPerformed(evt);
}
});
jl2.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
jl2.setText("密码:");
jl1.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
jl1.setText("用户名:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(JL)
.addContainerGap(94, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jl1, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
.addComponent(jl2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jb1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(dt2)
.addComponent(dt1, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)))
.addGap(74, 74, 74))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(JL, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jl1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jl2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(69, 69, 69)
.addComponent(jb1)
.addContainerGap())
);
pack();
}//
public void jb1actionPerformed(java.awt.event.ActionEvent evt) {
// TODO Auto-generated method stub
String name;
String psw;
String line;
boolean pass = false;
try{
//具有缓冲技术的Reader
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C://Login_user.txt.txt")));
while((line = br.readLine()) != null){
String[] str = line.split("#");
name = str[0];
psw = str[1];
System.out.println(dt1.getText());
System.out.println(dt2.getPassword());
boolean flag = true;//true表示匹配,false表示不匹配
char[] tmp = dt2.getPassword();
char[] cmp = psw.toCharArray();
//tmp是输入的密码,cmp是文件里的密码
//长度不等
if (tmp.length != cmp.length) {
flag = false;
} else {
//长度相等
for (int i = 0; i < tmp.length; i++) {
if (tmp[i] != cmp[i]) {
flag = false;
break;
}
}
}
if(dt1.getText().equals(name) && flag){
pass = true;
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
if(pass){
new Test();
//dispose 关闭当前窗口但是不退出程序
f.dispose();
} else {
//对话框
JOptionPane.showMessageDialog(null,"密码或账号错误","提示!",JOptionPane.YES_NO_OPTION);
}
//清空两个输入框
dt1.setText("");
dt2.setText("");
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史