设计如图所示图形界面,显示n个裁判的评分,根据制定规则计算出最后得分。要求:图形界面采用表格显示裁判评分,随裁判人数变化而变化;指定分数范围,若超出,则异常处理;得分规则有指定接口约定,由多个接口对象给出多种得分规则,如求平均数值,或去掉一个最高分与一个最低分后,再求平均值。
时间: 2024-01-25 15:05:14 浏览: 70
图形界面计算器用于成绩的统计
本题需要实现一个Java图形界面,用于显示n个裁判的评分,并根据制定规则计算出最后得分。需要实现的功能包括:
1. 图形界面采用表格显示裁判评分,随裁判人数变化而变化。
2. 指定分数范围,若超出,则异常处理。
3. 得分规则有指定接口约定,由多个接口对象给出多种得分规则,如求平均数值,或去掉一个最高分与一个最低分后,再求平均值。
以下是一个简单的实现示例代码,供参考:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ScoreCalculator extends JFrame implements ActionListener {
private JTable scoresTable;
private DefaultTableModel tableModel;
private JTextField scoreField;
private JButton addButton;
private JComboBox<String> ruleComboBox;
private JLabel resultLabel;
private List<Double> scores = new ArrayList<>();
private ScoreRule scoreRule;
public ScoreCalculator() {
setSize(600, 400);
setTitle("Score Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"No.", "Score"};
tableModel = new DefaultTableModel(columnNames, 0);
scoresTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(scoresTable);
scoreField = new JTextField(10);
addButton = new JButton("Add Score");
ruleComboBox = new JComboBox<>(new String[]{"Average", "Remove Highest and Lowest", "Custom"});
resultLabel = new JLabel("Result: N/A");
addButton.addActionListener(this);
ruleComboBox.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
panel.add(new JLabel("Scores:"), constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.fill = GridBagConstraints.BOTH;
panel.add(scrollPane, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.NONE;
panel.add(new JLabel("Add Score:"), constraints);
constraints.gridx = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
panel.add(scoreField, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
constraints.fill = GridBagConstraints.NONE;
panel.add(new JLabel("Score Rule:"), constraints);
constraints.gridx = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
panel.add(ruleComboBox, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 2;
constraints.fill = GridBagConstraints.HORIZONTAL;
panel.add(addButton, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
constraints.gridwidth = 2;
panel.add(resultLabel, constraints);
add(panel);
setVisible(true);
}
private void addScore(double score) {
if (score < 0 || score > 10) {
JOptionPane.showMessageDialog(this, "Invalid score!");
return;
}
scores.add(score);
int no = scores.size();
Object[] rowData = {no, score};
tableModel.addRow(rowData);
}
private void calculateResult() {
if (scoreRule == null) {
resultLabel.setText("Result: N/A");
return;
}
double result = scoreRule.calculateScore(scores);
resultLabel.setText("Result: " + result);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
try {
double score = Double.parseDouble(scoreField.getText());
addScore(score);
calculateResult();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid score!");
}
} else if (e.getSource() == ruleComboBox) {
String selectedRule = (String) ruleComboBox.getSelectedItem();
if (selectedRule.equals("Average")) {
scoreRule = new AverageScoreRule();
} else if (selectedRule.equals("Remove Highest and Lowest")) {
scoreRule = new RemoveHighestAndLowestScoreRule();
} else {
scoreRule = null;
}
calculateResult();
}
}
public static void main(String[] args) {
new ScoreCalculator();
}
}
interface ScoreRule {
double calculateScore(List<Double> scores);
}
class AverageScoreRule implements ScoreRule {
@Override
public double calculateScore(List<Double> scores) {
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.size();
}
}
class RemoveHighestAndLowestScoreRule implements ScoreRule {
@Override
public double calculateScore(List<Double> scores) {
if (scores.size() <= 2) {
return calculateAverage(scores);
}
List<Double> sortedScores = new ArrayList<>(scores);
Collections.sort(sortedScores);
sortedScores.remove(0);
sortedScores.remove(sortedScores.size() - 1);
return calculateAverage(sortedScores);
}
private double calculateAverage(List<Double> scores) {
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.size();
}
}
```
在这个示例代码中,我们创建了一个 `ScoreCalculator` 类,继承自 `JFrame`,实现了 `ActionListener` 接口。在构造函数中,我们创建了一个包含表格、输入框、按钮、下拉框和标签的面板,并将其添加到窗口中。
在 `addScore` 方法中,我们首先检查输入的评分是否在指定范围内,如果不在则弹出错误提示框。否则,我们将评分添加到评分列表中,并在表格中添加一行显示评分。
在 `calculateResult` 方法中,我们根据当前选择的得分规则计算最终得分,并将结果显示在标签中。
在 `actionPerformed` 方法中,我们首先检查事件源是哪个组件。如果是添加评分按钮,则尝试将用户输入的评分转换为一个 `double` 类型的数值,如果转换失败,则弹出一个错误提示框。否则,我们调用 `addScore` 和 `calculateResult` 方法,将新评分添加到评分列表中并重新计算最终得分。如果是得分规则下拉框,则根据选择的规则创建相应的得分规则对象,并重新计算最终得分。
最后,在 `main` 方法中,我们创建了一个 `ScoreCalculator` 实例,启动了整个应用程序。
阅读全文