From 4149fb0439c91788751c625fdc893605fa888559 Mon Sep 17 00:00:00 2001
From: GGB <1223416496@qq.com>
Date: Sun, 12 Jun 2022 18:32:06 +0800
Subject: [PATCH] Init student's Center,ChangePassword,Chat,MainWindow
---
.../com/cfive/classroom/student/Center.form | 60 ++++++------
.../com/cfive/classroom/student/Center.java | 91 +++++++++++--------
.../classroom/student/ChangePassword.java | 51 +++++------
.../com/cfive/classroom/student/Chat.java | 28 +++---
.../cfive/classroom/student/MainWindow.java | 8 +-
5 files changed, 123 insertions(+), 115 deletions(-)
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 bcabb38..4e87cf4 100644
--- a/Student/src/main/java/com/cfive/classroom/student/Center.form
+++ b/Student/src/main/java/com/cfive/classroom/student/Center.form
@@ -65,26 +65,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -119,17 +99,9 @@
-
+
-
-
-
-
-
-
-
-
@@ -138,6 +110,36 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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 f900147..ef81d0a 100644
--- a/Student/src/main/java/com/cfive/classroom/student/Center.java
+++ b/Student/src/main/java/com/cfive/classroom/student/Center.java
@@ -2,7 +2,6 @@ package com.cfive.classroom.student;
import com.cfive.classroom.library.database.DatabaseHelper;
import com.cfive.classroom.library.database.bean.AttStatus;
-import com.cfive.classroom.library.database.util.DependenciesNotFoundException;
import com.cfive.classroom.library.database.util.NoConfigException;
import com.cfive.classroom.library.net.StudentNet;
import com.cfive.classroom.library.net.util.MessageObject;
@@ -23,15 +22,15 @@ import java.util.Properties;
public class Center {
private static final Logger LOGGER = LogManager.getLogger();
- private static final Center center = new Center();
- private JButton signInButton;
+ private static Center center;
private JButton raiseHandButton;
private JButton chatButton;
private JPanel rootpanel;
private JButton changePasswordButton;
- private JTextArea textClass;
private JButton connectButton;
private JTextField stuNoText;
+ private JTextField stuNameText;
+ private JButton signInButton;
private static JFrame frame = new JFrame("Center");
private StudentNet studentNet;
private String host;
@@ -39,17 +38,22 @@ public class Center {
private String signInCode;
private String stuNo;
private String stuName;
+ private String getSignInCode;
private MessageObject messageObject;
- public Center() {
-
+ public Center(String stuNo) {
+ this.stuNo = stuNo;
+ this.stuName = getName(stuNo);
+ stuNoText.setText(stuNo);
+ stuNameText.setText(stuName);
//留言
chatButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
+ LOGGER.info("chatButton.studentNet"+studentNet);
if (studentNet != null) {
- Chat chat = new Chat();
- chat.start(stuNo, stuName, studentNet);
+ Chat chat = new Chat(studentNet,stuNo,stuName);
+ chat.start();
} else {
JOptionPane.showMessageDialog(null,"没有连接至教师");
}
@@ -59,8 +63,8 @@ public class Center {
changePasswordButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- ChangePassword changePassword = new ChangePassword();
- changePassword.start(stuNoText.getText());
+ ChangePassword changePassword = new ChangePassword(stuNoText.getText());
+ changePassword.start();
}
});
//连接
@@ -78,37 +82,35 @@ public class Center {
try {
studentNet = new StudentNet(host,port);
JOptionPane.showMessageDialog(null, "连接成功");
+ //签到
+ studentNet.setOnReceiveListener(new ReceiveListener() {
+ @Override
+ public void onReceive(MessageObject messageObject) {
+ if (messageObject.getMessageType() == MessageType.CheckIn) {
+ getSignInCode = messageObject.getCode();
+ LOGGER.info(messageObject.getCode());
+ }
+ }
+ });
+
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"连接失败");
LOGGER.error("IOException",ex);
}
+ LOGGER.info("connect.studentNet"+studentNet);
}
});
- //签到
- 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.getMessageType()==MessageType.CheckIn&&messageObject.getCode().equals(signInCode)) {
-// studentNet.sendMessageThread(new MessageObject(stuNo,stuName,null, null,null,AttStatus.signed,LocalDateTime.now(),null));
- JOptionPane.showMessageDialog(null, "签到成功");
- } else {
- JOptionPane.showMessageDialog(null,"签到失败");
- }
- }
- });
- }
- });
+
//举手
raiseHandButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
+
+ LOGGER.info(stuNo+stuName);
messageObject = new MessageObject(stuNo, stuName, null, null, null, null,null,MessageType.RaiseHand);
-// studentNet.sendMessageThread(messageObject);
+ studentNet.sendMessage(messageObject);
+ LOGGER.info(messageObject.getStuNo()+messageObject.getStuName());
JOptionPane.showMessageDialog(null,"你已经向老师举手");
}
});
@@ -123,21 +125,33 @@ public class Center {
}
});
}
+ signInButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ if (getSignInCode != null) {
+ signInCode = JOptionPane.showInputDialog(null, "签到码:", "签到", JOptionPane.PLAIN_MESSAGE);
+ LOGGER.info(getSignInCode);
+ if (getSignInCode.equals(signInCode)) {
+ studentNet.sendMessage(new MessageObject(stuNo, stuName, null, null, null, AttStatus.signed, LocalDateTime.now(), MessageType.CheckIn));
+ JOptionPane.showMessageDialog(null, "签到成功");
+ } else {
+ JOptionPane.showMessageDialog(null, "签到失败");
+ }
+ } else {
+ JOptionPane.showMessageDialog(null,"无签到码");
+ }
+ }
+ });
}
- public void start(String num){
+ public void start(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setContentPane(center.rootpanel);
+ frame.setContentPane(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 static void main(String[] args) {
@@ -150,10 +164,10 @@ public class Center {
}
//获取学生姓名
- public String getName(){
+ public String getName(String stuNo){
String name = null;
try {
- name=DatabaseHelper.selectFromStudent(Long.parseLong(stuNoText.getText())).getStuName();
+ name=DatabaseHelper.selectFromStudent(Long.parseLong(stuNo)).getStuName();
} catch (NoConfigException e) {
JOptionPane.showMessageDialog(null,"没有数据库配置文件","警告",JOptionPane.ERROR_MESSAGE);
LOGGER.error("No configuration", e);
@@ -165,4 +179,5 @@ public class Center {
}
+
}
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 0d157e3..dd18368 100644
--- a/Student/src/main/java/com/cfive/classroom/student/ChangePassword.java
+++ b/Student/src/main/java/com/cfive/classroom/student/ChangePassword.java
@@ -13,8 +13,6 @@ 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 textNum;
private JButton cancel;
@@ -22,10 +20,10 @@ public class ChangePassword {
private JPasswordField passwordField1;
private JPasswordField passwordField2;
private static JFrame frame = new JFrame("修改密码");
+ private String studentNo;
- private String stuNo;
-
- public ChangePassword() {
+ public ChangePassword(String stuNo) {
+ this.studentNo = stuNo;
confirm.addActionListener(e -> {
if(check()){
try {
@@ -57,40 +55,35 @@ public class ChangePassword {
String password1,password2,num;
num = String.valueOf(textNum.getText());
- LOGGER.info("传入学号"+stuNo);
+ LOGGER.info("传入学号"+studentNo);
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,"学号非本人学号");
+ if (num == null) {
+ JOptionPane.showMessageDialog(null, "输入的学号为空");
return false;
+ } else {
+ if(num.equals(studentNo)){
+ 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);
+ public void start() {
+ frame.setContentPane(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);
}
}
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 2df131e..9378f68 100644
--- a/Student/src/main/java/com/cfive/classroom/student/Chat.java
+++ b/Student/src/main/java/com/cfive/classroom/student/Chat.java
@@ -1,6 +1,5 @@
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.MessageType;
@@ -12,29 +11,28 @@ import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
import java.util.Properties;
public class Chat {
- private static final Chat chat = new Chat();
private JPanel panel1;
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;
private static final Logger LOGGER = LogManager.getLogger();
- public Chat() {
-
+ public Chat(StudentNet studentNet,String stuNo,String stuName) {
//发送消息
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- if(String.valueOf(sendText.getText())!=null) {
- LOGGER.info(LocalDateTime.now());
- studentNet.sendMessageThread(new MessageObject(stuNo, stuName, null, String.valueOf(sendText.getText()) ,null,null,LocalDateTime.now(),MessageType.Chat));
+ String sendMessage = sendText.getText();
+ if(sendMessage!=null) {
+ LOGGER.info(stuNo+stuName);
+ receiveText.append("我:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"))+sendMessage+"\n");
+ studentNet.sendMessage(new MessageObject(stuNo, stuName, null,stuName+":"+sendText.getText() ,null,null,LocalDateTime.now(),MessageType.Chat));
+ sendText.setText("");
}
else{
JOptionPane.showMessageDialog(null,"无发送内容","错误!",JOptionPane.ERROR_MESSAGE);
@@ -46,18 +44,16 @@ public class Chat {
@Override
public void onReceive(MessageObject messageObject) {
if(messageObject.getMessageType()==MessageType.ChatToAll){
- receiveText.append("教师:\n"+messageObject.getMessage()+"\n");
+ receiveText.append("教师:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"))+'\n'+messageObject.getMessage()+"\n");
}
}
});
}
- public void start(String num, String name,StudentNet studentNet1) {
- frame.setContentPane(new Chat().panel1);
+
+ public void start() {
+ frame.setContentPane(this.panel1);
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.java b/Student/src/main/java/com/cfive/classroom/student/MainWindow.java
index 307d8a4..eae5199 100644
--- a/Student/src/main/java/com/cfive/classroom/student/MainWindow.java
+++ b/Student/src/main/java/com/cfive/classroom/student/MainWindow.java
@@ -33,11 +33,13 @@ public class MainWindow{
@Override
public void actionPerformed(ActionEvent e) {
if(check()){
- LOGGER.info("new Center");
- new Center().start(stuNoText.getText());
- LOGGER.info("center.start");
+ Center center = new Center(stuNoText.getText());
+ center.start();
frame.dispose();
}
+ else {
+ JOptionPane.showMessageDialog(null,"密码错误");
+ }
}
});
}