当前位置: 首页 > 知识库问答 >
问题:

非抽象且不重写ActionListener中的抽象方法actionPerformed(ActionEvent)

萧晓博
2023-03-14

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

错误:MyPanel不是抽象的,并且不重写ActionListener公共类MyPanel extends JPanel实现ActionListener中的抽象方法actionPerformed(ActionEvent){

我想我做的一切都是对的,我不知道我做错了什么,这段代码用于测试使图像水平移动

这是我的密码

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();
            
            }
       }

我的框架。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);
      }

}

我的小组。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();
   }

}

共有1个答案

赵光赫
2023-03-14
public void actionPerfomed(ActionEvent e) {

应为(带两个字母“r”:

public void actionPerformed(ActionEvent e) {

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

@Override
public void actionPerformed(ActionEvent e) {

侧边栏重新:

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

自定义绘制任何组件,正确的方法是:

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

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

 类似资料: