2010-10-25 4 views
0

que je fais 2 programmes (ne peut pas avoir des changements majeurs à la mise en page du code), mais j'ai une erreur relative à cette ligne ci-dessous:J'ai un problème en Java relatif à « ActionEvent » et « ActionPerformed »

public void actionPerformed (ActionEvent e) 

Le même problème concerne également l'autre programme. Y a-t-il un moyen de le réparer (sans changer une grande partie du reste, puisque tout le reste doit être quelque peu le même)?

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

Quelle est l'erreur? – Feanor

+0

Bonjour, bienvenue sur stackoverflow.com! Dites-nous quel problème vous avez avec la ligne que vous mentionnez, sinon nous ne pouvons pas vous aider. – sleske

+0

LionsTigersBears.java:70: début illégale d'expression \t publique vide actionPerformed (ActionEvent e) \t ^ LionsTigersBears.java:70: début illégale d'expression \t public void 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

Répondre

2

Vous essayez de définir une méthode dans une autre méthode. C'est une erreur de syntaxe en Java. Déplacez votre méthode actionPerformed en dehors de votre méthode init.

+0

Si je fais cela, alors je reçois 26 autres erreurs (qui ne sont pas vraiment des erreurs). – user473973

+0

@ user473973: Il semble que vous accédiez à la variable 'totalCost' en dehors de la portée où elle est définie dans' actionPerformed'. Je pense que vous voulez dire que c'est une variable de classe, pas locale à 'actionPerformed'. –

Questions connexes