📄 login.java
字号:
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;
public class Login {
public static void main(String[] args) {
Loginframe frame = new Loginframe();
frame.setTitle("java图书馆系统登陆");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private String name;
public String getName(){
return name;
}
}
class Loginframe extends JFrame{
public Loginframe(){
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage("pic//icon.png");
setIconImage(img);
setTitle("java图书馆系统");
setSize(400,200);
setLocation(300,200);
label1=new JLabel("请登陆图书馆系统");
//输入用户名
label2=new JLabel("用户名:");
tf1=new JTextField(20);
tf1.setMaximumSize(tf1.getPreferredSize());
box1=Box.createHorizontalBox();
box1.add(label2);
box1.add(Box.createHorizontalStrut(10));
box1.add(tf1);
//输入密码
label3=new JLabel("密码: ");
tf2=new JPasswordField(20);
tf2.setMaximumSize(tf2.getPreferredSize());
box2=Box.createHorizontalBox();
box2.add(label3);
box2.add(Box.createHorizontalStrut(10));
box2.add(tf2);
//按钮
bt1=new JButton("管理员登陆");
bt2=new JButton("学生登陆");
box3=Box.createHorizontalBox();
box3.add(bt1);
box3.add(Box.createHorizontalStrut(15));
box3.add(bt2);
//合成组件
tbox=Box.createVerticalBox();
tbox.add(label1);
tbox.add(box1);
tbox.add(box2);
tbox.add(box3);
//将组件放入内容中
add(tbox,BorderLayout.CENTER);
//按钮功能
bt1.addActionListener(new mok());
bt2.addActionListener(new sok());
}
class mok implements ActionListener{
public void actionPerformed(ActionEvent avt){
Denglu("login.txt");
if (tof==true){
setVisible(false);
ManagerView MV=new ManagerView();
MV.setTitle("管理员"+name+"进入图书馆管理系统");
MV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MV.setSize(850, 500);
MV.setLocation(200, 180);
MV.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null,"用户名或密码输入错误!");
}
}
}
public void Denglu(String Filename){
String tefi1=tf1.getText();
String tefi2=tf2.getText();
String password,r;
try{
if (tefi1.trim().length()==0)
{
JOptionPane.showMessageDialog(null,"请输入用户名!");
}
else if (tefi2.trim().length()==0)
{
JOptionPane.showMessageDialog(null,"请输入密码!");
}
else{
FileReader fr = new FileReader(Filename);
BufferedReader br = new BufferedReader(fr);
while ((r = br.readLine()) != null) {
StringTokenizer sto = new StringTokenizer(r);
name = sto.nextToken();
password = sto.nextToken();
if (tefi1.equals(name) && tefi2.equals(password)) {
tof = true;
break;
}
}// 验证登陆员号码及密码
}
}catch(Exception e){
JOptionPane.showMessageDialog(null,"请重新输入!");
}
try {
Writer fr = new FileWriter("deng.txt", false);// 使得记录时不会覆盖
fr.write(name+"\r\n");
fr.flush();
fr.close();
}catch (IOException ie) {
System.err.println("error:" + ie.getMessage());
}
}
class sok implements ActionListener{
public void actionPerformed(ActionEvent avt){
Denglu("stulogin.txt");
if (tof==true){
setVisible(false);
StudentView SV=new StudentView();
SV.setTitle("学生"+name+"进入图书馆管理系统");
SV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SV.setSize(850, 500);
SV.setLocation(200, 180);
SV.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null,"用户名或密码输入错误!");
}
}
}
private JLabel label1,label2,label3;
private JTextField tf1;
private JPasswordField tf2;
private JButton bt1,bt2;
private Box box1,box2,box3,tbox;
private boolean tof=false;
private String name;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -