本文介绍了super.paintComponent问题(g)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码片段:

  protected void paintComponent(final Graphics g){

Runnable r = new Runnable(){

@Override

public void run(){
while(true){
super.paintComponent(g) ; //< -----错误行
g.setColor(Color.red);
g.drawOval(x,y,width,height);
g.fillOval(x,y,width,height);
x ++;
y ++;
width ++;
height ++;
if(width == 20)
break;
尝试{
Thread.sleep(100);
} catch(Exception exc){
System.out.println(exc);
}
}
}
};
主题moveIt =新主题(r);
moveIt.start();
}

编译完整代码时产生以下错误:

  d:\UnderTest> javac mainClass.java 
mainClass.java:18:找不到符号
super.paintComponent (G);

symbol:方法paintComponent(Graphics)
location:class Object
1错误



为什么我得到这个错误?



如果这是我的完整代码:

  import java.awt。*; 
import javax.swing。*;
import java.lang.Thread;

类moveObjects扩展了JPanel {
int x = 2,y = 2,width = 10,height = 10;

@Override

protected void paintComponent(final Graphics g){

Runnable r = new Runnable(){

@Override

public void run(){
while(true){
super.paintComponent(g);
g.setColor(Color.red);
g.drawOval(x,y,width,height);
g.fillOval(x,y,width,height);
x ++;
y ++;
width ++;
height ++;
if(width == 20)
break;
尝试{
Thread.sleep(100);
} catch(Exception exc){
System.out.println(exc);
}
}
}
};
主题moveIt =新主题(r);
moveIt.start();



class mainClass {

mainClass(){
buildGUI();
}

public void buildGUI(){
JFrame fr = new JFrame(Moving Objects);
movingObjects mO = new moveObjects();
fr.add(mO);
fr.setVisible(true);
fr.setSize(400,400);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


public static void main(String args []){
new mainClass();



$ div $解析方案

你应该使用Qualified Super。

  moveObjects.super.paintComponent(g); 

因为当您使用这个 super 内部类(在这种情况下: Runnable ),您将获得内部类。如果您想使用内部类的外部类,请使用或合格的超级。

  YourOuterClassName.this 
YourOuterClassName.super

Qualified Super是我无法在JLS中找到的术语,我自己发明的。 p>

This is the snippet :

protected void paintComponent(final Graphics g) {

 Runnable r=new Runnable() {

 @Override

  public void run() {
   while(true) {
     super.paintComponent(g);  // <----- line of error
     g.setColor(Color.red);
     g.drawOval(x,y,width,height);
     g.fillOval(x,y,width,height);
     x++;
     y++;
     width++;
     height++;
       if(width==20)
          break;
     try {
        Thread.sleep(100);
     } catch(Exception exc) {
         System.out.println(exc);
       }
   }
  }
};
 Thread moveIt=new Thread(r);
 moveIt.start();
}

The following error is produced when i compile the complete code :

d:\UnderTest>javac mainClass.java
mainClass.java:18: cannot find symbol
     super.paintComponent(g);
          ^
symbol:   method paintComponent(Graphics)
location: class Object
1 error

Why do i get this error?

In case this is my complete code :

import java.awt.*;
import javax.swing.*;
import java.lang.Thread;

class movingObjects extends JPanel {
int x=2,y=2,width=10,height=10;

@Override

protected void paintComponent(final Graphics g) {

Runnable r=new Runnable() {

 @Override

  public void run() {
   while(true) {
     super.paintComponent(g);
     g.setColor(Color.red);
     g.drawOval(x,y,width,height);
     g.fillOval(x,y,width,height);
     x++;
     y++;
     width++;
     height++;
       if(width==20)
          break;
     try {
        Thread.sleep(100);
     } catch(Exception exc) {
         System.out.println(exc);
       }
   }
  }
 };
   Thread moveIt=new Thread(r);
    moveIt.start();
    }
   }

class mainClass {

 mainClass() {
 buildGUI();
}

 public void buildGUI() {
 JFrame fr=new JFrame("Moving Objects");
 movingObjects mO=new movingObjects();
 fr.add(mO);
 fr.setVisible(true);
 fr.setSize(400,400); 
 fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

 public static void main(String args[]) {
  new mainClass();
  }
 }  
解决方案

You should use a Qualified Super.

movingObjects.super.paintComponent(g);

Because, when you use this or super inside an inner class (in this case: Runnable), you will get the innerclass. If you want to use the outer class from the inner class, use a Qualified This or a Qualified Super.

YourOuterClassName.this
YourOuterClassName.super

Qualified Super is a term I can't find in the JLS, I invented it by myself.

这篇关于super.paintComponent问题(g)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:06