Init student's Center,ChangePassword,Chat,MainWindow

This commit is contained in:
GGB
2022-06-12 18:32:06 +08:00
parent 225d69d6ef
commit 4149fb0439
5 changed files with 123 additions and 115 deletions

View File

@@ -65,26 +65,6 @@
</component>
</children>
</grid>
<grid id="9734" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="47a94" class="javax.swing.JButton" binding="signInButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="80" height="40"/>
</grid>
</constraints>
<properties>
<text value="签到"/>
</properties>
</component>
</children>
</grid>
<grid id="2e1bc" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
@@ -119,17 +99,9 @@
</constraints>
<properties>
<font name="Microsoft YaHei" size="20" style="0"/>
<text value="课程"/>
<text value="姓名"/>
</properties>
</component>
<component id="8648e" class="javax.swing.JTextArea" binding="textClass">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ebeec" class="javax.swing.JButton" binding="connectButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
@@ -138,6 +110,36 @@
<text value="连接"/>
</properties>
</component>
<component id="3e0aa" class="javax.swing.JTextField" binding="stuNameText">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<editable value="false"/>
</properties>
</component>
</children>
</grid>
<grid id="7594c" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="3824" class="javax.swing.JButton" binding="signInButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="80" height="40"/>
</grid>
</constraints>
<properties>
<text value="签到"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@@ -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 {
}
}

View File

@@ -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,12 +55,16 @@ 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 (num == null) {
JOptionPane.showMessageDialog(null, "输入的学号为空");
return false;
} else {
if(num.equals(studentNo)){
if (password1.length()==0 || password2.length()==0) {
JOptionPane.showMessageDialog(null,"输入的密码为空");
return false;
@@ -76,21 +78,12 @@ public class ChangePassword {
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);
}
}

View File

@@ -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;
}
}

View File

@@ -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,"密码错误");
}
}
});
}