package com.mathapp.panels; import com.mathapp.MathApp; import javax.swing.*; import java.awt.*; public class ResultsPanel extends JPanel { public ResultsPanel(MathApp app, int score, int totalQuestions) { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); JLabel titleLabel = new JLabel("测验完成!", SwingConstants.CENTER); titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 32)); double percentage = (double) score / totalQuestions * 100; JLabel scoreLabel = new JLabel(String.format("你的得分: %.0f 分", percentage), SwingConstants.CENTER); scoreLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24)); JLabel detailsLabel = new JLabel(String.format("(答对 %d 题,共 %d 题)", score, totalQuestions), SwingConstants.CENTER); detailsLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16)); JButton againButton = new JButton("返回选择界面"); JButton exitButton = new JButton("退出应用"); JPanel buttonPanel = new JPanel(); buttonPanel.add(againButton); buttonPanel.add(exitButton); gbc.insets = new Insets(10, 10, 10, 10); gbc.gridx = 0; gbc.gridy = 0; add(titleLabel, gbc); gbc.gridy = 1; add(scoreLabel, gbc); gbc.gridy = 2; add(detailsLabel, gbc); gbc.gridy = 3; gbc.insets = new Insets(20, 10, 10, 10); add(buttonPanel, gbc); againButton.addActionListener(e -> app.showPanel(MathApp.MAIN_MENU_PANEL)); exitButton.addActionListener(e -> System.exit(0)); } }