2010-10-25 44 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