类型';xxx和x27;必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
我试图编译我的代码,但我不断得到一个编译错误,我不知道如何修复。错误发生在该行类型';xxx和x27;必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent),java,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.Ac
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
}
}
MovingSignPanel
类需要一个actionPerformed(java.awt.event.ActionEvent)
方法。
例如:
void actionPerformed(java.awt.event.ActionEvent ev)
{
// your code
}
您需要这样做,因为您的类说它正在实现
ActionListener
接口,而actionPerformed()
是该接口的一部分。这就是您缺少的
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,请使用OrganizeImports来清理它。这会让你更清楚你在用什么,不在用什么。
您还应该尝试用switch语句替换“moveSun”方法中的big if else。错误不言而喻:您必须实现
actionPerformed(java.awt.event.ActionEvent)
methodMovingSignPanel
实现ActionListener
接口。您需要在class MovingSignPanel
中声明和定义方法void actionPerformed(ActionEvent)
,您需要更多地了解Java OOP。