I am doing 2 programs (cannot have major changes to the layout of the code), but I have an error relating to this line below:
public void actionPerformed (ActionEvent e)
The same problem is for the other program too. Is there a way to fix it (without changing much of the rest, since all of the rest has to be somewhat the same)?
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 开发者_StackOverflow社区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);
}
}
You're trying to define a method within another method. That's a syntax error in Java. Move your actionPerformed
method outside your init
method.
精彩评论