@ -0,0 +1,140 @@
|
||||
package view;
|
||||
|
||||
import model.Question;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.text.SimpleAttributeSet;
|
||||
import javax.swing.text.StyleConstants;
|
||||
import javax.swing.text.StyledDocument;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class ExamView extends JPanel {
|
||||
private final JLabel progressLabel = new JLabel(" ", SwingConstants.CENTER);
|
||||
private final JProgressBar progressBar = new JProgressBar();
|
||||
private final JTextPane questionPane = new JTextPane();
|
||||
private final JRadioButton[] optionButtons = new JRadioButton[4];
|
||||
private final ButtonGroup optionsGroup = new ButtonGroup();
|
||||
|
||||
private final JButton prevButton = new JButton("上一题");
|
||||
private final JButton nextButton = new JButton("下一题");
|
||||
private final JButton finishButton = new JButton("交卷并评分");
|
||||
|
||||
public ExamView() {
|
||||
setLayout(new BorderLayout(20, 20));
|
||||
setBorder(new EmptyBorder(20, 40, 40, 40));
|
||||
|
||||
// --- 顶部进度区域 ---
|
||||
JPanel topPanel = new JPanel(new BorderLayout(0, 10));
|
||||
progressLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
|
||||
topPanel.add(progressLabel, BorderLayout.NORTH);
|
||||
|
||||
progressBar.setPreferredSize(new Dimension(1, 15));
|
||||
topPanel.add(progressBar, BorderLayout.CENTER);
|
||||
add(topPanel, BorderLayout.NORTH);
|
||||
|
||||
// --- 中间题目区域 ---
|
||||
questionPane.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36));
|
||||
questionPane.setEditable(false);
|
||||
questionPane.setFocusable(false);
|
||||
questionPane.setOpaque(false);
|
||||
StyledDocument doc = questionPane.getStyledDocument();
|
||||
SimpleAttributeSet center = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
|
||||
doc.setParagraphAttributes(0, doc.getLength(), center, false);
|
||||
|
||||
JPanel questionWrapper = new JPanel(new BorderLayout());
|
||||
questionWrapper.setOpaque(false);
|
||||
questionWrapper.add(questionPane, BorderLayout.CENTER);
|
||||
add(questionWrapper, BorderLayout.CENTER);
|
||||
|
||||
// --- 底部选项和导航 ---
|
||||
JPanel southPanel = new JPanel(new BorderLayout(20, 30));
|
||||
|
||||
// --- 选项面板 ---
|
||||
JPanel optionsPanel = new JPanel(new GridBagLayout());
|
||||
optionsPanel.setOpaque(false);
|
||||
GridBagConstraints gbcOpt = new GridBagConstraints();
|
||||
gbcOpt.insets = new Insets(15, 80, 15, 80); // 调整选项的内外边距
|
||||
gbcOpt.anchor = GridBagConstraints.WEST;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
optionButtons[i] = new JRadioButton();
|
||||
optionButtons[i].setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
|
||||
optionButtons[i].setOpaque(false);
|
||||
optionsGroup.add(optionButtons[i]);
|
||||
}
|
||||
|
||||
|
||||
gbcOpt.gridx = 0; gbcOpt.gridy = 0; optionsPanel.add(optionButtons[0], gbcOpt); // 位置 A
|
||||
gbcOpt.gridx = 1; gbcOpt.gridy = 0; optionsPanel.add(optionButtons[1], gbcOpt); // 位置 B
|
||||
gbcOpt.gridx = 0; gbcOpt.gridy = 1; optionsPanel.add(optionButtons[2], gbcOpt); // 位置 C
|
||||
gbcOpt.gridx = 1; gbcOpt.gridy = 1; optionsPanel.add(optionButtons[3], gbcOpt); // 位置 D
|
||||
|
||||
JPanel optionsWrapper = new JPanel(new GridBagLayout());
|
||||
optionsWrapper.setOpaque(false);
|
||||
optionsWrapper.add(optionsPanel);
|
||||
southPanel.add(optionsWrapper, BorderLayout.CENTER);
|
||||
|
||||
// --- 导航按钮面板 ---
|
||||
JPanel navigationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 0));
|
||||
prevButton.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
nextButton.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
finishButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
|
||||
finishButton.putClientProperty("JButton.buttonType", "roundRect");
|
||||
|
||||
navigationPanel.add(prevButton);
|
||||
navigationPanel.add(finishButton);
|
||||
navigationPanel.add(nextButton);
|
||||
southPanel.add(navigationPanel, BorderLayout.SOUTH);
|
||||
|
||||
add(southPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
public void displayQuestion(Question q, int qNum, int total, int savedAnswerIndex) {
|
||||
progressLabel.setText(String.format("第 %d 题 / 共 %d 题", qNum, total));
|
||||
progressBar.setValue(qNum);
|
||||
questionPane.setText(q.getQuestionText());
|
||||
|
||||
StyledDocument doc = questionPane.getStyledDocument();
|
||||
SimpleAttributeSet center = new SimpleAttributeSet();
|
||||
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
|
||||
doc.setParagraphAttributes(0, doc.getLength(), center, false);
|
||||
|
||||
optionsGroup.clearSelection();
|
||||
List<String> options = q.getOptions();
|
||||
|
||||
char optionLabel = 'A';
|
||||
for (int i = 0; i < 4; i++) {
|
||||
optionButtons[i].setText(optionLabel + ". " + options.get(i));
|
||||
optionLabel++;
|
||||
}
|
||||
|
||||
if (savedAnswerIndex != -1) {
|
||||
optionButtons[savedAnswerIndex].setSelected(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void setupForExam(int totalQuestions) {
|
||||
progressBar.setMinimum(1);
|
||||
progressBar.setMaximum(totalQuestions);
|
||||
}
|
||||
|
||||
public void updateNavigationButtons(int qIndex, int total) {
|
||||
prevButton.setEnabled(qIndex > 0);
|
||||
nextButton.setEnabled(qIndex < total - 1);
|
||||
nextButton.setText(qIndex == total - 2 ? "最后一题" : "下一题");
|
||||
}
|
||||
|
||||
public int getSelectedIndex() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (optionButtons[i].isSelected()) { return i; }
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public JButton getPrevButton() { return prevButton; }
|
||||
public JButton getNextButton() { return nextButton; }
|
||||
public JButton getFinishButton() { return finishButton; }
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
package view;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class LoginView extends JPanel {
|
||||
private final JTextField identifierField = new JTextField(20);
|
||||
private final JPasswordField passwordField = new JPasswordField(20);
|
||||
private final JButton loginButton = new JButton("登 录");
|
||||
private final JButton registerButton = new JButton("还没有账号?立即注册");
|
||||
|
||||
public LoginView() {
|
||||
setLayout(new GridBagLayout());
|
||||
|
||||
JPanel formPanel = new JPanel(new GridBagLayout());
|
||||
formPanel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.insets = new Insets(10, 10, 10, 10);
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
|
||||
JLabel titleLabel = new JLabel("欢迎回来", SwingConstants.CENTER);
|
||||
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 28));
|
||||
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
|
||||
formPanel.add(titleLabel, gbc);
|
||||
|
||||
gbc.gridwidth = 1;
|
||||
gbc.gridx = 0; gbc.gridy = 1; addLabel(formPanel, "邮箱 / 用户名:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 1; addComponent(formPanel, identifierField, gbc);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 2; addLabel(formPanel, "密码:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 2; addComponent(formPanel, passwordField, gbc);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2;
|
||||
loginButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
|
||||
addComponent(formPanel, loginButton, gbc);
|
||||
|
||||
gbc.gridy = 4;
|
||||
|
||||
registerButton.putClientProperty("FlatLaf.style", "buttonType: borderless");
|
||||
addComponent(formPanel, registerButton, gbc);
|
||||
|
||||
add(formPanel);
|
||||
}
|
||||
|
||||
private void addLabel(JPanel panel, String text, GridBagConstraints gbc) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
panel.add(label, gbc);
|
||||
}
|
||||
|
||||
private void addComponent(JPanel panel, JComponent comp, GridBagConstraints gbc) {
|
||||
comp.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
panel.add(comp, gbc);
|
||||
}
|
||||
|
||||
public String getIdentifier() { return identifierField.getText().trim(); }
|
||||
public String getPassword() { return new String(passwordField.getPassword()); }
|
||||
public JButton getLoginButton() { return loginButton; }
|
||||
public JButton getRegisterButton() { return registerButton; }
|
||||
public void clearFields() {
|
||||
identifierField.setText("");
|
||||
passwordField.setText("");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package view;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class MainFrame extends JFrame {
|
||||
private final CardLayout cardLayout = new CardLayout();
|
||||
private final JPanel mainPanel = new JPanel(cardLayout);
|
||||
|
||||
public MainFrame() {
|
||||
super("数学学习软件");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(800, 600);
|
||||
setLocationRelativeTo(null);
|
||||
setResizable(false);
|
||||
add(mainPanel);
|
||||
}
|
||||
|
||||
public void addPanel(JPanel panel, String name) {
|
||||
mainPanel.add(panel, name);
|
||||
}
|
||||
|
||||
public void showPanel(String name) {
|
||||
cardLayout.show(mainPanel, name);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
package view;
|
||||
|
||||
import model.UserType;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class MainMenuView extends JPanel {
|
||||
private final JLabel welcomeLabel = new JLabel("", SwingConstants.CENTER);
|
||||
private final JComboBox<String> typeComboBox = new JComboBox<>();
|
||||
private final JSpinner countSpinner = new JSpinner(new SpinnerNumberModel(10, 10, 30, 1));
|
||||
private final JButton startExamButton = new JButton("开始答题");
|
||||
private final JButton changePasswordButton = new JButton("修改密码");
|
||||
private final JButton logoutButton = new JButton("退出登录");
|
||||
|
||||
public MainMenuView() {
|
||||
setLayout(new BorderLayout(20, 20));
|
||||
setBorder(BorderFactory.createEmptyBorder(40, 40, 40, 40));
|
||||
|
||||
welcomeLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 28));
|
||||
add(welcomeLabel, BorderLayout.NORTH);
|
||||
|
||||
JPanel centerPanel = new JPanel(new GridBagLayout());
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.insets = new Insets(15, 10, 15, 10);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 0; addLabel(centerPanel, "选择题目难度:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 0; addComponent(centerPanel, typeComboBox, gbc);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 1; addLabel(centerPanel, "选择题目数量:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 1; addComponent(centerPanel, countSpinner, gbc);
|
||||
|
||||
startExamButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
|
||||
|
||||
startExamButton.putClientProperty("JButton.buttonType", "roundRect");
|
||||
gbc.gridy = 2; gbc.gridwidth = 2; gbc.insets = new Insets(30, 10, 15, 10);
|
||||
addComponent(centerPanel, startExamButton, gbc);
|
||||
add(centerPanel, BorderLayout.CENTER);
|
||||
|
||||
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 0));
|
||||
southPanel.add(changePasswordButton);
|
||||
southPanel.add(logoutButton);
|
||||
add(southPanel, BorderLayout.SOUTH);
|
||||
|
||||
for (UserType type : UserType.values()) {
|
||||
typeComboBox.addItem(type.getDisplayName());
|
||||
}
|
||||
}
|
||||
|
||||
private void addLabel(JPanel panel, String text, GridBagConstraints gbc) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
panel.add(label, gbc);
|
||||
}
|
||||
|
||||
private void addComponent(JPanel panel, JComponent comp, GridBagConstraints gbc) {
|
||||
comp.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
panel.add(comp, gbc);
|
||||
}
|
||||
|
||||
public void setWelcomeMessage(String message) { welcomeLabel.setText(message); }
|
||||
public UserType getSelectedType() {
|
||||
String displayName = (String) typeComboBox.getSelectedItem();
|
||||
for (UserType type : UserType.values()) {
|
||||
if (type.getDisplayName().equals(displayName)) return type;
|
||||
}
|
||||
return UserType.PRIMARY;
|
||||
}
|
||||
public int getQuestionCount() { return (int) countSpinner.getValue(); }
|
||||
public JButton getStartExamButton() { return startExamButton; }
|
||||
public JButton getChangePasswordButton() { return changePasswordButton; }
|
||||
public JButton getLogoutButton() { return logoutButton; }
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
package view;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class RegisterView extends JPanel {
|
||||
private final JTextField emailField = new JTextField(20);
|
||||
private final JTextField usernameField = new JTextField(20);
|
||||
private final JTextField codeField = new JTextField(10);
|
||||
private final JButton getCodeButton = new JButton("发送验证邮件");
|
||||
private final JButton registerButton = new JButton("下一步:设置密码");
|
||||
private final JButton backButton = new JButton("返回登录");
|
||||
|
||||
public RegisterView() {
|
||||
setLayout(new GridBagLayout());
|
||||
JPanel formPanel = new JPanel(new GridBagLayout());
|
||||
formPanel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));
|
||||
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.insets = new Insets(10, 10, 10, 10);
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
|
||||
JLabel titleLabel = new JLabel("创建您的账号", SwingConstants.CENTER);
|
||||
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 28));
|
||||
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; formPanel.add(titleLabel, gbc);
|
||||
|
||||
gbc.gridwidth = 1;
|
||||
gbc.gridx = 0; gbc.gridy = 1; addLabel(formPanel, "邮箱地址:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 1; addComponent(formPanel, emailField, gbc);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 2; addLabel(formPanel, "设置用户名:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 2; addComponent(formPanel, usernameField, gbc);
|
||||
|
||||
JPanel codePanel = new JPanel(new BorderLayout(10, 0));
|
||||
addComponent(codePanel, codeField, BorderLayout.CENTER);
|
||||
codePanel.add(getCodeButton, BorderLayout.EAST);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 3; addLabel(formPanel, "邮箱验证码:", gbc);
|
||||
gbc.gridx = 1; gbc.gridy = 3; formPanel.add(codePanel, gbc);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 0));
|
||||
registerButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
|
||||
backButton.putClientProperty("FlatLaf.style", "buttonType: borderless");
|
||||
buttonPanel.add(registerButton);
|
||||
buttonPanel.add(backButton);
|
||||
gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 2; gbc.insets = new Insets(20, 10, 10, 10);
|
||||
formPanel.add(buttonPanel, gbc);
|
||||
|
||||
add(formPanel);
|
||||
}
|
||||
|
||||
private void addLabel(JPanel p, String s, GridBagConstraints g) {
|
||||
JLabel label = new JLabel(s);
|
||||
label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
p.add(label, g);
|
||||
}
|
||||
private void addComponent(JPanel p, JComponent c, GridBagConstraints g) {
|
||||
c.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
p.add(c, g);
|
||||
}
|
||||
private void addComponent(JPanel p, JComponent c, String pos) {
|
||||
c.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
|
||||
p.add(c, pos);
|
||||
}
|
||||
|
||||
public String getEmail() { return emailField.getText().trim(); }
|
||||
public String getUsername() { return usernameField.getText().trim(); }
|
||||
public String getCode() { return codeField.getText().trim(); }
|
||||
public JButton getGetCodeButton() { return getCodeButton; }
|
||||
public JButton getRegisterButton() { return registerButton; }
|
||||
public JButton getBackButton() { return backButton; }
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package view;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import java.awt.*;
|
||||
|
||||
public class ScoreView extends JPanel {
|
||||
private final JLabel scoreLabel = new JLabel("0", SwingConstants.CENTER);
|
||||
private final JLabel scoreUnitLabel = new JLabel("分", SwingConstants.CENTER);
|
||||
private final JLabel detailLabel = new JLabel(" ", SwingConstants.CENTER);
|
||||
private final JButton continueButton = new JButton("再来一组");
|
||||
private final JButton exitButton = new JButton("退出登录");
|
||||
|
||||
public ScoreView() {
|
||||
setLayout(new GridBagLayout());
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.gridwidth = GridBagConstraints.REMAINDER;
|
||||
gbc.insets = new Insets(10, 10, 10, 10);
|
||||
|
||||
JLabel titleLabel = new JLabel("答题结束!", SwingConstants.CENTER);
|
||||
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 32));
|
||||
add(titleLabel, gbc);
|
||||
|
||||
JPanel scorePanel = new JPanel();
|
||||
scoreLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 100));
|
||||
scoreUnitLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 30));
|
||||
scoreUnitLabel.setBorder(new EmptyBorder(0,0, -50, 0));
|
||||
scorePanel.add(scoreLabel);
|
||||
scorePanel.add(scoreUnitLabel);
|
||||
add(scorePanel, gbc);
|
||||
|
||||
detailLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
|
||||
add(detailLabel, gbc);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
|
||||
continueButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
|
||||
// 【修复】将 "=" 修改为 ":"
|
||||
continueButton.putClientProperty("JButton.buttonType", "roundRect");
|
||||
buttonPanel.add(continueButton);
|
||||
buttonPanel.add(exitButton);
|
||||
gbc.insets = new Insets(30, 10, 10, 10);
|
||||
add(buttonPanel, gbc);
|
||||
}
|
||||
|
||||
public void setScore(int score, int correct, int total) {
|
||||
scoreLabel.setText(String.valueOf(score));
|
||||
detailLabel.setText(String.format("答对 %d 题,共 %d 题", correct, total));
|
||||
}
|
||||
|
||||
public JButton getContinueButton() { return continueButton; }
|
||||
public JButton getExitButton() { return exitButton; }
|
||||
}
|
||||
Loading…
Reference in new issue