Last active
June 3, 2025 03:12
-
-
Save JIM-oktovisit/df422f83954519caefde366a70777a02 to your computer and use it in GitHub Desktop.
考试系统
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.util.HashMap; | |
import java.util.Map; | |
// 主窗口类,继承自 JFrame 并实现 ActionListener 接口 | |
class JTest extends JFrame implements ActionListener { | |
JButton btn1, btn2, btn3; // 登录、注册、退出按钮 | |
JLabel lab1, lab2, titleLabel; // 用户名、密码、标题标签 | |
JPanel pan1, titlePanel; // 输入面板、标题面板 | |
JTextField textField; // 用户名输入框 | |
JPasswordField passwordField; // 密码输入框 | |
// 存储注册用户信息的映射 | |
private Map<String, String> registeredUsers = new HashMap<>();// 注册用户信息 | |
// 构造函数,初始化登录注册界面 | |
JTest(String title, int x, int y, int h, int w) { | |
super(title); // 设置窗口标题 | |
this.setLayout(new GridLayout(4, 1)); // 设置布局为 4 行 1 列 | |
this.setSize(h, w); // 设置窗口大小 | |
this.setLocationRelativeTo(null); // 窗口居中显示 | |
this.setResizable(true); // 允许窗口调整大小 | |
pan1 = new JPanel(); // 创建输入面板 | |
this.add(pan1); // 添加输入面板到窗口 | |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭窗口时的操作 | |
// 标题标签 | |
JLabel titleLabel = new JLabel("欢迎进入考试系统"); | |
titleLabel.setFont(new Font("宋体", Font.BOLD, 24)); | |
titleLabel.setForeground(Color.BLUE); | |
titleLabel.setHorizontalAlignment(SwingConstants.CENTER); | |
this.add(titleLabel); | |
pan1 = new JPanel(new GridLayout(2, 2, 20, 10));// 输入面板 | |
lab1 = new JLabel("用户名:"); | |
lab2 = new JLabel("密码:"); | |
textField = new JTextField(10); | |
passwordField = new JPasswordField(10); | |
pan1.add(lab1); | |
pan1.add(textField); | |
pan1.add(lab2); | |
pan1.add(passwordField); | |
this.add(pan1, BorderLayout.CENTER); | |
btn1 = new JButton("登录"); | |
btn2 = new JButton("注册"); | |
btn3 = new JButton("退出"); | |
// 按钮面板 | |
JPanel btnPanel = new JPanel(); | |
btnPanel.add(btn1); | |
btnPanel.add(btn2); | |
btnPanel.add(btn3); | |
this.add(btnPanel); | |
btn1.addActionListener(this); | |
btn2.addActionListener(this); | |
btn3.addActionListener(this); | |
this.setVisible(true);// 显示窗口 | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (e.getSource() == btn1) { | |
System.out.println("正在登陆中"); | |
String username = textField.getText().toLowerCase(); | |
String password = new String(passwordField.getPassword()); | |
if (registeredUsers.containsKey(username) && registeredUsers.get(username).equals(password)) { | |
JOptionPane.showMessageDialog(this, "登录成功!"); | |
System.out.println("登陆成功!"); | |
String[] options = {"Java", "C++", "Python", "退出"}; | |
int choice = JOptionPane.showOptionDialog(this, "请选择你要考的科目:", "科目选择", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); | |
if (choice == 0) { | |
JOptionPane.showMessageDialog(this, "你选择了Java科目"); | |
new JavaQuizApp();// 打开选择题测试窗口 | |
} | |
else if (choice == 1) { | |
JOptionPane.showMessageDialog(this, "你选择了C++科目"); | |
JOptionPane.showMessageDialog(this,"该考试系统未开放该科目,请选择其他科目"); | |
} | |
else if (choice == 2) { | |
JOptionPane.showMessageDialog(this, "你选择了Python科目"); | |
JOptionPane.showMessageDialog(this,"该考试系统未开放该科目,请选择其他科目"); | |
} | |
else if (choice == 3) { | |
JOptionPane.showMessageDialog(this, "你选择了退出"); | |
System.exit(0); | |
} | |
} else { | |
JOptionPane.showMessageDialog(this, "登录失败!"); | |
System.out.println("登陆失败!"); | |
} | |
} else if (e.getSource() == btn2) { | |
System.out.println("正在注册中"); | |
String username = textField.getText().toLowerCase(); | |
String password = new String(passwordField.getPassword()); | |
if (registeredUsers.containsKey(username)) { | |
JOptionPane.showMessageDialog(this, "用户名已存在,请重新输入!"); | |
} else { | |
registeredUsers.put(username, password); | |
JOptionPane.showMessageDialog(this, "注册成功!"); | |
System.out.println("注册成功!"); | |
} | |
} else if (e.getSource() == btn3) { | |
System.out.println("正在退出中"); | |
System.exit(0); | |
} | |
} | |
} | |
// 选择题测试窗口类,继承自 JFrame 并实现 ActionListener 接口 | |
class JavaQuizApp extends JFrame { | |
private JLabel questionLabel;// 问题标签 | |
private JButton[] optionButtons;// 选项按钮数组 | |
private int currentQuestionIndex = 0;// 当前问题索引 | |
private int score = 0;// 记录得分 | |
private String[] questions = { | |
"以下哪个是 Java 中的基本数据类型?", | |
"下面哪个关键字用于定义一个类的构造方法?", | |
"以下代码片段的输出结果是什么? int x = 5; System.out.println(x++);", | |
"关于 Java 中的继承,以下说法正确的是?", | |
"以下代码中,`try - catch` 块的作用是什么? try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println(\"除数不能为零\"); }", | |
"下面代码中 `s1` 和 `s2` 是否相等? String s1 = \"Hello\"; String s2 = new String(\"Hello\"); System.out.println(s1 == s2);", | |
"关于 Java 中的多线程,以下哪个方法可以用来启动一个线程?", | |
"以下代码中,`List` 接口的泛型使用是否正确? import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(\"Java\"); list.add(123); } }", | |
"以下关于 Java 反射机制的说法,错误的是?" | |
}; | |
private String[][] options = { | |
{"String", "Integer", "boolean", "ArrayList"}, | |
{"class", "static", "void", "与类名相同"}, | |
{"5", "6", "编译错误", "运行时错误"}, | |
{"一个类可以继承多个类", "一个类只能继承一个类,但可以实现多个接口", "接口可以继承多个接口", "B 和 C 都正确"}, | |
{"防止程序崩溃", "捕获并处理 `ArithmeticException` 异常", "打印错误信息", "以上都是"}, | |
{"true", "false", "编译错误", "运行时错误"}, | |
{"run()", "start()", "resume()", "init()"}, | |
{"正确,因为 `ArrayList` 可以存储不同类型的元素", "错误,因为 `List<String>` 表示只能存储 `String` 类型的元素", "编译通过,但运行时会出错", "以上都不对"}, | |
{"反射可以在运行时获取类的信息", "反射可以在运行时创建对象", "反射会降低程序的性能", "反射只能用于访问公共成员"} | |
}; | |
private int[] correctAnswers = {2, 3, 0, 3, 3, 1, 1, 1, 3}; | |
// 构造函数,初始化选择题测试界面 | |
public JavaQuizApp() { | |
setTitle("Java 选择题测试"); | |
setSize(600, 400); | |
setLocationRelativeTo(null);// 窗口居中显示 | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭窗口时的操作 | |
setLayout(new BorderLayout());// 设置布局为边界布局 | |
questionLabel = new JLabel();// 创建问题标签 | |
questionLabel.setFont(new Font("宋体", Font.BOLD, 16)); | |
questionLabel.setHorizontalAlignment(SwingConstants.CENTER); | |
add(questionLabel, BorderLayout.NORTH);// 将问题标签添加到窗口的顶部 | |
JPanel optionPanel = new JPanel();// 创建选项面板 | |
optionPanel.setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));// 设置选项面板的边距 | |
optionPanel.setLayout(new GridLayout(2, 2));// 设置选项面板的布局为 4 行 4 列 | |
optionButtons = new JButton[4]; | |
for (int i = 0; i < 4; i++) { | |
optionButtons[i] = new JButton(); | |
optionButtons[i].addActionListener(new OptionButtonListener()); | |
optionPanel.add(optionButtons[i]); | |
} | |
add(optionPanel, BorderLayout.CENTER); | |
showQuestion(currentQuestionIndex); | |
setVisible(true); | |
} | |
// 显示指定索引的问题及其选项 | |
private void showQuestion(int index) { | |
StringBuilder questionText = new StringBuilder("<html><body>"); | |
questionText.append(questions[index]).append("<br>"); | |
for (int i = 0; i < 4; i++) { | |
questionText.append((char) ('A' + i)).append(". ").append(options[index][i]).append("<br>"); | |
} | |
questionText.append("</body></html>"); | |
questionLabel.setText(questionText.toString()); | |
for (int i = 0; i < 4; i++) { | |
optionButtons[i].setText(String.valueOf((char) ('A' + i))); | |
} | |
} | |
// 选项按钮的动作监听器类 | |
private class OptionButtonListener implements ActionListener { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
JButton selectedButton = (JButton) e.getSource(); | |
int selectedOption = -1; | |
for (int i = 0; i < 4; i++) { | |
if (optionButtons[i] == selectedButton) { | |
selectedOption = i; | |
break; | |
} | |
} | |
if (selectedOption == correctAnswers[currentQuestionIndex]) { | |
JOptionPane.showMessageDialog(JavaQuizApp.this, "回答正确!"); | |
score++; | |
} else { | |
JOptionPane.showMessageDialog(JavaQuizApp.this, "回答错误,正确答案是:" + (char) ('A' + correctAnswers[currentQuestionIndex])); | |
} | |
currentQuestionIndex++;// 切换到下一个问题 | |
if (currentQuestionIndex < questions.length) { | |
showQuestion(currentQuestionIndex); | |
} else { | |
JOptionPane.showMessageDialog(JavaQuizApp.this, "测试结束!满分是:" + questions.length + "分,你得了:" + score + "分"); | |
dispose(); | |
} | |
} | |
} | |
} | |
// 主类,程序入口 | |
public class JFrameTest { | |
public static void main(String[] args) { | |
new JTest("考试系统", 300, 400, 400, 300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment