简体   繁体   English

我在Java中遇到与“ActionEvent”和“actionPerformed”有关的问题

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

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: 我正在做2个程序(不能对代码的布局进行重大更改),但是下面的行与此有关:

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 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. 这是Java中的语法错误。 Move your actionPerformed method outside your init method. 将您的actionPerformed方法actionPerformed init方法之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于ActionPerformed和ActionEvent - About ActionPerformed and ActionEvent actionPerformed(ActionEvent e){ - actionPerformed(ActionEvent e) { 我的 actionPerformed 方法(Java)不起作用,我不知道为什么 - My actionPerformed method(Java) is not working and I have no clue why jTable中的文本字段比ActionPerformed(java.awt.event.ActionEvent evt)更好! - text field in jTable something better than ActionPerformed(java.awt.event.ActionEvent evt)! Java错误:类'Anonymous'必须被声明为abstract或在'ActionListener'中实现抽象方法'actionPerformed(ActionEvent)' - Java Error: Class 'Anonymous' must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener' 在actionPerformed(ActionEvent event)方法中抛出IOException - Throw IOException in actionPerformed(ActionEvent event) method 必须实现 ActionListener.actionPerformed( ActionEvent ) - Must implement ActionListener.actionPerformed( ActionEvent ) Java中的ActionEvent - ActionEvent in Java java:按钮不是抽象的,并且不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent) - java: Button is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener “必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)”是什么意思? - what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM