2015-05-13 3 views
0
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.CalcButtonListener не является абстрактным

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

'' public void actionPerformed (Action e) '' должно быть '' public void actionPerformed (ActionEvent e) '' –

ответ

0

Функция внутри вашего класса должен быть public void actionPerformed(ActionEvent e) так, что он определяет абстрактный метод, изложенный в ActionListener.

Чтобы более точно ответить на ваш вопрос, компилятор говорит, что класс CalcButtonListener также должен быть абстрактным, поскольку он по-прежнему имеет абстрактный метод реализации ActionListener.

+0

>.

0

Метод абстрактного методаПерформированный (ActionEvent) присутствует в интерфейсе ActionListener. Ввод этого метода - ActionEvent вместо Action.

Вы фактически переопределяете метод Action вместо ActionEvent e и, следовательно, ошибку.

Смежные вопросы