package view;
import dao.StudentDao;
import dao.UserDao;
import entity.Student;
import entity.User;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import util.ExcelExporter;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import static view.Login.admin;
/**
* 主界面
*/
public class Main {
//创建dao层对象
StudentDao studentdao = new StudentDao();
UserDao userDao = new UserDao();
//用来存数据库数据(这样有bug,弃用)
// ArrayList<Student> students = studentdao.queryAll();
//临时数据
ArrayList studentlist = new ArrayList();
//给子窗口传参
JFrame frame = new JFrame("学生成绩管理系统");
JFrame frame1;
JFrame frame2;
public static Student stu = null; // 临时学生对象(Student)
public static User u = null; // 临时用户对象(User)
//表格
public static DefaultTableModel dtm;
public JTable table1;
public JScrollPane scrollPane;
//获取表格的id,行
static String id = null;
static int row = -1;
//用来保存返回值,给窗口调用
static double average1 = 0;
static double average2 = 0;
static double average3 = 0;
static double rate1;
static double rate2;
static double rate3;
static double max1;
static double max2;
static double max3;
static double min1;
static double min2;
static double min3;
/**
* 定义部件
*/
//查询栏
JButton jbb = new JButton("用户管理");
JButton jb = new JButton("查询所有");
JButton jb1 = new JButton("查询");
JButton jb2 = new JButton("查询");
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JLabel label1 = new JLabel("姓名查询");
JLabel label2 = new JLabel("学号查询");
JMenuItem m1 = new JMenuItem("修改密码");
JMenuItem m2 = new JMenuItem("退出登录");
//右侧按钮
JButton jb3 = new JButton("新增学生");
JButton jb4 = new JButton("修改");
JButton jb5 = new JButton("删除");
JButton jb6 = new JButton("排序");
JButton jb7 = new JButton("平均值");
JButton jb8 = new JButton("不及格");
JButton jb9 = new JButton("最值");
JButton jb10 = new JButton("优秀率");
JButton jb11 = new JButton("下载表格");
//下侧文本域
JTextArea area = new JTextArea(3, 80);
/***
* 防火墙
* 检测用户是否登录
* 浅做一下样子
*/
void MainTest() {
if (admin == null) {
JOptionPane.showMessageDialog(null, "还未登录!请先登录", "提示", JOptionPane.INFORMATION_MESSAGE);
frame.dispose();
new Login();
} else {
new Main();
}
}
/**
* 构造方法
*/
public Main() {
//文本框不可编辑
area.setEditable(false);
dtm = new DefaultTableModel() {
// 设置单元格不可编辑
public boolean isCellEditable(int rowIndex, int ColIndex) {
return false;
}
};
//表头
String[] head = {"学号", "姓名", "性别", "年龄", "java", "高数", "英语", "总分"};
for (int i = 0; i < head.length; i++) {
dtm.addColumn(head[i]);
}
//初始调用数据
fillTable();
table1 = new JTable(dtm);
scrollPane = new JScrollPane(table1);
/**
* 事件监控
*/
{
//获取Table属性
table1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
row = table1.getSelectedRow();
id = (String) table1.getValueAt(row, 0);
/* System.out.println("row:" + row);
System.out.println("id:" + id);*/
}
});
//右上角修改密码
m1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new UpdatePwd(frame);
if (u != null) {
u.setUsername(admin);//将当期登录账号封装到该对象
if (UpdatePwd.pwd.equals(new UserDao().queryByName(admin).getPassword())) {
int i = userDao.update(u);
if (i > 0) {
area.setText("修改成功!您的新密码为:" + u.getPassword());
}
} else {
area.setText("原密码错误!请检查后重新修改。");
}
}
}
});
//右上角退出登录
m2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
new Login();
}
});
//用户管理界面
jbb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new UserMain();
frame.dispose();
}
});
//查询所有
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearTable();
fillTable();
}
});
//姓名查询
jb1.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!field1.getText().isEmpty()) {
fillTableByName();
}
}
});
//学号查询
jb2.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!field2.getText().isEmpty()) {
fillTableById();
}
}
});
//添加学生信息
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AddDialog(frame);
if (stu != null) {
studentdao.save(stu);
// students.add(stu);
clearTable();
fillTable();
stu = null;
}
}
});
//修改学生信息
jb4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (row != -1) {
new UpdateDialog(frame);
if (stu != null) {
studentdao.update(stu);
// updateTable();
clearTable();
fillTable();
stu = null;