2016-03-25 70 views
1

我正在努力爲學校完成一份遲到的作業。這項任務是構建一個GUI程序,該程序需要分數和加權平均值,然後顯示最終分數。該程序是用Java編寫的。我遇到的問題是試圖讓ActionListener工作,它給我一個錯誤說{例外。任何指導的幫助將是感恩。試圖讓ActionListener工作出現錯誤

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

您可能想看看[Interfaces and Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/index.html)和[如何編寫Action Listeners](http ://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer

+0

我會看看這些鏈接,並閱讀它們。 –

回答

2

實現ActionListener後刪除「()」。

private class CalculateClass implements ActionListener{ 

} 

原因是ActionListener是一個接口,而不是一個方法,沒有方法的人應該存在。

+0

謝謝,它得到了它的工作。 –

+0

StackOverFlow的方式來說聲謝謝是通過接受答案^^不客氣。 – TheMirrox