package com.clic.Expetion.Work.practiCaltraining;
import com.clic.Expetion.Work.practiCaltraining.Account;
import com.clic.Expetion.Work.practiCaltraining.StudentInterface;
import com.clic.Expetion.Work.practiCaltraining.TeacherInterface;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
class myFrame {
JFrame jFrame=new JFrame("登录");
JLabel title=new JLabel("欢迎学生成绩管理系统");
JLabel account=new JLabel("用户名:");
JTextField number=new JTextField(15);
JLabel passWord=new JLabel("密 码:");
JPasswordField code=new JPasswordField(15);
JButton register =new JButton("登录");
JButton cancel=new JButton("取消");
JRadioButton student=new JRadioButton("学生",true);
JRadioButton teacher=new JRadioButton("教师");
ButtonGroup buttonGroup=new ButtonGroup();//组
JDialog jDialog;
void init(){//组装
int i=0;
title.setFont(new Font("黑体",Font.BOLD,35));//设置字体以及大小
account.setFont(new Font("楷体",Font.PLAIN,20));
passWord.setFont(new Font("楷体",Font.PLAIN,20));
account.setForeground(Color.RED);//设置标签字体颜色
passWord.setForeground(Color.RED);
JPanel jPanel1=new JPanel();
buttonGroup.add(student);//将选项添加到ButtonGroup,只能单选
buttonGroup.add(teacher);
JPanel jPanel2=new JPanel();//创建5个JPanel对象
JPanel jPanel3=new JPanel();
JPanel jPanel4=new JPanel();
JPanel jPanel5=new JPanel();
jPanel1.add(title);//向JPanel对象中添加组件
jPanel2.add(account);
jPanel2.add(number);
jPanel3.add(passWord);
jPanel3.add(code);
jPanel4.add(student);
jPanel4.add(teacher);
//创建账号密码
Account[] accounts=new Account[6];
accounts[0] =new Account("111","111","张三","学生",80);
accounts[1] =new Account("222","222","李四","学生",78);
accounts[2] =new Account("333","333","王五","学生",90);
accounts[3] =new Account("444","444","王二","学生",100);
accounts[4] =new Account("555","555","赵六","学生",60);
accounts[5] =new Account("666","666","小黑","教师");
File file=new File("D://桌面//data");
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
if (file.exists()){
try {
ois=new ObjectInputStream(new FileInputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
try {
while((accounts[i]= (Account) ois.readObject())!=null){
i++;
if(i==6){
break;
}
}
ois.close();
} catch (EOFException e) {
// e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(accounts[0]);
oos.writeObject(accounts[1]);
oos.writeObject(accounts[2]);
oos.writeObject(accounts[3]);
oos.writeObject(accounts[4]);
oos.writeObject(accounts[5]);
oos.writeObject(null);
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//为取消添加监听事件
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
number.setText("");
code.setText("");
}
});
//为登录添加监听事件
register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {//点击登陆按钮
int i=0;
if (number.getText().equals("")){//判断是否输入账号
JOptionPane.showMessageDialog(jFrame,"请输入账号","错误",JOptionPane.ERROR_MESSAGE);
}else if (code.getText().equals("")){//判断是否输入密码
JOptionPane.showMessageDialog(jFrame,"请输入密码","错误",JOptionPane.ERROR_MESSAGE);
}else if (!(student.isSelected()||teacher.isSelected())){
JOptionPane.showMessageDialog(jFrame,"请选择您的身份","失败",JOptionPane.QUESTION_MESSAGE);
}
for ( i = 0; i <6 ; i++) {
if (number.getText().equals(accounts[i].getAccount())&&code.getText().equals(accounts[i].getPassWord())&&accounts[i].getIdentity().equals(student.isSelected()?"学生":"教师")){
JOptionPane.showMessageDialog(jFrame,"登陆成功","成功",JOptionPane.PLAIN_MESSAGE);
if (teacher.isSelected()){
new TeacherInterface().init(accounts,accounts[i]);
jFrame.dispose();
}else {
new StudentInterface().init(accounts,accounts[i]);
jFrame.dispose();
}
break;
}
}
while (i==6){
JOptionPane.showMessageDialog(jFrame,"账号或密码错误","失败",JOptionPane.ERROR_MESSAGE);
break;
}
}
});
jPanel5.add(register);
jPanel5.add(cancel);
jFrame.setLayout(new GridLayout(5,1));//设置网格布局管理
jFrame.add(jPanel1);
jFrame.add(jPanel2);
jFrame.add(jPanel3);
jFrame.add(jPanel4);
jFrame.add(jPanel5);
jFrame.setSize(500,350);//设置窗口大小
jFrame.setLocationRelativeTo(null);//使窗体居中显示
jFrame.setResizable(false);//设置窗体大小不可变
jFrame.setVisible(true);
}
}
class mianText {
public static void main(String[] args) {
new myFrame().init();
}
}