不是抽象的,并且不会覆盖ActionListener中的ActionPerformed(ActionEvent)的抽象方法

发布于 2025-01-19 20:05:34 字数 1987 浏览 0 评论 0原文

有人可以向我解释为什么它一直给我这个错误

错误: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2025-01-26 20:05:34
public void actionPerfomed(ActionEvent e) {

应该是(带有两个字母'r):

public void actionPerformed(ActionEvent e) {

但请始终添加覆盖符号,因此:

@Override
public void actionPerformed(ActionEvent e) {

sidebar re:

public void paint(Graphics g) {
   super.paint(g);

要自定义绘制任何jcomponent正确的方法是:

public void paintComponent(Graphics g) {
   super.paintComponent(g);

当然,也要添加覆盖符号。

public void actionPerfomed(ActionEvent e) {

Should be (with two letter 'r's):

public void actionPerformed(ActionEvent e) {

But always add the override notation, so:

@Override
public void actionPerformed(ActionEvent e) {

Sidebar re:

public void paint(Graphics g) {
   super.paint(g);

To custom paint any JComponent the correct method is:

public void paintComponent(Graphics g) {
   super.paintComponent(g);

Of course, also add the override notation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文