parent
b532585547
commit
11bcf75553
@ -0,0 +1,278 @@
|
||||
package mathlearning.ui;
|
||||
|
||||
import mathlearning.model.User;
|
||||
import mathlearning.service.UserService;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class SelTestFrame extends JFrame {
|
||||
private MainFrame mainFrame;
|
||||
private User user;
|
||||
private UserService userService;
|
||||
|
||||
private int questionNum;
|
||||
private int currentQuestionDex;
|
||||
private String[] answers;
|
||||
|
||||
private JLabel titleLabel;
|
||||
private JButton optionA;
|
||||
private JButton optionB;
|
||||
private JButton optionC;
|
||||
private JButton optionD;
|
||||
private JButton prevButton;
|
||||
private JButton nextButton;
|
||||
private JButton submitButton;
|
||||
|
||||
public SelTestFrame(MainFrame mainFrame, User user, UserService userService, int num) {
|
||||
this.mainFrame = mainFrame;
|
||||
this.user = user;
|
||||
this.userService = userService;
|
||||
this.questionNum = num;
|
||||
this.currentQuestionDex = 0;
|
||||
this.answers = new String[questionNum];
|
||||
InitUI();
|
||||
addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
||||
handleCloseOperation();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void InitUI() {
|
||||
setTitle("答题界面");
|
||||
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
||||
setSize(800, 600);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
//主面板
|
||||
JPanel mainPanel = new JPanel(new BorderLayout());
|
||||
|
||||
//上
|
||||
titleLabel = new JLabel("当前为:第" + (currentQuestionDex + 1) + "题");
|
||||
|
||||
//左侧面板
|
||||
JPanel leftPanel = createLeftPanel();
|
||||
|
||||
//中
|
||||
JPanel centerPanel = centerPanel();
|
||||
|
||||
//右
|
||||
JPanel rightPanel = createRightPanel();
|
||||
|
||||
//底
|
||||
JPanel bottomPanel = createButtonPanel();
|
||||
|
||||
mainPanel.add(titleLabel, BorderLayout.NORTH);
|
||||
mainPanel.add(leftPanel, BorderLayout.WEST);
|
||||
mainPanel.add(centerPanel, BorderLayout.CENTER);
|
||||
mainPanel.add(rightPanel, BorderLayout.EAST);
|
||||
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
|
||||
|
||||
add(mainPanel);
|
||||
|
||||
updateNavigationButtons();
|
||||
}
|
||||
|
||||
private JPanel createLeftPanel() {
|
||||
JPanel panel = new JPanel(new BorderLayout());
|
||||
|
||||
JLabel usernameLabel = new JLabel("用户:" + user.getUsername());
|
||||
usernameLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
|
||||
usernameLabel.setForeground(Color.BLUE);
|
||||
panel.add(usernameLabel, BorderLayout.NORTH);
|
||||
|
||||
JList<String> questionList = new JList<>();
|
||||
String[] questions = new String[questionNum + 1];
|
||||
for (int i = 0; i < questionNum; i++) {
|
||||
questions[i] = "题目" + (i + 1);
|
||||
}
|
||||
questionList.setListData(questions);
|
||||
panel.add(questionList, BorderLayout.CENTER);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private JPanel centerPanel() {
|
||||
JPanel panel = new JPanel(new BorderLayout());
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private JPanel createRightPanel() {
|
||||
JPanel panel = new JPanel(new BorderLayout());
|
||||
|
||||
JLabel titleLabel = new JLabel("作答区域:");
|
||||
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
|
||||
panel.add(titleLabel, BorderLayout.NORTH);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new GridLayout(4, 1));
|
||||
|
||||
optionA = new JButton("A");
|
||||
optionA.setBackground(Color.LIGHT_GRAY);
|
||||
optionA.setForeground(Color.BLACK);
|
||||
optionA.addActionListener(e -> saveAnswer("A"));
|
||||
optionB = new JButton("B");
|
||||
optionB.setBackground(Color.LIGHT_GRAY);
|
||||
optionB.setForeground(Color.BLACK);
|
||||
optionB.addActionListener(e -> saveAnswer("B"));
|
||||
optionC = new JButton("C");
|
||||
optionC.setBackground(Color.LIGHT_GRAY);
|
||||
optionC.setForeground(Color.BLACK);
|
||||
optionC.addActionListener(e -> saveAnswer("C"));
|
||||
optionD = new JButton("D");
|
||||
optionD.setBackground(Color.LIGHT_GRAY);
|
||||
optionD.setForeground(Color.BLACK);
|
||||
optionD.addActionListener(e -> saveAnswer("D"));
|
||||
buttonPanel.add(optionA);
|
||||
buttonPanel.add(optionB);
|
||||
buttonPanel.add(optionC);
|
||||
buttonPanel.add(optionD);
|
||||
panel.add(buttonPanel, BorderLayout.CENTER);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
private JPanel createButtonPanel() {
|
||||
JPanel panel = new JPanel(new FlowLayout());
|
||||
|
||||
prevButton = new JButton("上一题");
|
||||
prevButton.addActionListener(e -> goToPrevQuestion());
|
||||
|
||||
nextButton = new JButton("下一题");
|
||||
nextButton.addActionListener(e -> goToNextQuestion());
|
||||
|
||||
submitButton = new JButton("提交");
|
||||
submitButton.addActionListener(e -> goToSubmit());
|
||||
|
||||
panel.add(prevButton);
|
||||
panel.add(nextButton);
|
||||
panel.add(submitButton);
|
||||
return panel;
|
||||
}
|
||||
|
||||
private void saveAnswer(String answer) {
|
||||
resetButtonColors();
|
||||
|
||||
switch (answer) {
|
||||
case "A":
|
||||
optionA.setBackground(Color.GREEN);
|
||||
answers[currentQuestionDex] = answer;
|
||||
break;
|
||||
case "B":
|
||||
optionB.setBackground(Color.GREEN);
|
||||
answers[currentQuestionDex] = answer;
|
||||
break;
|
||||
case "C":
|
||||
optionC.setBackground(Color.GREEN);
|
||||
answers[currentQuestionDex] = answer;
|
||||
break;
|
||||
case "D":
|
||||
optionD.setBackground(Color.GREEN);
|
||||
answers[currentQuestionDex] = answer;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void resetButtonColors() {
|
||||
optionA.setBackground(Color.LIGHT_GRAY);
|
||||
optionB.setBackground(Color.LIGHT_GRAY);
|
||||
optionC.setBackground(Color.LIGHT_GRAY);
|
||||
optionD.setBackground(Color.LIGHT_GRAY);
|
||||
}
|
||||
|
||||
private void goToPrevQuestion() {
|
||||
if (currentQuestionDex > 0) {
|
||||
currentQuestionDex--;
|
||||
updateTitleLabel();
|
||||
if (answers[currentQuestionDex] == null) {
|
||||
resetButtonColors();
|
||||
}
|
||||
else {
|
||||
saveAnswer(answers[currentQuestionDex]);
|
||||
}
|
||||
|
||||
updateNavigationButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private void goToNextQuestion() {
|
||||
if (currentQuestionDex < questionNum - 1) {
|
||||
currentQuestionDex++;
|
||||
updateTitleLabel();
|
||||
if (answers[currentQuestionDex] == null) {
|
||||
resetButtonColors();
|
||||
} else {
|
||||
saveAnswer(answers[currentQuestionDex]);
|
||||
}
|
||||
|
||||
updateNavigationButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private void goToSubmit() {
|
||||
int confirm = JOptionPane.showConfirmDialog(SelTestFrame.this, "你确定要提交试卷吗?", "提示", JOptionPane.YES_NO_OPTION);
|
||||
if (confirm == JOptionPane.YES_OPTION) {
|
||||
int score = 0;
|
||||
|
||||
// 弹出分数结果
|
||||
String[] options = {"返回主菜单", "继续做题"};
|
||||
int choice = JOptionPane.showOptionDialog(
|
||||
SelTestFrame.this,
|
||||
"您的得分是: " + score + "分",
|
||||
"测试结果",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.INFORMATION_MESSAGE,
|
||||
null,
|
||||
options,
|
||||
options[0]
|
||||
);
|
||||
|
||||
if (choice == 0) {
|
||||
mainFrame.setVisible(true);
|
||||
dispose();
|
||||
} else if (choice == 1) {
|
||||
mainFrame.reTryTest();
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateNavigationButtons() {
|
||||
if (currentQuestionDex == 0) {
|
||||
prevButton.setEnabled(false);
|
||||
prevButton.setVisible(false);
|
||||
}
|
||||
else {
|
||||
prevButton.setEnabled(true);
|
||||
prevButton.setVisible(true);
|
||||
}
|
||||
|
||||
if (currentQuestionDex == questionNum - 1) {
|
||||
nextButton.setEnabled(false);
|
||||
nextButton.setVisible(false);
|
||||
submitButton.setEnabled(true);
|
||||
submitButton.setVisible(true);
|
||||
} else {
|
||||
nextButton.setEnabled(true);
|
||||
nextButton.setVisible(true);
|
||||
submitButton.setEnabled(false);
|
||||
submitButton.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTitleLabel() {
|
||||
titleLabel.setText("当前为:第" + (currentQuestionDex + 1) + "题");
|
||||
}
|
||||
|
||||
private void handleCloseOperation() {
|
||||
int result = JOptionPane.showConfirmDialog(SelTestFrame.this, "确定要中途退出答题吗?当前答题进度将会丢失!", "确认退出", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
|
||||
|
||||
if (result == JOptionPane.YES_OPTION) {
|
||||
mainFrame.setVisible(true);
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue