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

时间:2014-01-04 04:25:50

标签: java swing compiler-errors

此代码有什么问题?: 当我声明 私有类Listener实现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

3 个答案:

答案 0 :(得分:5)

您尚未为ActionListener界面实现所需的方法。

在该课程的某个地方,请填写:

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

你有action(ActionEvent)方法,但拼写错误。

答案 1 :(得分:3)

您尚未实施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

        }
    }
}

答案 2 :(得分:1)

在您的代码中将操作更改为 actionPerformed ,因为界面ActionListener的方法actionPerformed(..)不是action(..)

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