package view;
import method.DBConnection;
import method.Export;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TeacherView {
public static JFrame jframe_1 = new JFrame("学生信息管理系统");
// 定义面板
public static JPanel jpanel_1 = new JPanel(new FlowLayout());//流式布局
public static JPanel jpanel_2 = new JPanel(null);//空布局
// 设置文本区用于显示信息
public static JTextArea j_1 = new JTextArea();
// 主界面
public void init_0() {
// 窗口大小
jframe_1.setSize(810, 400);
// 空布局
jframe_1.setLayout(null);
// 窗口不可调整
jframe_1.setResizable(false);
// 关闭窗口则退出程序
jframe_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 菜单栏
JMenuBar bar = new JMenuBar();
// 一级菜单
JMenu men_1 = new JMenu("更新成绩");
JMenu men_2 = new JMenu("按系别、专业、班级查询信息");
JMenuItem item_1 = new JMenuItem("录入成绩");
JMenuItem item_2 = new JMenuItem("修改成绩");
JMenuItem item_3 = new JMenuItem("删除成绩");
JMenuItem item_4 = new JMenuItem("查询学生成绩");
JMenuItem item_5 = new JMenuItem("查询不及格人数");
JMenuItem item_6 = new JMenuItem("查询最高分、最低分和平均分");
JMenuItem item_7 = new JMenuItem("导出报表");
Font font = new Font("黑体", Font.PLAIN, 15);
// 设置菜单字体
// 设置菜单字体
men_1.setFont(font);
men_2.setFont(font);
item_1.setFont(font);
item_2.setFont(font);
item_3.setFont(font);
item_4.setFont(font);
item_5.setFont(font);
item_6.setFont(font);
item_7.setFont(font);
// 加入
men_1.add(item_1);
men_1.add(item_2);
men_1.add(item_3);
men_2.add(item_4);
men_2.add(item_5);
men_2.add(item_6);
men_2.add(item_7);
bar.add(men_1);
bar.add(men_2);
jframe_1.setJMenuBar(bar);
jpanel_1.setBounds(0, 0, 200, 400);
jpanel_2.setBounds(210, 0, 600, 400);
jpanel_1.setBackground(Color.LIGHT_GRAY);
jpanel_2.setBackground(Color.LIGHT_GRAY);
jframe_1.add(jpanel_1);
jframe_1.add(jpanel_2);
//设置文本区不能编辑
j_1.setEditable(false);
//将j1作为可滚动面板sp的显示区域
JScrollPane sp = new JScrollPane(j_1);
sp.setSize(585, 340);
TeacherView.jpanel_2.add(sp);
// 窗口居中
jframe_1.setLocationRelativeTo(null);
// 窗口显示
jframe_1.setVisible(true);
item_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_1();
}
});
item_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_2();
}
});
item_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_3();
}
});
item_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_4();
}
});
item_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_5();
}
});
item_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_6();
}
});
item_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TeacherView.init_7();
}
});
}
public static void init_1() {
// 刷新面板
TeacherView.jpanel_1.removeAll();
// 刷新面板
TeacherView.jpanel_1.updateUI();
// 字体
JLabel jlabel0 = new JLabel("请输入要录入的信息\n");
TeacherView.jpanel_1.add(jlabel0);
Font font = new Font("黑体", Font.PLAIN, 15);
jlabel0.setFont(font);
TeacherView.jpanel_1.add(jlabel0);
String s[]=new String[4];
s[0]=" 学 号 :";
s[1]=" 课 程 号 :";
s[2]=" 成 绩 :";
s[3]=" 学 期 :";
JLabel jlabel[]=new JLabel[4];
// 文本框
JTextField jtextfield[] = new JTextField[4];
for(int i=0;i<4;i++) {
jlabel[i]=new JLabel(s[i]);
jlabel[i].setFont(font);
TeacherView.jpanel_1.add(jlabel[i]);
jtextfield[i]=new JTextField(12);
TeacherView.jpanel_1.add(jtextfield[i]);
}
JButton jbutton_1=new JButton("提交");
jbutton_1.setFont(font);
TeacherView.jpanel_1.add(jbutton_1);
jbutton_1.addActionListener(e -> {
String str[] = new String[4];
for(int i=0;i<4;i++) {
str[i] = String.valueOf(jtextfield[i].getText());
}
if(jtextfield[0]!=null||jtextfield[1]!=null) {
PreparedStatement ps;
ResultSet rs = null;
DBConnection dbc = new DBConnection();
String sno = String.valueOf(jtextfield[0].getText());
String course = String.valueOf(jtextfield[1].getText());
String sql = "select * from student where sno = ? ";
try {
ps = dbc.getConnection().prepareStatement(sql);
ps.setString(1, sno);
rs = ps.executeQuery();
if(rs.next()){
sql = "select * from course where cno=?";
PreparedStatement ps1 = dbc.getConnection().prepareStatement(sql);
ps1.setString(1, course);
ResultSet rs1 = ps1.executeQuery();
if(rs1.next()){
String grade = String.valueOf(jtextfield[2].getText());
String semester = String.valueOf(jtextfield[3].getText());
sql = "insert ignore into sc values(?,?,?,?);";
PreparedStatement ps2 = dbc.getConnection().prepareStatement(sql);
ps2.setString(1, sno);
ps2.setString(2, course);
ps2.setString(3, grade);
ps2.setString(4,semester);
int num=ps2.executeUpdate();
if(num==1){
JOptionPane.showMessageDialog(jbutton_1, "成绩录入成功!",
"提示",JOptionPane.PLAIN_MESSAGE);
}
else{
JOptionPane.showMessageDialog(jbutton_1, "该学生此课程成绩已经录入!",
"提示",JOptionPane.PLAIN_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(jbutton_1, "录入失败,查无此课程!",
"提示",JOptionPane.PLAIN_MESSAGE);
}