用swing写一个在线考试系统
时间: 2023-12-29 20:51:01 浏览: 30
好的,以下是一个简单的在线考试系统的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class OnlineExamSystem extends JFrame implements ActionListener {
private JLabel titleLabel;
private JLabel questionLabel;
private JRadioButton option1, option2, option3, option4;
private JButton submitButton;
private int score = 0;
private int currentQuestion = 0;
private String[] questions = {"What is the capital of France?",
"What is the largest ocean in the world?",
"What is the highest mountain in the world?",
"What is the currency of Japan?"};
private String[][] options = {{"London", "Paris", "New York", "Tokyo"},
{"Atlantic Ocean", "Pacific Ocean", "Indian Ocean", "Arctic Ocean"},
{"Mount Everest", "K2", "Kilimanjaro", "Matterhorn"},
{"Dollar", "Euro", "Yen", "Pound Sterling"}};
private int[] answers = {1, 2, 0, 2};
public OnlineExamSystem() {
super("Online Exam System");
setSize(500, 300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add title label
titleLabel = new JLabel("Welcome to the Online Exam System!");
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(titleLabel, BorderLayout.NORTH);
// Add question label
questionLabel = new JLabel("Question " + (currentQuestion+1) + ": " + questions[currentQuestion]);
questionLabel.setFont(new Font("Arial", Font.PLAIN, 16));
add(questionLabel, BorderLayout.CENTER);
// Add radio buttons for options
JPanel optionsPanel = new JPanel(new GridLayout(4, 1));
option1 = new JRadioButton(options[currentQuestion][0]);
option2 = new JRadioButton(options[currentQuestion][1]);
option3 = new JRadioButton(options[currentQuestion][2]);
option4 = new JRadioButton(options[currentQuestion][3]);
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
group.add(option3);
group.add(option4);
optionsPanel.add(option1);
optionsPanel.add(option2);
optionsPanel.add(option3);
optionsPanel.add(option4);
add(optionsPanel, BorderLayout.SOUTH);
// Add submit button
submitButton = new JButton("Submit");
submitButton.addActionListener(this);
add(submitButton, BorderLayout.EAST);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (option1.isSelected() && answers[currentQuestion] == 0) {
score++;
} else if (option2.isSelected() && answers[currentQuestion] == 1) {
score++;
} else if (option3.isSelected() && answers[currentQuestion] == 2) {
score++;
} else if (option4.isSelected() && answers[currentQuestion] == 3) {
score++;
}
currentQuestion++;
if (currentQuestion < questions.length) {
questionLabel.setText("Question " + (currentQuestion+1) + ": " + questions[currentQuestion]);
option1.setText(options[currentQuestion][0]);
option2.setText(options[currentQuestion][1]);
option3.setText(options[currentQuestion][2]);
option4.setText(options[currentQuestion][3]);
} else {
JOptionPane.showMessageDialog(null, "Your score is " + score + "/" + questions.length);
System.exit(0);
}
}
public static void main(String[] args) {
new OnlineExamSystem();
}
}
```
该系统有四个问题,每个问题有四个选项。用户可以选择其中一个选项并提交答案,每个正确答案加一分。最后显示用户的总分数。
注意:该示例代码只是一个简单的在线考试系统,仅供参考和学习。实际应用中可能需要更多的功能和更复杂的实现。