Java 为什么我会犯这个错误?BMICalc.CalcButtonListener不是抽象的
错误:Java 为什么我会犯这个错误?BMICalc.CalcButtonListener不是抽象的,java,Java,错误: import javax.swing.*; import java.awt.*; import java.text.DecimalFormat; import java.awt.event.*; public class BMICalc extends JApplet { private final String PROGRAM_NAME = "BMI Calculator"; JTextField weightText = new JTextField(3); JT
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.awt.event.*;
public class BMICalc extends JApplet
{
private final String PROGRAM_NAME = "BMI Calculator";
JTextField weightText = new JTextField(3);
JTextField ftHeightText = new JTextField(3);
JTextField inHeightText = new JTextField(3);
DecimalFormat bmiFmt = new DecimalFormat("##0.0");
public void init()
{
JLabel label;
JPanel panel;
setLayout(new GridLayout(4, 1));
//Heading cell
panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
label = new JLabel(PROGRAM_NAME);
label.setFont(new Font("Verdana", Font.BOLD, 18));
panel.add(label);
add(panel);
//Weight cell
panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.add(new JLabel("Weight: "));
panel.add(weightText);
panel.add(new JLabel("pounds"));
add(panel);
//Button cell
panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton calculateButton = new JButton("Calculate");
panel.add(calculateButton);
add(panel);
calculateButton.addActionListener(new CalcButtonListener());
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(Action e)
{
double weight;
double feet;
double inches;
double bmi;
String result;
//Weight error message
try
{
weight = Double.parseDouble(weightText.getText());
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(BMICalc.this,
"Weight must be a number in pounds.",
PROGRAM_NAME,
JOptionPane.ERROR_MESSAGE);
return;
}
//Height error message
try
{
feet = Double.parseDouble(ftHeightText.getText());
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(BMICalc.this,
"Height must be a number in feet.",
PROGRAM_NAME,
JOptionPane.ERROR_MESSAGE);
return;
}
//Height error message
try
{
inches = Double.parseDouble(inHeightText.getText());
}
catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(BMICalc.this,
"Height must be a number in inches.",
PROGRAM_NAME,
JOptionPane.ERROR_MESSAGE);
return;
}
}
}
类中的函数应该是
public void actionPerformed(ActionEvent e)
,以便定义ActionListener
中概述的抽象方法
为了更直接地回答您的问题,编译器说类
CalcButtonListener
也应该是抽象的,因为它仍然有一个来自实现抽象方法actionPerformed(ActionEvent)的抽象方法存在于ActionListener接口中。此方法的输入是ActionEvent而不是Action
实际上,您正在用Action而不是ActionEvent e覆盖该方法,因此出现了错误。
public void actionPerformed(Action e)
应该是public void actionPerformed(ActionEvent e)
>。谢谢您。你不知道我看过多少次代码…自言自语…哈哈
BMICalc.java:56:
error: BMICalc.CalcButtonListener is not abstract and does not override
abstract method actionPerformed(ActionEvent) in ActionListener
private class CalcButtonListener implements ActionListener
^