簡體   English   中英

不能使用 ActionListener

[英]Cannot use ActionListener

我一直試圖找出這段代碼的問題,每當我使用 ActionListener 為 convertbutton 創建一個事件時,它都會向我顯示此錯誤:(我從我的教科書上寫了這段代碼,它適用於我的大多數同事,但我不知道為什么它對我不起作用)

    error: cannot find symbol
        private class ConvertButtonListener implements ActionListener {
      symbol:   class ActionListener
      location: class KiloConverter
    java:37: error: cannot find symbol
            public void actionPerformed(ActionEvent e){
      symbol:   class ActionEvent
      location: class KiloConverter.ConvertButtonListener
    cannot be converted to ActionListener
            convertButton.addActionListener(new ConvertButtonListener());
    Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
    3 errors
   The following error occurred while executing this line:
    Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 1 second)

這是我的代碼:

我的框架:

package myframe;
import javax.swing.*;

public class MyFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyFrame");
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setTitle("My First Frame");
        
        
        
    }
    
}

千元轉換器:

package myframe;
import java.awt.*;
import javax.swing.*;

public class KiloConverter extends JFrame {
    private JPanel panel; 
    private JButton convertButton;
    private JLabel Label;
    private JTextField kiloTextField;
    final int WINDOW_WIDTH = 400;
    final int WINDOW_HEIGHT = 150;
    
    //Constructor
    public KiloConverter() {
        setTitle("Kilometer Converter");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setLocationRelativeTo(null);
        setVisible(true);
        
    }
    
    private void buildPanel(){
        panel = new JPanel();
        Label = new JLabel("Enter the distance in kilo");
        kiloTextField = new JTextField(10);
        convertButton = new JButton("Convert");
        convertButton.addActionListener(new ConvertButtonListener());
        panel.add(Label);
        panel.add(kiloTextField);
        panel.add(convertButton);
    }
    
    private class ConvertButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e){
            final double conversion = 0.6214;
            String input;
            double miles;

            input = kiloTextField.getText();
            miles = Double.parseDouble(input) * conversion;
            JOptionPane.showMessageDialog(null, input+" Kilometer is equal to " + miles +" in miles.");
        }
    }
    

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

我認為這是因為您需要使用正確的 ActionEvent 導入。

導入java.awt.event.ActionListener

應該這樣做。

祝你今天過得愉快!

:D

答案可能是因為導入錯誤,或者因為你沒有這樣做。

在行中:

convertButton.addActionListener(new ConvertButtonListener());

它不會將 ActionListener 轉換為“new ConvertButtonListener()”

它應該是這樣的:

convertButton.addActionListener((ActionListener) new ConvertButtonListener());

嘗試一下,看看問題是否解決,如果沒有。 它絕對是進口的。

我用我的機器復制了代碼並且工作得很好。 所以如果我剛才說的答案不起作用。 必須是進口的。

祝你今天過得愉快!

如果您有更多問題,請隨時提問!

暫無
暫無

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

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