概要
本文所有数据均保存在文件中。默认学生和老师账号密码均为admin。
登录模块
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Login extends JFrame implements ActionListener {
private JLabel jl1, jl2, jl3;
private JTextField jtf;
private JPasswordField jpf;
private JButton jb1;
private JRadioButton radioButton1, radioButton2;
public static String username;
public static final void setUsername(String username) {
Login.username = username;
}
public static final String getUsername() {
return username;
}
/**
* 登录
*/
Login() {
jl1 = new JLabel("账号:");
jl2 = new JLabel("密码:");
jl3 = new JLabel("权限:");
radioButton1 = new JRadioButton("管理员", true);
radioButton2 = new JRadioButton("学生");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
jtf = new JTextField(20);
jpf = new JPasswordField(20);
jb1 = new JButton("登录");
}
/**
* 窗口
*/
public void launch() {
jb1.addActionListener(this);
jl1.setBounds(50, 10, 120, 30);
jtf.setBounds(90, 10, 250, 30);
jl2.setBounds(50, 60, 120, 30);
jpf.setBounds(90, 60, 250, 30);
jl3.setBounds(50, 100, 50, 30);
radioButton1.setBounds(90, 100, 70, 30);
radioButton2.setBounds(160, 100, 100, 30);
jb1.setBounds(50, 140, 290, 30);
add(jl1);
add(jl2);
add(jtf);
add(jpf);
add(jb1);
add(jl3);
add(radioButton1);
add(radioButton2);
setLayout(null);
setTitle("在线心理测试系统");
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(null, "确认退出?", "确认", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
setSize(400, 220);
setLocationRelativeTo(null);
setResizable(false);
}
/**
* 检查登录状态
*/
public boolean checkLogin() {
String radioValue = null;
if (radioButton1.isSelected()) {
radioValue = "管理员";
} else if (radioButton2.isSelected()) {
radioValue = "学生";
}
new Account(radioValue);
for (int i = 0; i < Account.getAccounts().size(); i++) {
if (Account.getAccounts().get(i).getUsername().equals(jtf.getText()) &&
Account.getAccounts().get(i).getPassword().equals(jpf.getText())) {
Account.getAccounts().clear();
Account.setAccounts(Account.getAccounts());
return true;
}
}
Account.getAccounts().clear();
Account.setAccounts(Account.getAccounts());
return false;
}
/**
* 执行操作
*
* @param e e
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("登录")) {
if (jtf.getText().isEmpty() || jpf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "用户名或密码不能为空!", "提示消息", JOptionPane.WARNING_MESSAGE);
jtf.setText("");
jpf.setText("");
} else if (checkLogin()) {
setUsername(jtf.getText());
JOptionPane.showMessageDialog(null, "登录成功!", "提示消息", JOptionPane.WARNING_MESSAGE);
dispose();
if (radioButton1.isSelected()){
new TeacherMenu(getUsername());
}else{
new StudentMenu();
}
} else {
JOptionPane.showMessageDialog(null, "账号或密码错误!", "提示消息", JOptionPane.WARNING_MESSAGE);
}
}
}
}
管理员界面
管理员可录入题目,生成试卷,设置测试评级等等。
同时可查看具体测试结果。
学生界面
当点击具体的测试后,第一次进入完善信息的界面。
随后点击提交,进入测试界面。
答完题点击提交,可查看结果。
不允许重复作答。