1

我正在开发一个简单的 Java GUI,但出现了关于抽象方法的错误。我用注释 ERROR - 等标记了有错误的代码。接口类在底部,它也有一个关于找不到符号的错误。它被标记。

import javax.swing.*;
import java.awt.event.*;

public class KiloConverter extends JFrame {

    private JPanel panel;                   //To reference a panel
    private JLabel messageLabel;            //To reference a label
    private JTextField kiloTextField;       //To reference a text field
    private JButton calcButton;             //To reference a button
    private final int WINDOW_WIDTH = 310;   //Window width
    private final int WINDOW_HEIGHT = 100;  //Window height

    public KiloConverter() {

        setTitle("Kilometer Converter");        //Set the window title
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);    //Set the size of the window

        //Specify what happens when the close button is clicked
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buildPanel();                           //Build panel and add to frame
        add(panel);                             //Add panel to content pane
        setVisible(true);                       //Display the window
    }

    private void buildPanel() {

        messageLabel = new JLabel("Enter a distance in kilometers");
        kiloTextField = new JTextField(10);
        calcButton = new JButton("Calculate");

        //ERROR - method addActionListener in class AbstractButton cannot be   
        //applied to given types
        calcButton.addActionListener(new CalcButtonListener()); 



        panel = new JPanel();

        panel.add(messageLabel);
        panel.add(kiloTextField);
        panel.add(calcButton);
    }

    private class CalcButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String input;
            double miles;

            input = kiloTextField.getText();
            miles = Double.parseDouble(input) * 0.6214;

            JOptionPane.showMessageDialog(null, input + "kilometers is " +
                    miles + " miles.");
        }
    }

    public static void main(String[] args) {
        new KiloConverter();
    }
}

接口类:

import java.awt.event.ActionEvent;

public interface ActionListener {

    public void actionPerformed(ActionEvent e);
}
4

1 回答 1

3
public interface ActionListener {

    public void actionPerformed(ActionEvent e);
}

应该:

import java.awt.event.ActionEvent;

public interface ActionListener {

    //ERROR - Cannot find symbol
    //symbol: class Action Event
    public void actionPerformed(ActionEvent e);
}

但是现在我更仔细地查看您的示例,您不应该声明与现有接口具有完全相同名称的接口!这更符合要求。

import java.awt.event.*;

public abstract class OurActionListener implements ActionListener {

    public abstract void actionPerformed(ActionEvent e);
}
于 2013-05-16T08:02:45.633 回答