Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton错误无法应用于给定类型_Java_Swing_Jbutton_Actionlistener - Fatal编程技术网

Java JButton错误无法应用于给定类型

Java JButton错误无法应用于给定类型,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,我正在开发一个简单的Java GUI,但是当我应用它时,JButton的方法addActionListener出现了一个错误。这是我的密码。错误标记为错误 import javax.swing.*; import java.awt.event.*; public class KiloConverter extends JFrame { private JPanel panel; //To reference a panel private

我正在开发一个简单的Java GUI,但是当我应用它时,
JButton
的方法
addActionListener
出现了一个错误。这是我的密码。错误标记为
错误

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
        //reason: actual argument KiloConverter.CalcButtonListener cannot be 
        //converted to ActionListener by method invocation conversion.
        calcButton.addActionListener(new CalcButtonListener()); 



        panel = new JPanel();

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

    private class CalcButtonListener implements ActionListener {
        @Override
        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);
}

根据您的评论,您已经创建了自己的
接口
,名为
ActionListener

JButton
需要
java.awt.ActionListener
的实例


尝试删除您的实现,并在您自己的侦听器中实现
java.awt.ActionListener

您是否重新实现了
ActionListener
接口
JButton
需要一个
java.awt.ActionListener
private final int WINDOW_WIDTH=310不设置顶级容器的大小。而是布局内容&调用
pack()
。重新实现ActionListener界面是什么意思@AndrewThompson哦,好吧,我会记住这一点。提示:添加@MadProgrammer(或任何人)以通知他们新的评论。当然,每个评论只能通知一个人。评论应该在此基础上分开。@mad程序员哇,这就解决了所有问题!谢谢你想提交答案让我接受吗?