用java的swing图形界面采用表格显示裁判评分,显示n个裁判的评分,随裁判人数变化而变化;指定分数范围,若超出,则异常处理;得分规则有指定接口约定,根据制定规则计算出最后得分。由多个接口对象给出多种得分规则,如求平均数值,或去掉一个最高分与一个最低分后,再求平均值。可用按钮选择规则来改变分数
时间: 2024-02-19 19:03:30 浏览: 232
java的swing界面
好的,这是一个关于用Java Swing实现表格显示裁判评分的问题,需要考虑以下几个方面:
1. 如何动态显示n个裁判的评分?
可以使用JTable来实现表格的显示,其中每一行代表一个裁判,每一列代表一个评分。当裁判人数变化时,只需要重新设置JTable的行数即可。
2. 如何进行异常处理以防止分数超出指定范围?
可以在输入分数时判断是否超出指定范围,如果超出则抛出异常并进行相应的处理。
3. 如何根据不同的规则计算出最后得分?
可以定义一个接口ScoreRule,其中包含一个calculateScore方法用于计算最后得分。然后分别实现该接口以实现不同的得分规则。在选择规则时,只需要将对应的接口对象传入即可。
以下是一个简单的代码示例:
```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 ScoreTable extends JFrame {
private JTable table;
private JLabel lblMaxScore, lblMinScore;
private JTextField txtMaxScore, txtMinScore;
private JButton btnCalculate, btnAvgScore, btnAdjustedScore;
private List<ScoreRule> scoreRules = new ArrayList<>();
private ScoreRule currScoreRule;
public ScoreTable() {
setTitle("裁判评分表");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 初始化表格
table = new JTable(new DefaultTableModel(new Object[][]{}, new Object[]{"裁判1", "裁判2", "裁判3", "裁判4", "裁判5"}));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
// 初始化控件
JPanel panel = new JPanel(new GridLayout(2, 4));
lblMaxScore = new JLabel("最高分");
panel.add(lblMaxScore);
txtMaxScore = new JTextField();
panel.add(txtMaxScore);
lblMinScore = new JLabel("最低分");
panel.add(lblMinScore);
txtMinScore = new JTextField();
panel.add(txtMinScore);
btnCalculate = new JButton("计算得分");
panel.add(btnCalculate);
btnAvgScore = new JButton("平均分");
panel.add(btnAvgScore);
btnAdjustedScore = new JButton("去掉最高分和最低分的平均分");
panel.add(btnAdjustedScore);
add(panel, BorderLayout.SOUTH);
// 添加得分规则
scoreRules.add(new AvgScoreRule());
scoreRules.add(new AdjustedScoreRule());
currScoreRule = scoreRules.get(0);
// 注册事件
btnCalculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateScore();
}
});
btnAvgScore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currScoreRule = scoreRules.get(0);
}
});
btnAdjustedScore.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
currScoreRule = scoreRules.get(1);
}
});
}
// 计算得分
private void calculateScore() {
try {
int rowCount = table.getRowCount();
int colCount = table.getColumnCount();
List<Double> scores = new ArrayList<>();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
double score = Double.parseDouble(table.getValueAt(i, j).toString());
if (score < Double.parseDouble(txtMinScore.getText()) || score > Double.parseDouble(txtMaxScore.getText())) {
throw new Exception("分数超出指定范围");
}
scores.add(score);
}
}
double finalScore = currScoreRule.calculateScore(scores);
JOptionPane.showMessageDialog(this, "最终得分为:" + finalScore);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
// 平均分规则
private class AvgScoreRule implements ScoreRule {
@Override
public double calculateScore(List<Double> scores) {
double totalScore = 0;
for (double score : scores) {
totalScore += score;
}
return totalScore / scores.size();
}
}
// 去掉最高分和最低分的平均分规则
private class AdjustedScoreRule implements ScoreRule {
@Override
public double calculateScore(List<Double> scores) {
scores.remove(Collections.min(scores));
scores.remove(Collections.max(scores));
double totalScore = 0;
for (double score : scores) {
totalScore += score;
}
return totalScore / scores.size();
}
}
// 得分规则接口
private interface ScoreRule {
double calculateScore(List<Double> scores);
}
public static void main(String[] args) {
ScoreTable scoreTable = new ScoreTable();
scoreTable.setVisible(true);
}
}
```
阅读全文