From 149e38046a7fb6e863a52f73bebe3811a5628d8e Mon Sep 17 00:00:00 2001 From: cccccyb <995134776@qq.com> Date: Fri, 10 Jun 2022 14:07:17 +0800 Subject: [PATCH] TeacherUI reModify --- .../cfive/classroom/teacher/Attendance.java | 8 +- .../classroom/teacher/ChangePassword.form | 2 +- .../classroom/teacher/ChangePassword.java | 37 +++--- .../com/cfive/classroom/teacher/CheckIn.java | 13 ++- .../cfive/classroom/teacher/ClassList.form | 2 +- .../cfive/classroom/teacher/ClassList.java | 63 ++++++++-- .../com/cfive/classroom/teacher/Main.form | 6 +- .../com/cfive/classroom/teacher/Main.java | 110 ++++++++++++++---- .../cfive/classroom/teacher/SendMessage.form | 55 +++++---- .../cfive/classroom/teacher/SendMessage.java | 15 ++- .../com/cfive/classroom/teacher/SignIn.form | 4 +- .../com/cfive/classroom/teacher/SignIn.java | 52 +++++++-- 12 files changed, 263 insertions(+), 104 deletions(-) diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/Attendance.java b/Teacher/src/main/java/com/cfive/classroom/teacher/Attendance.java index 78c83c9..bed1af1 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/Attendance.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/Attendance.java @@ -31,7 +31,7 @@ public class Attendance { @Override public void valueChanged(ListSelectionEvent e) { int row = table_undo.getSelectedRow(); - // int col = table_undo.getSelectedColumn(); + // int col = table_undo.getSelectedColumn(); String newString=table_undo.getValueAt(row,2).toString(); attendance.test.setText(newString); LOGGER.info(newString); @@ -45,7 +45,8 @@ public class Attendance { public static void main(String[] args) { frame.setContentPane(attendance.rootPanel); - frame.setBounds(600,400,600,400); + frame.setSize(600,400); + frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(false); @@ -53,7 +54,8 @@ public class Attendance { } public void start(){ frame.setContentPane(attendance.rootPanel); - frame.setBounds(600,400,600,400); + frame.setSize(600,400); + frame.setLocationRelativeTo(null); frame.setResizable(false); DefaultTableModel alreadyTableModel=new DefaultTableModel(data,t1_columnTitle); alreadyTableModel.setColumnCount(3); diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.form b/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.form index bfdd241..c43be88 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.form +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.form @@ -106,7 +106,7 @@ - + diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.java b/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.java index f750518..6ef0f8e 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/ChangePassword.java @@ -3,12 +3,14 @@ package com.cfive.classroom.teacher; import javax.swing.*; public class ChangePassword { + private static final ChangePassword changePassword=new ChangePassword(); private JPanel rootPanel; - private JTextField workNo; + private JTextField workNo_input; private JButton cancel; private JButton confirm; private JPasswordField passwordField1; private JPasswordField passwordField2; + private String workNo,password1,password2; static JFrame frame = new JFrame("ChangePassword"); @@ -26,31 +28,38 @@ public class ChangePassword { public static void main(String[] args) { - frame.setContentPane(new ChangePassword().rootPanel); + frame.setContentPane(changePassword.rootPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600,400); frame.setVisible(false); } - public void start() { - frame.setContentPane(new ChangePassword().rootPanel); + public void start(String workNo) { + frame.setContentPane(changePassword.rootPanel); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.setResizable(false); -// frame.pack(); + changePassword.workNo=workNo; + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } private boolean check() { - String password1,password2; - password1 = String.valueOf(passwordField1.getPassword()); - password2 = String.valueOf(passwordField2.getPassword()); - if (password1.length()==0 || password2.length()==0) { - JOptionPane.showMessageDialog(null,"输入的密码为空"); + password1 = String.valueOf(changePassword.passwordField1.getPassword()); + password2 = String.valueOf(changePassword.passwordField2.getPassword()); + if(String.valueOf(changePassword.workNo_input.getText()).equals(workNo)) + { + if (password1.length()==0 || password2.length()==0) { + JOptionPane.showMessageDialog(null,"输入的密码为空"); + return false; + } else if (!password1.equals(password2)) { + JOptionPane.showMessageDialog(null, "两次输入密码不同"); + return false; + } else return true; + }else { + JOptionPane.showMessageDialog(null, "请输入正确的工号"); return false; - } else if (password1.equals(password2) == false) { - JOptionPane.showMessageDialog(null, "两次输入密码不同"); - return false; - } else return true; + } + } } diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java b/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java index 214aa90..f8eb064 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/CheckIn.java @@ -33,10 +33,10 @@ public class CheckIn { textField1.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { - n1=textField1.getText(); - if(n1.length()>=1){ - e.consume(); - } + n1=textField1.getText(); + if(n1.length()>=1){ + e.consume(); + } } }); textField2.addKeyListener(new KeyAdapter() { @@ -83,9 +83,10 @@ public class CheckIn { } public void start(){ frame.setContentPane(checkIn.rootPanel); - frame.setBounds(600,400,600,400); + frame.setSize(600,400); + frame.setLocationRelativeTo(null); frame.setResizable(false); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } } diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.form b/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.form index a7abb91..249e911 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.form +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.form @@ -32,7 +32,7 @@ - + diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.java b/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.java index 9166aa8..18e8c7d 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/ClassList.java @@ -1,9 +1,23 @@ package com.cfive.classroom.teacher; +import com.cfive.classroom.library.database.DatabaseHelper; +import com.cfive.classroom.library.database.bean.Course; +import com.cfive.classroom.library.database.util.DependenciesNotFoundException; +import com.cfive.classroom.library.database.util.NoConfigException; +import com.cfive.classroom.library.net.TeacherNet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.ItemListener; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.sql.SQLException; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; public class ClassList { private static final ClassList classlist=new ClassList(); @@ -12,13 +26,31 @@ public class ClassList { private JButton Button1; private JComboBox comboBox; private JPanel selectPanel; - private String workerNo,className; + private String workerNo, subName; + private List courseList; + private static final Logger LOGGER = LogManager.getLogger(); + private TeacherNet teacherNet; public ClassList() { Button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - className=(String) classlist.comboBox.getSelectedItem(); - Main.start(classlist.workerNo,classlist.className); + subName =(String) classlist.comboBox.getSelectedItem(); + //从配置文件中读取端口号并传参到主界面 + Properties properties = new Properties(); + try { + properties.load(new BufferedReader(new FileReader("Teacher/src/main/connect.properties"))); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + LOGGER.info(properties.getProperty("port")); + try { + teacherNet = new TeacherNet(Integer.valueOf(properties.getProperty("port"))); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + if(subName!=null){ + Main.start(classlist.workerNo,classlist.subName,teacherNet); + } frame.setVisible(false); } }); @@ -37,11 +69,24 @@ public class ClassList { frame.setLocationRelativeTo(null); frame.setResizable(false); classlist.workerNo=workerNo; - classlist.comboBox.addItem("20软工3班"); - classlist.comboBox.addItem("20软工4班"); - classlist.comboBox.addItem("20软工5班"); - classlist.comboBox.addItem("20软工6班"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //添加下拉列表的内容:该教师所教的科目名 + try { + courseList=DatabaseHelper.queryCourses(Long.valueOf(classlist.workerNo)); + if(courseList!=null){ + Iterator iterator = courseList.iterator(); //使用迭代器进行遍历 + while(iterator.hasNext()){ + String subName = iterator.next().getSubject().getSubName(); + classlist.comboBox.addItem(subName); + } + } + } catch (NoConfigException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (DependenciesNotFoundException e) { + throw new RuntimeException(e); + } + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/Main.form b/Teacher/src/main/java/com/cfive/classroom/teacher/Main.form index 1610843..7eed0d3 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/Main.form +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/Main.form @@ -24,7 +24,7 @@ - + @@ -41,7 +41,7 @@ - + @@ -115,7 +115,7 @@ - + diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java b/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java index 170156d..f695c62 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/Main.java @@ -1,9 +1,22 @@ package com.cfive.classroom.teacher; +import com.cfive.classroom.library.database.DatabaseHelper; +import com.cfive.classroom.library.database.bean.Student; +import com.cfive.classroom.library.database.util.DependenciesNotFoundException; +import com.cfive.classroom.library.database.util.NoConfigException; +import com.cfive.classroom.library.net.TeacherNet; +import com.cfive.classroom.library.net.util.MessageObject; +import com.cfive.classroom.library.net.util.ReceiveListener; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.sql.SQLException; import java.util.Arrays; +import java.util.Iterator; +import java.util.List; public class Main { private static final Main main=new Main(); @@ -15,17 +28,24 @@ public class Main { private JButton bt_sendMessage; private JButton changePasswordButton; - private JTextField workNo; - private JTextField className; + private JTextField workNo_show; + private JTextField subName_show; + private String workNo,subName; + private List studentList; + private String[] student; + private static final Logger LOGGER = LogManager.getLogger(); + private TeacherNet teacherNet; public Main() { + //发送消息按钮的监听 bt_sendMessage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { SendMessage sendMessage=new SendMessage(); - sendMessage.start(); + sendMessage.start(teacherNet); } }); + //发布签到按钮的监听 bt_checkIn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -33,6 +53,7 @@ public class Main { checkIn.start(); } }); + ////考勤情况按钮的监听 bt_attendance.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -40,43 +61,78 @@ public class Main { attendance.start(); } }); + ////随机选人按钮的监听 bt_select.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int person; String count; - Object[] possibleValues = {"提问1个同学", "提问2个同学", "提问3个同学", "提问4个同学", "提问5个同学", "提问6个同学", "提问7个同学"}; + Object[] dropList = {"提问1个同学", "提问2个同学", "提问3个同学", "提问4个同学", "提问5个同学", "提问6个同学", "提问7个同学"}; Object selectedValue = JOptionPane.showInputDialog(null, "选择提问同学个数", "随机选人,持续工作中...", - JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]); + JOptionPane.INFORMATION_MESSAGE, null, dropList, dropList[0]); //下拉列表的内容:选择提问的人数 while (true) { count = ""; - String substring = selectedValue.toString().substring(2, 3); + String substring = selectedValue.toString().substring(2, 3); //获取所选择的人数转化成int类型 int i = Integer.parseInt(substring); - String[] arr = new String[i]; - for (int j = 0; j < i; ) { - //随机得到的数值 - person = (int) (Math.random() * 10); - //遍历数组对 - if (!Arrays.asList(arr).contains(String.valueOf(person))) { - arr[j] = String.valueOf(person); - j++; + String[] arr = new String[i]; //new一个该数长度的String数组 + + try { + studentList = DatabaseHelper.selectStudentsFromCourse(DatabaseHelper.selectFromSubject(subName).getSubID()); + if(studentList!=null){ + for (int j = 0; j < i; ) { + //随机得到的数值 + person = (int) (Math.random() * studentList.size()); + //将该数组转化为列表,并看该列表是否包含该随机数,没有则添加进去 + if (!Arrays.asList(arr).contains(String.valueOf(person))) { + arr[j] = String.valueOf(person); + j++; + } + } + Iterator iterator = studentList.iterator(); + while (iterator.hasNext()){ + student=new String[studentList.size()]; + for(int k=0;k< studentList.size();k++){ + student[k]=iterator.next().getStuName(); + } + + } + for (int s = 0; s < arr.length; s++) { //遍历该数组并把每一个随机数所对应的人放到count中 + person = Integer.parseInt(arr[s]); + count +=student[person]; + } + JOptionPane.showMessageDialog(null, "恭喜以下同学被选中:\n\t\n" + count); + //将选人结果群发出去 + teacherNet.sendAllMessage(new MessageObject(null,null,null,null,count,true)); } + break; + } catch (DependenciesNotFoundException ex) { + throw new RuntimeException(ex); + } catch (NoConfigException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); } - String[] arr2 = {"小白 ", "小红 ", "小黑 ", "小绿 ", "小宗 ", "小橙 ", "小黄 ", "小青 ", "小蓝 ", "小紫 ", }; - for (int s = 0; s < arr.length; s++) { - person = Integer.parseInt(arr[s]); - count += arr2[person]; - } - JOptionPane.showMessageDialog(null, "恭喜以下同学,获得了本次回答问题的机会\n\t\n" + count); - break; + } } }); + //修改密码按钮的监听 changePasswordButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ChangePassword changePassword=new ChangePassword(); - changePassword.start(); + changePassword.start(main.workNo); + } + }); + + //学生端举手监听 + teacherNet.setOnReceiveListener(new ReceiveListener() { + @Override + public void onReceive(MessageObject messageObject) { + if (messageObject.isState()) { + JOptionPane.showMessageDialog(null,messageObject.getStuName()+" 举手了","温馨提示!",JOptionPane.INFORMATION_MESSAGE); + messageObject.setState(false); + } } }); } @@ -88,13 +144,17 @@ public class Main { frame.setVisible(false); } - public static void start(String workerNo,String className){ + public static void start(String workerNo, String subName, TeacherNet teacherNet1){ frame.setContentPane(main.rootPanel); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.setResizable(false); - main.workNo.setText(workerNo); - main.className.setText(className); + main.workNo=workerNo; + main.subName=subName; + main.teacherNet = teacherNet1; + //开启主界面即读取端口号 + main.workNo_show.setText(workerNo); + main.subName_show.setText(subName); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.form b/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.form index d66a603..a1c747d 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.form +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.form @@ -16,35 +16,22 @@ - - - - - - - - - - - - - - - - - - - - + + + - + - + + + + + @@ -58,7 +45,7 @@ - + @@ -66,6 +53,28 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java b/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java index acf2a52..5fa7423 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/SendMessage.java @@ -1,5 +1,7 @@ package com.cfive.classroom.teacher; +import com.cfive.classroom.library.net.TeacherNet; + import javax.swing.*; public class SendMessage { @@ -7,9 +9,10 @@ public class SendMessage { private static JFrame frame = new JFrame("SendMessage"); private JPanel rootPanel; - private JTextPane textPane1; - private JTextArea textArea1; + private JTextArea messageInput; private JButton bt_send; + private JTextArea messageShow; + private TeacherNet teacherNet; public static void main(String[] args) { frame.setContentPane(sendMessage.rootPanel); @@ -18,11 +21,13 @@ public class SendMessage { frame.setVisible(false); } - public void start(){ + public void start(TeacherNet teacherNet1){ frame.setContentPane(sendMessage.rootPanel); - frame.setBounds(600,400,600,400); + frame.setSize(600,400); + frame.setLocationRelativeTo(null); frame.setResizable(false); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); + sendMessage.teacherNet = teacherNet1; } } diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.form b/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.form index 3100fc9..f56f0f6 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.form +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.form @@ -55,7 +55,7 @@ - + @@ -84,7 +84,7 @@ - + diff --git a/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java b/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java index 187f4a2..51fc7c7 100644 --- a/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java +++ b/Teacher/src/main/java/com/cfive/classroom/teacher/SignIn.java @@ -1,21 +1,30 @@ package com.cfive.classroom.teacher; -import com.formdev.flatlaf.FlatDarculaLaf; +import com.cfive.classroom.library.database.DatabaseHelper; +import com.cfive.classroom.library.database.bean.Course; +import com.cfive.classroom.library.database.util.DependenciesNotFoundException; +import com.cfive.classroom.library.database.util.NoConfigException; import com.formdev.flatlaf.FlatLightLaf; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import javax.swing.*; -import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.sql.SQLException; +import java.util.List; public class SignIn { private static final SignIn sigIn = new SignIn(); private static final JFrame frame = new JFrame("教师端");; private JPanel rootJPanel; private JButton login_Button; - private JTextField workerNo; - private JPasswordField password; - + private JTextField workerNo_input; + private JPasswordField password_input; + private String workerNo,password; + private static final Logger LOGGER = LogManager.getLogger(); public static void main(String[] args) { FlatLightLaf.setup(); @@ -26,14 +35,33 @@ public class SignIn { sigIn.login_Button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(!sigIn.workerNo.getText().isEmpty()&&!sigIn.password.getText().isEmpty()){ - ClassList classList = new ClassList(); - String test=sigIn.workerNo.getText().toString(); - classList.start(test); - frame.setVisible(false); - }else{ - + sigIn.workerNo=String.valueOf(sigIn.workerNo_input.getText()); + sigIn.password=String.valueOf(sigIn.password_input.getPassword()); + if(sigIn.workerNo.length()==0||sigIn.password.length()==0){ //判断用户名和密码是否为空 JOptionPane.showMessageDialog(null,"用户名和密码不能为空","提示!!",JOptionPane.ERROR_MESSAGE); + }else{ + try { + if(DatabaseHelper.checkPasswdInTeacher(Long.valueOf(sigIn.workerNo), sigIn.password)){ + ClassList classList = new ClassList(); + String test=sigIn.workerNo_input.getText().toString(); + classList.start(test); + frame.setVisible(false); + }else { + JOptionPane.showMessageDialog(null,"密码错误,请重新输入","错误!!",JOptionPane.ERROR_MESSAGE); + } + } catch (NoConfigException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } catch (DependenciesNotFoundException ex) { + throw new RuntimeException(ex); + } catch (NoSuchAlgorithmException ex) { + throw new RuntimeException(ex); + } catch (InvalidKeySpecException ex) { + throw new RuntimeException(ex); + } + + } }