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;
}
}
}
错误:
BMICalc.java:56:
error: BMICalc.CalcButtonListener is not abstract and does not override
abstract method actionPerformed(ActionEvent) in ActionListener
private class CalcButtonListener implements ActionListener
^
答案 0 :(得分:0)
您的类中的函数应为public void actionPerformed(ActionEvent e)
,以便它定义ActionListener
中概述的抽象方法。
为了更直接地回答你的问题,编译器说类CalcButtonListener
也应该是抽象的,因为它仍然有一个抽象方法来实现ActionListener
。
答案 1 :(得分:0)
抽象方法actionPerformed(ActionEvent)存在于ActionListener接口中。此方法的输入是ActionEvent而不是Action。
您实际上是使用Action而不是ActionEvent e覆盖该方法,因此错误。