From 08da72e7358ab43921f15498952b0a167c603f34 Mon Sep 17 00:00:00 2001 From: GGB <1223416496@qq.com> Date: Fri, 10 Jun 2022 01:32:14 +0800 Subject: [PATCH] Init student --- .idea/dataSources.xml | 4 +- Student/src/main/connect.properties | 2 + .../com/cfive/classroom/student/Center.form | 32 ++-- .../com/cfive/classroom/student/Center.java | 138 ++++++++++++++---- .../classroom/student/ChangePassword.form | 15 +- .../classroom/student/ChangePassword.java | 100 +++++++++---- .../com/cfive/classroom/student/Chat.form | 47 +++--- .../com/cfive/classroom/student/Chat.java | 58 ++++++-- .../cfive/classroom/student/MainWindow.form | 6 +- .../cfive/classroom/student/MainWindow.java | 65 ++++++--- .../com/cfive/classroom/student/MyTest.java | 20 +++ mysql.properties | 7 + 12 files changed, 361 insertions(+), 133 deletions(-) create mode 100644 Student/src/main/connect.properties create mode 100644 Student/src/test/java/com/cfive/classroom/student/MyTest.java create mode 100644 mysql.properties diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index c186654..f90f67d 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,11 +1,11 @@ - + mysql.8 true com.mysql.cj.jdbc.Driver - jdbc:mysql://10.14.0.7:3306 + jdbc:mysql://10.14.0.7:3306/class $ProjectFileDir$ diff --git a/Student/src/main/connect.properties b/Student/src/main/connect.properties new file mode 100644 index 0000000..2ec1d7f --- /dev/null +++ b/Student/src/main/connect.properties @@ -0,0 +1,2 @@ +host=127.0.0.1 +port=8888 \ No newline at end of file diff --git a/Student/src/main/java/com/cfive/classroom/student/Center.form b/Student/src/main/java/com/cfive/classroom/student/Center.form index e9ec95d..bcabb38 100644 --- a/Student/src/main/java/com/cfive/classroom/student/Center.form +++ b/Student/src/main/java/com/cfive/classroom/student/Center.form @@ -1,6 +1,6 @@
- + @@ -16,7 +16,7 @@ - + @@ -41,17 +41,10 @@ + - - - - - - - - @@ -60,6 +53,16 @@ + + + + + + + + + + @@ -70,7 +73,7 @@ - + @@ -97,7 +100,7 @@ - + @@ -115,10 +118,11 @@ + - + @@ -126,7 +130,7 @@ - + diff --git a/Student/src/main/java/com/cfive/classroom/student/Center.java b/Student/src/main/java/com/cfive/classroom/student/Center.java index e7799cf..7f9e0bb 100644 --- a/Student/src/main/java/com/cfive/classroom/student/Center.java +++ b/Student/src/main/java/com/cfive/classroom/student/Center.java @@ -1,61 +1,147 @@ package com.cfive.classroom.student; +import com.cfive.classroom.library.database.DatabaseHelper; +import com.cfive.classroom.library.database.util.NoConfigException; +import com.cfive.classroom.library.net.StudentNet; +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.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.sql.SQLException; +import java.util.Properties; public class Center { - private JButton Button1; - private JButton button2; + private static final Logger LOGGER = LogManager.getLogger(); + private static final Center center = new Center(); + private JButton signInButton; + private JButton raiseHandButton; private JButton chatButton; - private JPanel rootpanel2; - private JTextField textField1; + private JPanel rootpanel; private JButton changePasswordButton; - private JTextArea textArea1; - private JButton 连接Button; - static JFrame frame = new JFrame("Center"); + private JTextArea textClass; + private JButton connectButton; + private JTextField stuNoText; + private static JFrame frame = new JFrame("Center"); + private StudentNet studentNet; + private String host; + private int port; + private String signInCode; + private String stuNo; + private String stuName; + private MessageObject messageObject; public Center() { + + //留言 chatButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Chat chat = new Chat(); - chat.start(); + chat.start(stuNo,stuName,studentNet); } }); + //修改密码 changePasswordButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ChangePassword changePassword = new ChangePassword(); - changePassword.start(); - frame.setVisible(false); + changePassword.start(stuNoText.getText()); } }); - Button1.addActionListener(new ActionListener() { + //连接 + connectButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - String code = JOptionPane.showInputDialog(null,"签到码:","签到",JOptionPane.PLAIN_MESSAGE); - if (code.equals("1234")) { - JOptionPane.showMessageDialog(null, "签到成功"); - } else { - JOptionPane.showMessageDialog(null,"签到失败"); + Properties properties = new Properties(); + try { + properties.load(new BufferedReader(new FileReader("Student/src/main/connect.properties"))); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + host = properties.getProperty("host"); + port = Integer.parseInt(properties.getProperty("port")); + try { + studentNet = new StudentNet(host,port); + JOptionPane.showMessageDialog(null, "连接成功"); + } catch (IOException ex) { + JOptionPane.showMessageDialog(null,"连接失败"); + LOGGER.error("IOException",ex); + } + } + }); + //签到 + signInButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + signInCode = JOptionPane.showInputDialog(null,"签到码:","签到",JOptionPane.PLAIN_MESSAGE); + studentNet.setOnReceiveListener(new ReceiveListener() { + @Override + public void onReceive(MessageObject messageObject) { + if (messageObject.getCode().equals(signInCode)) { + JOptionPane.showMessageDialog(null, "签到成功"); + } else { + JOptionPane.showMessageDialog(null,"签到失败"); + } + } + }); + } + }); + + //举手 + raiseHandButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + messageObject = new MessageObject(stuNo, stuName, null, null, null,true); + studentNet.sendMessageThread(messageObject); + JOptionPane.showMessageDialog(null,"你已经向老师举手"); + } + }); + //随机抽人 + studentNet.setOnReceiveListener(new ReceiveListener() { + @Override + public void onReceive(MessageObject messageObject) { + if (messageObject.isState()) { + JOptionPane.showMessageDialog(null,"恭喜以下同学被选中:\n\t\n"+messageObject.getCount()); } } }); } - public void start(){ - frame.setContentPane(new Center().rootpanel2); - frame.setBounds(650,300,600,400); + + public void start(String num){ + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setContentPane(center.rootpanel); + frame.setSize(600,400); + frame.setLocationRelativeTo(null); frame.setResizable(false); frame.setVisible(true); + center.stuNoText.setText(num); + stuNo = String.valueOf(stuNoText.getText()); + stuName = getName(); + LOGGER.info("学号"+stuNo); + LOGGER.info("姓名"+stuName); + } + //获取学生姓名 + public String getName(){ + String name = null; + try { + name=DatabaseHelper.selectFromStudent(Long.parseLong(stuNoText.getText())).getStuName(); + } catch (NoConfigException e) { + JOptionPane.showMessageDialog(null,"没有数据库配置文件","警告",JOptionPane.ERROR_MESSAGE); + LOGGER.error("No configuration", e); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null,"数据库出错","警告",JOptionPane.ERROR_MESSAGE); + LOGGER.error("SQLException",e); + } + return name; } - public static void main(String[] args) { - frame.setContentPane(new Center().rootpanel2); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(600,400); - frame.setResizable(false); - frame.setVisible(false); - } + } diff --git a/Student/src/main/java/com/cfive/classroom/student/ChangePassword.form b/Student/src/main/java/com/cfive/classroom/student/ChangePassword.form index 9eab78f..b977961 100644 --- a/Student/src/main/java/com/cfive/classroom/student/ChangePassword.form +++ b/Student/src/main/java/com/cfive/classroom/student/ChangePassword.form @@ -55,7 +55,7 @@ - + @@ -63,7 +63,7 @@ - + @@ -74,25 +74,28 @@ - + + - + + - + + @@ -106,7 +109,7 @@ - + diff --git a/Student/src/main/java/com/cfive/classroom/student/ChangePassword.java b/Student/src/main/java/com/cfive/classroom/student/ChangePassword.java index c63a459..b80e802 100644 --- a/Student/src/main/java/com/cfive/classroom/student/ChangePassword.java +++ b/Student/src/main/java/com/cfive/classroom/student/ChangePassword.java @@ -1,58 +1,96 @@ package com.cfive.classroom.student; +import com.cfive.classroom.library.database.DatabaseHelper; +import com.cfive.classroom.library.database.util.DependenciesNotFoundException; +import com.cfive.classroom.library.database.util.NoConfigException; +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.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.sql.SQLException; public class ChangePassword { + private static final Logger LOGGER = LogManager.getLogger(); + private static final ChangePassword changePassword = new ChangePassword(); + private JPanel rootPanel; - private JTextField textField1; + private JTextField textNum; private JButton cancel; private JButton confirm; private JPasswordField passwordField1; private JPasswordField passwordField2; + private static JFrame frame = new JFrame("修改密码"); - static JFrame frame = new JFrame("ChangePassword"); + private String stuNo; public ChangePassword() { confirm.addActionListener(e -> { if(check()){ - Center.frame.setVisible(true); - frame.setVisible(false); + try { + DatabaseHelper.changePasswdInStudent(Long.parseLong(stuNo), passwordField1.getPassword().toString()); + } catch (NoConfigException ex) { + JOptionPane.showMessageDialog(null,"没有数据库配置文件","警告",JOptionPane.ERROR_MESSAGE); + LOGGER.error("No configuration", e); + } catch (SQLException ex) { + JOptionPane.showMessageDialog(null,"数据库出错","警告",JOptionPane.ERROR_MESSAGE); + LOGGER.error("SQLException",e); + } catch (DependenciesNotFoundException ex) { + LOGGER.error("DependenciesNotFoundException",e); + } catch (NoSuchAlgorithmException ex) { + LOGGER.error("NoSuchAlgorithmException",e); + } catch (InvalidKeySpecException ex) { + LOGGER.error("InvalidKeySpecException",e); + } + frame.dispose(); } }); cancel.addActionListener(e -> { - Center.frame.setVisible(true); - frame.setVisible(false); + frame.dispose(); }); } - public static void main(String[] args) { - frame.setContentPane(new ChangePassword().rootPanel); + + private boolean check() { + String password1,password2,num; + num = String.valueOf(textNum.getText()); + + LOGGER.info("传入学号"+stuNo); + LOGGER.info(num); + + password1 = String.valueOf(passwordField1.getPassword()); + password2 = String.valueOf(passwordField2.getPassword()); + if(num.equals(stuNo)){ + 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; + } + } + + + public void start(String stuNo) { + frame.setContentPane(changePassword.rootPanel); + frame.setSize(600,400); + frame.setLocationRelativeTo(null); + frame.setResizable(false); + frame.setVisible(true); + changePassword.stuNo = stuNo; + LOGGER.info(changePassword.stuNo); + } + public static void main(String[] args) { + 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); - frame.setBounds(650,300,600,400); - frame.setResizable(false); -// frame.pack(); - 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,"输入的密码为空"); - return false; - } else if (password1.equals(password2) == false) { - JOptionPane.showMessageDialog(null, "两次输入密码不同"); - return false; - } else return true; - } } diff --git a/Student/src/main/java/com/cfive/classroom/student/Chat.form b/Student/src/main/java/com/cfive/classroom/student/Chat.form index a0aa32f..3a9735c 100644 --- a/Student/src/main/java/com/cfive/classroom/student/Chat.form +++ b/Student/src/main/java/com/cfive/classroom/student/Chat.form @@ -8,7 +8,7 @@ - + @@ -16,40 +16,41 @@ - + - + - + - + + + - - - - - - - - - - - + - - + + - + + + + + + + + + + @@ -58,13 +59,17 @@ - + + + - + + + diff --git a/Student/src/main/java/com/cfive/classroom/student/Chat.java b/Student/src/main/java/com/cfive/classroom/student/Chat.java index d37b112..b6e66ea 100644 --- a/Student/src/main/java/com/cfive/classroom/student/Chat.java +++ b/Student/src/main/java/com/cfive/classroom/student/Chat.java @@ -1,23 +1,55 @@ package com.cfive.classroom.student; +import com.cfive.classroom.library.net.ReceiveThread; +import com.cfive.classroom.library.net.StudentNet; +import com.cfive.classroom.library.net.util.MessageObject; +import com.cfive.classroom.library.net.util.ReceiveListener; + import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Properties; public class Chat { + private static final Chat chat = new Chat(); private JPanel panel1; - private JTextField textField1; - private JButton 发送Button; - private JTextArea textArea1; - static JFrame frame = new JFrame("test"); - public void start(){ - frame.setContentPane(new Chat().panel1); - frame.setBounds(650,300,600,400); - frame.setVisible(true); - } - public static void main(String[] args) { + private JButton sendButton; + private JTextArea receiveText; + private JTextArea sendText; + private static JFrame frame = new JFrame("留言"); + private StudentNet studentNet; + private String stuNo, stuName,host; + private int port; + public Chat() { + + //发送消息 + sendButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(String.valueOf(sendText.getText())!=null) { + studentNet.sendMessageThread(new MessageObject(stuNo, stuName, null, String.valueOf(sendText.getText()), null, false)); + } + else{ + JOptionPane.showMessageDialog(null,"无发送内容"); + } + } + }); + //接收消息 + studentNet.setOnReceiveListener(new ReceiveListener() { + @Override + public void onReceive(MessageObject messageObject) { + receiveText.setText("教师:\n"+messageObject.getMessage()); + } + }); + } + public void start(String num, String name,StudentNet studentNet1) { frame.setContentPane(new Chat().panel1); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// frame.pack(); - frame.setVisible(false); + frame.setSize(600, 400); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + chat.stuNo = num; + chat.stuName = name; + chat.studentNet = studentNet1; } } diff --git a/Student/src/main/java/com/cfive/classroom/student/MainWindow.form b/Student/src/main/java/com/cfive/classroom/student/MainWindow.form index e8643a6..39f0c62 100644 --- a/Student/src/main/java/com/cfive/classroom/student/MainWindow.form +++ b/Student/src/main/java/com/cfive/classroom/student/MainWindow.form @@ -52,7 +52,7 @@ - + @@ -72,7 +72,7 @@ - + @@ -81,7 +81,7 @@ - + diff --git a/Student/src/main/java/com/cfive/classroom/student/MainWindow.java b/Student/src/main/java/com/cfive/classroom/student/MainWindow.java index 8cf1357..b813072 100644 --- a/Student/src/main/java/com/cfive/classroom/student/MainWindow.java +++ b/Student/src/main/java/com/cfive/classroom/student/MainWindow.java @@ -1,47 +1,78 @@ package com.cfive.classroom.student; +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.formdev.flatlaf.FlatLightLaf; +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.WindowEvent; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.sql.SQLException; public class MainWindow{ - + private static final MainWindow mainWindow = new MainWindow(); private JPanel rootPanel; private JLabel title; - private JTextField textField1; - private JPasswordField passwordField1; + private JTextField stuNoText; + private JPasswordField passwordText; private JButton login; - static JFrame frame = new JFrame("学生登录界面"); + private JLabel password; + private static JFrame frame = new JFrame("学生登录界面"); + private static final Logger LOGGER = LogManager.getLogger(); public MainWindow() { + //登录按钮 login.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(check()){ Center center = new Center(); - center.start(); - frame.setVisible(false); + center.start(String.valueOf(stuNoText.getText())); + frame.dispose(); } } }); - } - public boolean check(){ - String password = new String(passwordField1.getPassword()); - if (textField1.getText() == null || password.length() == 0) { + String stuPassword = new String(passwordText.getPassword()); + String stuNo = new String(stuNoText.getText()); + LOGGER.info(Long.valueOf(stuNo)); + //判断密码 + if (stuNo.length() == 0 || stuPassword.length() == 0) { JOptionPane.showMessageDialog(null, "账号密码不能为空"); return false; } else { - return true; + boolean checkPassword=false; + try { + checkPassword=DatabaseHelper.checkPasswdInStudent(Long.parseLong(stuNo),stuPassword); + } catch (NoConfigException e) { + JOptionPane.showMessageDialog(null,"没有数据库配置文件","警告",JOptionPane.ERROR_MESSAGE); + LOGGER.error("No configuration", e); + } catch (SQLException e) { + JOptionPane.showMessageDialog(null,"数据库出错","警告",JOptionPane.ERROR_MESSAGE); + LOGGER.error("SQLException",e); + } catch (DependenciesNotFoundException e) { + LOGGER.error("DependenciesNotFoundException",e); + } catch (NoSuchAlgorithmException e) { + LOGGER.error("NoSuchAlgorithmException",e); + } catch (InvalidKeySpecException e) { + LOGGER.error("InvalidKeySpecException",e); + } + return checkPassword; } } - public static void main(String[] args) { - - frame.setContentPane(new MainWindow().rootPanel); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// frame.pack(); - frame.setBounds(650,300,600,400); + FlatLightLaf.setup(); + frame.setContentPane(mainWindow.rootPanel); + frame.setSize(600,400); + frame.setTitle("登录"); + frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setResizable(false); } diff --git a/Student/src/test/java/com/cfive/classroom/student/MyTest.java b/Student/src/test/java/com/cfive/classroom/student/MyTest.java new file mode 100644 index 0000000..252833b --- /dev/null +++ b/Student/src/test/java/com/cfive/classroom/student/MyTest.java @@ -0,0 +1,20 @@ +package com.cfive.classroom.student; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; + +public class MyTest { + private static final Logger LOGGER = LogManager.getLogger(); + @Test + void firstTest() { + LOGGER.info("This is a log"); + + try { + throw new Exception("exception"); + } catch (Exception e) { + LOGGER.error("Err", e); + } + } + +} diff --git a/mysql.properties b/mysql.properties new file mode 100644 index 0000000..579eac9 --- /dev/null +++ b/mysql.properties @@ -0,0 +1,7 @@ +#MySQL Configuration +#Thu Jun 09 22:24:30 CST 2022 +port=3306 +password=class_five +database=class +ip=10.14.0.7 +username=class