【问题标题】:ExitButtonHandler is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListenerExitButtonHandler 不是抽象的,不会覆盖 ActionListener 中的抽象方法 actionPerformed(ActionEvent)
【发布时间】:2016-03-22 19:42:26
【问题描述】:

好的,我有这个相当简单的代码,尽管它一直给我这个错误消息。我通过谷歌尝试了各种不同的解决方案,但我似乎无法弄清楚。我显示错误消息发生的位置。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 
import java.lang.*;

public class Project4 extends JFrame
{


    private JLabel InCDAmount, YearTMature, CDInRate, EndBalance;

    private JTextField InCDAmountTF, YearTMatureTF, CDInRateTF, EndBalanceTF;

    private JButton calculateB, exitB;

    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    private static int WIDTH = 400;
    private static int HEIGHT = 300;

    public Project4()
    {
    InCDAmount = new JLabel("Initial CD Amount", SwingConstants.LEFT);

    YearTMature = new JLabel("Years to Maturity", SwingConstants.LEFT);

    CDInRate = new JLabel("CD Intrest Rate", SwingConstants.LEFT);

    EndBalance = new JLabel("Ending Balance", SwingConstants.LEFT);


    InCDAmountTF = new JTextField(10);
    YearTMatureTF = new JTextField(10);
    CDInRateTF = new JTextField(10);

    calculateB = new JButton ("Calculate");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);

    exitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);

    setTitle("ACME Bank Certificate Of Deposit Calculator");

    Container pane = getContentPane();

    pane.setLayout(new GridLayout(5, 2));

    pane.add(InCDAmount);
    pane.add(InCDAmountTF);
    pane.add(YearTMature);
    pane.add(YearTMatureTF);
    pane.add(CDInRate);
    pane.add(CDInRateTF);
    pane.add(EndBalance);
    pane.add(EndBalanceTF);

    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  private class CalculateButtonHandler implements ActionListener
  {
     public void actionPerformed(ActionEvent e)
     {
     double InCDAmount, YearTMature, CDInRate, EndBalance;

     InCDAmount = Double.parseDouble(InCDAmountTF.getText());
     YearTMature = Double.parseDouble(YearTMatureTF.getText());
     CDInRate = Double.parseDouble(CDInRateTF.getText());
     EndBalance = InCDAmount + YearTMature + CDInRate;

     EndBalanceTF.setText("" + EndBalance);
     }
  }

这就是我遇到问题的地方。

  private class ExitButtonHandler implements ActionListener

它给了我错误信息

ExitButtonHandler 不是抽象的,不会覆盖 ActionListener 中的抽象方法 actionPerformed(ActionEvent)

private class ExitButtonHandler implements ActionListener
{
     public void actionPreformed(ActionEvent e)
     {
        System.exit(0);
     }

     public static void main(String[] args)
     {
        Project4 rectObject = new Project4();
     }
}

【问题讨论】:

  • 错误信息看起来很清楚。你没有超越 actionPerformed

标签: java


【解决方案1】:

你有错别字

public void actionPreformed(ActionEvent e)
                   ^^

应该改为

public void actionPerformed(ActionEvent e)

下次,使用注解@Override 可能会帮助您解决这类问题。

@Override
public void actionPerformed(ActionEvent e)

如果方法不是正确的覆盖,它会抛出一个错误。

【讨论】:

  • 非常感谢。现在工作正常!
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2014-01-16
相关资源
最近更新 更多