'xxx' 类型必须实现继承的抽象方法 java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23379081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type 'xxx' must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
提问by user3587633
I am trying to compile my code, but I keep getting a compiling error that I do not know how to fix. The error occurs at the line
我正在尝试编译我的代码,但我不断收到一个我不知道如何修复的编译错误。错误发生在该行
public class MovingSignPanel extends JPanel implements ActionListener {
and the error is
错误是
The type MovingSignPanel must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
if one of you lovely people could help me it would be greatly appreciated.
如果你们中的一位可爱的人能帮助我,我将不胜感激。
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
}
}
回答by John3136
The MovingSignPanel
class needs an actionPerformed(java.awt.event.ActionEvent)
method.
For example:
这个MovingSignPanel
类需要一个actionPerformed(java.awt.event.ActionEvent)
方法。例如:
void actionPerformed(java.awt.event.ActionEvent ev)
{
// your code
}
You need to do this because your class says it's implementing the ActionListener
interface, and actionPerformed()
is part of that interface.
您需要这样做,因为您的类说它正在实现ActionListener
接口,并且actionPerformed()
是该接口的一部分。
回答by vkg
This is what your are missing
这就是你所缺少的
public class Test extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0) {
// Do something here
}
}
Also as a best practice never import packages with com.somepackage.* as you have in your code. If you are using eclipse use organize imports to clean that. It will make it more obvious what your are using and what not. You should also try to replace the big if else in "moveSun" method with a switch statement.
同样作为最佳实践,永远不要像在代码中那样使用 com.somepackage.* 导入包。如果您使用 eclipse,请使用组织导入来清理它。这将使您更清楚地了解您正在使用什么以及不使用什么。您还应该尝试用 switch 语句替换“moveSun”方法中的大 if else。