类型'xxx'必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)

时间:2014-04-30 03:19:36

标签: java

我正在尝试编译我的代码,但我不断收到编译错误,我不知道如何修复。错误发生在

public class MovingSignPanel extends JPanel implements ActionListener {

,错误是

The type MovingSignPanel must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)

如果你们中的一个可爱的人能帮助我,我将不胜感激。

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

public class MovingSignPanel extends JPanel implements ActionListener {

    JButton move = new JButton("Move");
    JButton quit = new JButton("Quit");//create buttons 
    private int press;
    private int x;
    private int y;
    private String time;

    public void CityPanel() {
        press = 0;
        this.add(move);
        move.addActionListener(this);
        this.add(quit);
        quit.addActionListener(this);
        x = 5;
        y = 175;//will be starting place for sun
        time = "8 AM";//will be starting time
    }

    public void moveSun(ActionEvent e) {
        if (e.getSource() == move) {
            {
                press = 0;
            }
            x = 5;
            y = 175;
            time = "8 AM";
            super.repaint();
        } else if (press == 1) {
            x = 100;
            y = 75;
            time = "10 AM";
            super.repaint();
        } else if (press == 2) {
            x = 250;
            y = 80;
            time = "12 PM";
            super.repaint();
        } else if (press == 3) {
            x = 350;
            y = 140;
            time = "2 pm";
            super.repaint();
        } else if (press == 4) {
            x = 415;
            y = 200;
            time = "4 PM";
            super.repaint();
        } else if (press == 5) {
            x = 465;
            y = 230;
            time = "6 PM";
            super.repaint();
        } else if (press == 6) {
            x = 500;
            y = 260;
            time = "8 PM";
            super.repaint();
        }
        press += 1;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);//color for buildings
        g.drawRect(150, 275, 50, 80);
        g.drawRect(200, 250, 50, 100);//draw buildings
        g.drawRect(300, 300, 30, 60);
        g.drawRect(100, 275, 40, 40);
        g.drawLine(0, 350, 500, 350);
        g.drawString(time, x, y + 75);//gives time
        g.setColor(Color.yellow);//color for sun
        g.fillOval(x, y, 50, 50);//draws sun
    }
}

2 个答案:

答案 0 :(得分:2)

MovingSignPanel类需要actionPerformed(java.awt.event.ActionEvent)方法。 例如:

void actionPerformed(java.awt.event.ActionEvent ev)
{
    // your code
}

您需要这样做,因为您的课程表示它正在实施ActionListener界面,而actionPerformed()是该界面的一部分。

答案 1 :(得分:2)

这就是你所缺少的

public class Test extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // Do something here

    }
}

另外,最佳做法是永远不要像在代码中那样使用com.somepackage。*导入包。如果您正在使用eclipse,请使用组织导入来清理它。它会让你更明显地使用什么和不使用什么。 您还应该尝试更换" moveSun"带有switch语句的方法。