簡體   English   中英

JButton的抽象錯誤,無法在接口類上找到符號

[英]Abstract Error with JButton and Cannot Find Symbol on interface class

我正在開發一個簡單的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);
}
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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM