我正在努力完成学校的迟到作业。任务是建立一个GUI程序,取得等级和加权平均值,然后显示最终成绩。该程序是Java。我遇到的问题是试图让ActionListener工作它给我一个错误说{excepted。任何指导帮助都会感激不尽。
package programmingproblem6;
//This is the class file for the program used to create the GUI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame {
private JLabel Score1, Score2, Score3, Score4, Avg1, Avg2, Avg3, Avg4, Fgrade;
private JTextField Testscore1, Testscore2, Testscore3, Testscore4, Weightavg1, Weightavg2, Weightavg3, Weightavg4, Finalgrade;
private JButton Calc, Reset;
public Gui(){
super("Calculate Weighted Test Scores");
setLayout(new FlowLayout());
Score1 = new JLabel("Test Score 1:");
Testscore1 = new JTextField(2);
Avg1 = new JLabel("Weighted Avg 1:");
Weightavg1 = new JTextField(4);
Score2 = new JLabel("Test Score 2:");
Testscore2 = new JTextField(2);
Avg2 = new JLabel("Weighted Avg 2:");
Weightavg2 = new JTextField(4);
Score3 = new JLabel("Test Score 1:");
Testscore3 = new JTextField(2);
Avg3 = new JLabel("Weighted Avg 1:");
Weightavg3 = new JTextField(4);
Score4 = new JLabel("Test Score 1:");
Testscore4 = new JTextField(2);
Avg4 = new JLabel("Weighted Avg 1:");
Weightavg4 = new JTextField(4);
Fgrade = new JLabel("Final Grade:");
Finalgrade = new JTextField(4);
Calc = new JButton("Calculate");
Reset = new JButton("Reset");
getContentPane().add(Score1);
getContentPane().add(Testscore1);
getContentPane().add(Avg1);
getContentPane().add(Weightavg1);
getContentPane().add(Score2);
getContentPane().add(Testscore2);
getContentPane().add(Avg2);
getContentPane().add(Weightavg2);
getContentPane().add(Score3);
getContentPane().add(Testscore3);
getContentPane().add(Avg3);
getContentPane().add(Weightavg3);
getContentPane().add(Score3);
getContentPane().add(Testscore4);
getContentPane().add(Avg3);
getContentPane().add(Weightavg4);
getContentPane().add(Fgrade);
getContentPane().add(Finalgrade);
getContentPane().add(Calc);
getContentPane().add(Reset);
CalculateClass Calculate = new CalculateClass();
Calc.addActionListener(Calculate);
//RestClass Resttxt = new RestClass();
//Reset.addActionListener(Resttxt);
}
private class CalculateClass implements ActionListener(){
public void actionPerformed(ActionEvent event){
//getting the values from the textboxes and converting them to numbers
int num1 = Integer.parseInt(Testscore1.getText());
int num2 = Integer.parseInt(Testscore2.getText());
int num3 = Integer.parseInt(Testscore3.getText());
int num4 = Integer.parseInt(Testscore4.getText());
float num5 = Float.parseFloat(Weightavg1.getText());
float num6 = Float.parseFloat(Weightavg2.getText());
float num7 = Float.parseFloat(Weightavg3.getText());
float num8 = Float.parseFloat(Weightavg4.getText());
float Total;
//performing the calculation for the grade.
Total = num1*num5+num2*num6+num3*num7+num4*num8/100;
//conveting the final number back to a string.
Finalgrade.setText(Float.toString(Total));
}
}
答案 0 :(得分:2)
删除"()"在实现ActionListener之后。
private class CalculateClass implements ActionListener{
}
原因是ActionListener是一个接口,而不是一个方法,并且不存在任何方法体。