写一个java的swing点名程序,教师端:配置服务端端口号,发送给学生,开始进行电子点名,等待学生端发送个人信息,将收到的学生信息显示在界面中,点名结束后关闭服务端服务。同时还有随机点名功能,输入点名数量,在数据库中随机抽取相应数量的学生,对应学生端显示需要显示点名界面。学生信息显示学号,姓名,ip,每行表示一个学生。实现将本次点名数据保存至数据库。
时间: 2023-12-05 16:06:37 浏览: 75
好的,下面是一个简单的Java Swing点名程序的示例代码:
教师端:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TeacherFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea messageArea;
private JButton startButton;
private JButton randomButton;
private ServerSocket serverSocket;
private List<Student> students;
public TeacherFrame() {
setTitle("教师端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
messageArea = new JTextArea();
add(new JScrollPane(messageArea), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
startButton = new JButton("开始点名");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startAttendance();
}
});
buttonPanel.add(startButton);
randomButton = new JButton("随机点名");
randomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
randomAttendance();
}
});
buttonPanel.add(randomButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private void startAttendance() {
try {
messageArea.append("等待学生加入...\n");
serverSocket = new ServerSocket(8888);
new Thread() {
public void run() {
students = new ArrayList<Student>();
try {
while (true) {
Socket socket = serverSocket.accept();
Student student = new Student(socket);
students.add(student);
student.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
startButton.setEnabled(false);
randomButton.setEnabled(false);
} catch (IOException e) {
e.printStackTrace();
}
}
private void randomAttendance() {
int count = Integer.parseInt(messageArea.getText().trim());
List<Student> selectedStudents = new ArrayList<Student>();
Random random = new Random();
for (int i = 0; i < count; i++) {
int index = random.nextInt(students.size());
selectedStudents.add(students.get(index));
}
for (Student student : selectedStudents) {
student.showAttendancePage();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TeacherFrame frame = new TeacherFrame();
frame.setVisible(true);
}
});
}
class Student extends Thread {
private Socket socket;
private String name;
private String ip;
public Student(Socket socket) {
this.socket = socket;
this.ip = socket.getInetAddress().getHostAddress();
}
public void run() {
try {
AttendancePage attendancePage = new AttendancePage(this);
attendancePage.setVisible(true);
while (true) {
byte[] buffer = new byte[1024];
int len = socket.getInputStream().read(buffer);
String message = new String(buffer, 0, len);
if (message.startsWith("name:")) {
name = message.substring(5);
messageArea.append(name + "加入点名\n");
} else if (message.equals("attendance")) {
attendancePage.setAttendance(true);
messageArea.append(name + "已到\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void showAttendancePage() {
AttendancePage attendancePage = new AttendancePage(this);
attendancePage.setVisible(true);
}
public String getIp() {
return ip;
}
public String getName() {
return name;
}
}
}
```
学生端:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StudentFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField nameField;
private JButton joinButton;
private JButton attendanceButton;
private Socket socket;
public StudentFrame() {
setTitle("学生端");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
nameField = new JTextField();
nameField.setPreferredSize(new Dimension(200, 30));
add(nameField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
joinButton = new JButton("加入点名");
joinButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
joinAttendance();
}
});
buttonPanel.add(joinButton);
attendanceButton = new JButton("已到");
attendanceButton.setEnabled(false);
attendanceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendAttendance();
}
});
buttonPanel.add(attendanceButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private void joinAttendance() {
try {
socket = new Socket("localhost", 8888);
socket.getOutputStream().write(("name:" + nameField.getText()).getBytes());
socket.getOutputStream().flush();
joinButton.setEnabled(false);
attendanceButton.setEnabled(true);
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendAttendance() {
try {
socket.getOutputStream().write("attendance".getBytes());
socket.getOutputStream().flush();
attendanceButton.setEnabled(false);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
StudentFrame frame = new StudentFrame();
frame.setVisible(true);
}
});
}
}
```
注意:以上代码仅为示例,可能存在一些不足之处,请根据具体需求进行修改和完善。另外,本示例未包含将点名数据保存至数据库的功能,需要自行添加。
阅读全文