好吧,我有这个相当简单的代码,尽管它一直给我这个错误消息。我通过谷歌尝试了各种不同的解决方案,但我似乎无法弄清楚。我显示了错误消息发生的位置。
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();
}
}
最佳答案
你有一个错别字
public void actionPreformed(ActionEvent e)
^^
应改为
public void actionPerformed(ActionEvent e)
<小时/>
下次,使用注释@Override
可能会帮助您解决此类问题。
@Override
public void actionPerformed(ActionEvent e)
如果该方法不是正确的重写,则会抛出错误。
关于java - ExitButtonHandler不是抽象的,不会重写ActionListener中的抽象方法actionPerformed(ActionEvent),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36163711/