不是抽象的,并且不会覆盖ActionListener中的ActionPerformed(ActionEvent)的抽象方法
有人可以向我解释为什么它一直给我这个错误
错误:MyPanel不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent) public class MyPanel extends JPanel Implements ActionListener {
我认为我做的一切都是对的,我不知道我做错了什么,这段代码是为了测试使图像水平移动,
这是我的代码
Main.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class Main {
public static void main(String [] args) {
new MyFrame();
}
}
MyFrame。 java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class MyFrame extends JFrame {
MyPanel panel;
MyFrame(){
panel = new MyPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024,720);
this.add(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
MyPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class MyPanel extends JPanel implements ActionListener {
final int PANEL_WIDTH = 1024;
final int PANEL_HEIGHT = 720;
Image pilota;
Image backgroundImage;
Timer timer;
int xVelocity = 1;
int yVelocity = 1;
int x = 0;
int y = 0;
MyPanel() {
this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
this.setBackground(Color.black);
pilota = new ImageIcon("pilota1.png").getImage();
timer = new Timer(10,null);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(pilota, x, y, null);
}
public void actionPerfomed(ActionEvent e) {
x = x + xVelocity;
repaint();
}
}
someone can explain to me why it keeps giving me this error
error: MyPanel is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class MyPanel extends JPanel implements ActionListener {
i think i did everything right i can't figure out what i did wrong, this code was for a test to make an image move horizontally
this is my code
Main.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class Main {
public static void main(String [] args) {
new MyFrame();
}
}
MyFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class MyFrame extends JFrame {
MyPanel panel;
MyFrame(){
panel = new MyPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024,720);
this.add(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
MyPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class MyPanel extends JPanel implements ActionListener {
final int PANEL_WIDTH = 1024;
final int PANEL_HEIGHT = 720;
Image pilota;
Image backgroundImage;
Timer timer;
int xVelocity = 1;
int yVelocity = 1;
int x = 0;
int y = 0;
MyPanel() {
this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
this.setBackground(Color.black);
pilota = new ImageIcon("pilota1.png").getImage();
timer = new Timer(10,null);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(pilota, x, y, null);
}
public void actionPerfomed(ActionEvent e) {
x = x + xVelocity;
repaint();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是(带有两个字母'r):
但请始终添加覆盖符号,因此:
sidebar re:
要自定义绘制任何
jcomponent
正确的方法是:当然,也要添加覆盖符号。
Should be (with two letter 'r's):
But always add the override notation, so:
Sidebar re:
To custom paint any
JComponent
the correct method is:Of course, also add the override notation.