Java IDE将侦听器标记为红色,并表示它不是抽象的

Java IDE将侦听器标记为红色,并表示它不是抽象的,java,swing,compiler-errors,Java,Swing,Compiler Errors,此代码中有什么错误?: 当我声明私有类侦听器实现ActionListener时,IDE将just Listener标记为红色,并表示它不是抽象的 package hello; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class hello{ private JFrame mainFrame; private JLabel title; private

此代码中有什么错误?: 当我声明私有类侦听器实现ActionListener时,IDE将just 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
接口实现所需的方法

在该类中的某个位置,填入:

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

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

您没有实现
actionPerformed()
方法

这样做

@Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }
在实现某个接口时,需要覆盖所有方法

添加所需的代码后,它将如下所示

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");
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) { // this method you need to ovverride
            // TODO Auto-generated method stub

        }
    }
}

在您的代码中将操作更改为执行的操作,因为接口
ActionListener
具有方法
actionPerformed(…)
而不是
操作(…)


我很确定这不是全部的错误信息。您可以从阅读整个错误并理解它所说的内容开始。请切换到使用真正的IDE,它将自动为您解决此问题。
private class Listener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
         String com = e.getActionCommand();
            if(com.equals("Test")){
                title.setText("button clicked");
            }
    }
    }