java - IDE 将 Listener 标记为红色并表示它不是抽象的

标签 java swing compiler-errors

这段代码有什么问题?:
当我声明 私有(private)类监听器实现 ActionListener IDE 仅将 Listener 标记为红色,并表示它不是抽象的

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

public class hello{

    private JFrame mainFrame;
    private JLabel title;
    private JPanel mainPanel;

    public hello(){
        prepareGUI();
    }

    public static void main(String[] args){
        hello helloo = new hello();
        helloo.Event();
    }

    private void prepareGUI(){
        mainFrame = new JFrame("This is a test project");
        mainFrame.setSize(500, 500);
        mainFrame.setLayout(new GridLayout(3,1));

        mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());

        title = new JLabel("",JLabel.CENTER);

        mainFrame.add(mainPanel);
        mainFrame.add(title);
        mainFrame.setVisible(true);
    }

    private void Event(){
        JButton button1 = new JButton("Test");
        button1.setSize(15,10);
        button1.setActionCommand("Test");
        button1.addActionListener(new Listener());

        mainPanel.add(button1);

        mainFrame.setVisible(true);
    }

    private class Listener implements ActionListener{
        public void action(ActionEvent e){
            String com = e.getActionCommand();
            if(com.equals("Test")){
                title.setText("button clicked");
            }
        }
    }
}

我是 JAVA 的新手,昨天开始,所以任何建议都会有所帮助
附言我正在使用 NetBeans IDE 7.4

最佳答案

您还没有实现 ActionListener 所需的方法界面。

在那个类(class)的某个地方,填写:

@Override
public void actionPerformed(ActionEvent e){
    // Fill
}

您有一个 action(ActionEvent)方法,但拼写错误。

关于java - IDE 将 Listener 标记为红色并表示它不是抽象的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916867/

相关文章:

java - 与 JAX-RS 混淆,Jersey 与 JAX-RS 混淆

java - 如何用eclipse配置hadoop

java - Java 对 String 的内部表示是什么?修改过的UTF-8? UTF-16?

java - 使用容器框架使 javafx 场景拉伸(stretch)

java - JFrame 中的 JFileChooser 使 setVisible() 卡住

java - 如何配置事务管理以在 Spring 中使用 2 个不同的数据库?

java - GridBagLayout - 一行的高度导致下一行的宽度发生变化

java - 错误: Type mismatch: cannot convert from List<Integer> to ArrayList<Integer>

matlab - 为什么我的代码无法在墨西哥构建,但可以与cl一起使用?

c++ - Qt5 : Strange compilation error while using QCommandLineParser class