2010-10-25 42 views
0

我在做2個程序(不能對代碼的佈局進行重大更改),但是我有以下錯誤:我在Java中有一個與「ActionEvent」和「actionPerformed」有關的問題

public void actionPerformed (ActionEvent e) 

同樣的問題也適用於其他程序。有沒有一種方法可以解決這個問題(其餘部分沒有改變,因爲所有其他的必須有些相同)?

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*; 
import java.text.DecimalFormat; 

public class LionTigersBears extends Applet implements ActionListener 
{ 
    //declare variables and color 
    double dollars, answer; 
    int servicesCode; 

    Color darkRed = new Color(160, 50, 0); 

    //Create components for applet 
    Label promptLabel = new Label("Welcome to Lions, Tigers, and Bears Pet Clinic"); 
     TextField currencyField = new TextField(20); 

    Label outputLabel = new Label ("Choose your services and then press Calculate."); 
    Button calculateButton = new Button ("Calculate"); 

     Checkbox annualvisitBox = new Checkbox("Annual Visit",false); 
     Checkbox vaccinationsBox = new Checkbox("Vaccinations",false); 
     Checkbox hospitalizationsBox = new Checkbox("Hospitalizations",false); 
     Checkbox heartwormBox = new Checkbox("Heartworm",true); 
     Checkbox boardingBox = new Checkbox("Boarding",false); 
     Checkbox dentistryBox = new Checkbox("Dentistry",false); 
     Checkbox labworkBox = new Checkbox("Lab Work",false); 
     Checkbox prescriptionsBox = new Checkbox("Prescriptions",false); 
     Checkbox hiddenBox = new Checkbox("",true); 


    public void init() 
    { 
     setBackground(darkRed); 
     setForeground(Color.white); 
     add(promptLabel); 

     add(annualvisitBox); 
     annualvisitBox.addItemListener(this); 
     add(vaccinationsBox); 
     vaccinationsBox.addItemListener(this); 
     add(hospitalizationsBox); 
     hospitalizationsBox.addItemListener(this); 
     add(heartwormBox); 
     heartwormBox.addItemListener(this); 
     add(boardingBox); 
     boardingBox.addItemListener(this); 
     add(dentistryBox); 
     dentistryBox.addItemListener(this); 
     add(labworkBox); 
     labworkBox.addItemListener(this); 
     add(prescriptionsBox); 
     prescriptionsBox.addItemListener(this); 
     add(calculateButton); 
     calculateButton.addActionListener(this); 
     add(outputLabel); 

     try 
     { 
      totalCost = gettotalCost(); 
      serviceCode = getCode(); 
      answer = gettotalCost(); 
      output(answer,dollars); 
     } 

     catch (NumberFormatException e) 
     { 
     } 

    public void actionPerformed (ActionEvent e) 
    { 
     double totalCost = 0; 

     if (annualvisitBox.getState()) totalCost = totalCost + 20; 
     if (vaccinationsBox.getState()) totalCost = totalCost + 20; 
     if (hospitalizationsBox.getState()) totalCost = totalCost + 20; 
     if (annualvisitBox.getState()) totalCost = totalCost + 20; 
     if (heartwormBox.getState()) totalCost = totalCost + 20; 
     if (boardingBox.getState()) totalCost = totalCost + 20; 
     if (dentistryBox.getState()) totalCost = totalCost+ 20; 
     if (labworkBox.getState()) totalCost = totalCost + 20; 
     if (prescriptionsBox.getState()) totalCost = totalCost + 20; 
    } 
    return code; 

    outputLabel.setText("The total cost is "+ totalCost); 
    annualvisitBox.setState(false); 
    vaccinationsBox.setState(false); 
    hospitalizationsBox.setState(false); 
    heartwormBox.setState(false); 
    boardingBox.setState(false); 
    dentistryBox.setState(false); 
    labwork.setState(false); 
    prescriptions.setState(false); 
} 
} 
+0

什麼是錯誤? – Feanor 2010-10-25 22:20:51

+0

嗨,歡迎來到stackoverflow.com!請告訴我們您提及的行有哪些問題,否則我們無法幫助您。 – sleske 2010-10-25 22:21:19

+0

LionsTigersBears.java:70:表達 \t公共無效的actionPerformed(ActionEvent的E) \t^ LionsTigersBears.java:70的非法啓動:表達的非法啓動 \t公共無效的actionPerformed(ActionEvent的E) \t^ LionsTigersBears.java :70:';'預計 \t public void actionPerformed(ActionEvent e) \t^ LionsTigersBears.java:70:';'預計 \t public void actionPerformed(ActionEvent e) \t^ – user473973 2010-10-25 22:23:52

回答

2

您試圖在另一個方法中定義一個方法。這是Java中的語法錯誤。在您的init方法之外移動actionPerformed方法。

+0

如果我這樣做,那麼我會得到26多個錯誤(根本不是真正的錯誤)。 – user473973 2010-10-25 22:21:37

+0

@ user473973:它看起來像在'actionPerformed'中定義的範圍之外訪問'totalCost'變量。我認爲你的意思是說它是一個類變量,不是'actionPerformed'的本地變量。 – 2010-10-25 22:24:32