我在做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);
}
}
什麼是錯誤? – Feanor 2010-10-25 22:20:51
嗨,歡迎來到stackoverflow.com!請告訴我們您提及的行有哪些問題,否則我們無法幫助您。 – sleske 2010-10-25 22:21:19
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