java垃圾分类答题系统随机抽题有图形化界面,代码实现
时间: 2024-01-14 12:04:07 浏览: 100
以下是一个简单的 Java 垃圾分类答题系统,其中包含随机抽题和图形化界面。代码如下:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GarbageSortingQuiz extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton startButton, nextButton, submitButton;
private JPanel quizPanel, resultPanel;
private JLabel questionLabel, resultLabel;
private JRadioButton option1, option2, option3, option4;
private ButtonGroup optionsGroup;
private String[][] quizData = {{"Which of the following is a biodegradable waste?", "Plastic bags", "Food waste", "Aluminum cans", "Glass bottles", "2"},
{"Which of the following is an example of hazardous waste?", "Old newspapers", "Used batteries", "Cardboard boxes", "Empty soda cans", "2"},
{"Which of the following is a type of recyclable waste?", "Styrofoam cups", "Plastic straws", "Metal cans", "Cotton balls", "3"},
{"Which of the following is a non-biodegradable waste?", "Vegetable peels", "Eggshells", "Napkins", "Plastic bottles", "4"},
{"Which of the following is a type of e-waste?", "Old books", "Broken toys", "Used cell phones", "Empty milk jugs", "3"}};
private int currentQuestion = 0;
private int score = 0;
public GarbageSortingQuiz() {
setTitle("Garbage Sorting Quiz");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
startButton = new JButton("Start Quiz");
startButton.addActionListener(this);
nextButton = new JButton("Next");
nextButton.addActionListener(this);
submitButton = new JButton("Submit");
submitButton.addActionListener(this);
quizPanel = new JPanel(new GridLayout(6, 1));
quizPanel.add(new JLabel(""));
questionLabel = new JLabel("");
quizPanel.add(questionLabel);
option1 = new JRadioButton("");
option2 = new JRadioButton("");
option3 = new JRadioButton("");
option4 = new JRadioButton("");
optionsGroup = new ButtonGroup();
optionsGroup.add(option1);
optionsGroup.add(option2);
optionsGroup.add(option3);
optionsGroup.add(option4);
quizPanel.add(option1);
quizPanel.add(option2);
quizPanel.add(option3);
quizPanel.add(option4);
resultPanel = new JPanel(new GridLayout(2, 1));
resultPanel.add(new JLabel(""));
resultLabel = new JLabel("");
resultPanel.add(resultLabel);
add(startButton);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
add(quizPanel);
remove(startButton);
currentQuestion = 0;
score = 0;
showQuestion();
add(nextButton);
validate();
}
if (e.getSource() == nextButton) {
if (currentQuestion < quizData.length - 1) {
if (checkAnswer()) score++;
currentQuestion++;
showQuestion();
} else {
if (checkAnswer()) score++;
showResult();
}
}
if (e.getSource() == submitButton) {
if (checkAnswer()) score++;
showResult();
}
}
public void showQuestion() {
questionLabel.setText(quizData[currentQuestion][0]);
option1.setText(quizData[currentQuestion][1]);
option2.setText(quizData[currentQuestion][2]);
option3.setText(quizData[currentQuestion][3]);
option4.setText(quizData[currentQuestion][4]);
optionsGroup.clearSelection();
}
public boolean checkAnswer() {
if (option1.isSelected() && quizData[currentQuestion][5].equals("1")) return true;
if (option2.isSelected() && quizData[currentQuestion][5].equals("2")) return true;
if (option3.isSelected() && quizData[currentQuestion][5].equals("3")) return true;
if (option4.isSelected() && quizData[currentQuestion][5].equals("4")) return true;
return false;
}
public void showResult() {
remove(quizPanel);
remove(nextButton);
remove(submitButton);
resultLabel.setText("You scored " + score + " out of " + quizData.length);
add(resultPanel);
validate();
}
public static void main(String[] args) {
new GarbageSortingQuiz();
}
}
```
该程序使用了 Swing 库来创建图形化界面。在主方法中,创建了一个 GarbageSortingQuiz 对象,该对象在构造函数中设置了程序的标题和大小,并创建了开始按钮。在 actionPerformed 方法中,为开始按钮、下一题按钮和提交按钮添加了事件监听器。当开始按钮被点击时,会创建出一个包含问题和选项的面板,并将开始按钮从程序中移除。当下一题按钮被点击时,会检查前一题的答案是否正确,并显示下一题。当提交按钮被点击时,会检查最后一题的答案是否正确,并显示得分。showQuestion 方法用于显示当前的问题和选项,checkAnswer 方法用于检查选中的选项是否正确,showResult 方法用于显示最终得分。
阅读全文