簡體   English   中英

我在Java中遇到與“ActionEvent”和“actionPerformed”有關的問題

[英]I have a Problem in Java relating to “ActionEvent” and “actionPerformed”

我正在做2個程序(不能對代碼的布局進行重大更改),但是下面的行與此有關:

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);
}
}

您正在嘗試在另一個方法中定義方法。 這是Java中的語法錯誤。 將您的actionPerformed方法actionPerformed init方法之外。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM