Error: cannot find symbol "super.paintComponent(brush);" being run in non-inner class

Error: cannot find symbol "super.paintComponent(brush);" being run in non-inner class

据我所知,此代码不在内部 class 中,我能找到的唯一相关问题是关于此代码何时 运行 在内部 [=27] =].

相关代码:

public JDemoLocation()
{
   setTitle("Fireworks");
   setLayout(new FlowLayout());
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   add(button);
   button.addActionListener(this);
}

public void paintComponent(Graphics brush)
{
   super.paintComponent(brush);
   if (drawIt)
   {
      x = 450;
      y = 500;
      z = 250;

完整代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Thread;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JDemoLocation extends JFrame implements ActionListener
{
   JButton button = new JButton("Fire!");
   int y = 50;
   int x = 50;
   int z = 500;
   int v = 500;
   boolean drawIt = false;

   final int GAP = 30;

   public JDemoLocation()
   {
      setTitle("Fireworks");
      setLayout(new FlowLayout());
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      add(button);
      button.addActionListener(this);
   }

   public void paintComponent(Graphics brush)
   {
      super.paintComponent(brush);
      if (drawIt)
      {
         x = 450;
         y = 500;
         z = 250;
         v = 500;
         brush.drawLine(z, v, x, y);
         x = 550;
         y = 500;
         z = 750;
         v = 500;
         brush.drawLine(z, v, x, y);
         x = 500;
         y = 550;
         z = 500;
         v = 750;
         brush.drawLine(z, v, x, y);
         x = 500;
         y = 450;
         z = 500;
         v = 250;
         brush.drawLine(z, v, x, y);
         x = 550;
         y = 450;
         z = 750;
         v = 250;
         brush.drawLine(z, v, x, y);
         x = 550;
         y = 550;
         z = 750;
         v = 750;
         brush.drawLine(z, v, x, y);
         x = 450;
         y = 550;
         z = 250;
         v = 750;
         brush.drawLine(z, v, x, y);
         x = 450;
         y = 450;
         z = 250;
         v = 250;
         brush.drawLine(z, v, x, y);
         int xPoints[] = {502, 512, 532, 512, 520, 500, 475, 488, 469, 492, 502};
         int yPoints[] = {468, 492, 498, 510, 535, 515, 532, 505, 488, 490, 468};
         brush.drawPolygon(xPoints, yPoints, xPoints.length);
      }
   }

   public void actionPerformed(ActionEvent e)
   {
      drawIt = true;
      repaint();
   }


public static void main(String[] args)
   {
      JDemoLocation frame = new JDemoLocation();
      frame.setSize(1000, 1000);
      frame.setVisible(true);
   }
}

假设我认为是正确的,那么带有 paintComponent 的部分不在内部 class 或匿名 class 中,并且不需要 "qualifier",这不需要也为我工作...

编辑

好的,我明白了,谢谢。

您的基础 class、JFrame 没有名为 paintComponent() 的方法。

JFrame 不是 JComponent,它没有可以覆盖的 paintComponent 方法。相反,您可以扩展 JPanel 并将其添加到框架中。

请参阅此答案 以获取示例代码