当前位置: 首页 > 知识库问答 >
问题:

ExitButtonHandler不是抽象的,并且不重写ActionListener中的抽象方法actionPerformed(ActionEvent

吕奇
2023-03-14
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();
     }
}

共有1个答案

水铭晨
2023-03-14

你打错了

public void actionPreformed(ActionEvent e)
                   ^^

应更改为

public void actionPerformed(ActionEvent e)

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

@Override
public void actionPerformed(ActionEvent e)
 类似资料: