import java.awt.*;
import java.awt.event.*;
public class test4 extends Frame implements WindowListener{
  public static test4 fr;
  public static void main(String args[]){
   fr=new test4("hello the first frame!");
   Button b1=new Button("press me");
   Button b2=new Button("dont't press me");
   fr.add("North",b1);
   fr.add("South",b2);
   fr.pack();
   fr.addWindowListener(fr);
   fr.setSize(200,300);
   fr.setBackground(Color.red);
   fr.setForeground(Color.blue);
   fr.setVisible(true);
   fr.paint(fr.getGraphics());
  }
  public test4(String str){
   super(str);
  }
  public void paint(Graphics g){
    super.paint(g);
   g.drawString("12312312312312",100,100);
   g.drawString("5555555555555555",150,150);
  }
  public void windowClosing(WindowEvent e){
   System.exit(1);
  }
 public  void windowActivated(WindowEvent e) {}         
 public void windowClosed(WindowEvent e) {}
 public void windowDeactivated(WindowEvent e) {}        
 public void windowDeiconified(WindowEvent e) {}         
 public void windowIconified(WindowEvent e) {}      
 public void windowOpened(WindowEvent e) {}
 }其中
public void paint(Graphics g){
    super.paint(g);
   g.drawString("12312312312312",100,100);
   g.drawString("5555555555555555",150,150);
  }
这段代码的super.paint(g)好像有没有没什么关系啊,请问这个例子用这条语句的目的何在
虽然调用了父类的paint()方法,但好像什么事情都没有做

解决方案 »

  1.   

    因为Frame的paint()方法中本来就什么都不做.如果你是继承自一个Frame类的孙类, 而父类在paint()方法中执行了一些代码, 那么调用super.paint()就有意义了.
      

  2.   

    Frame是从Container类继承来的,Container类的paint()方法的文档是这么写的:Paints the container. This forwards the paint to any lightweight components that are children of this container. If this method is reimplemented, super.paint(g) should be called so that lightweight components are properly rendered. If a child component is entirely clipped by the current clipping setting in g, paint() will not be forwarded to that child. 你派生的Frame里如果加入了轻量组件,比如JButton或者JLabel,在覆盖paint()方法的时候就应该调用super.paint(),否则轻量组件就不能正常显示出来了。
      

  3.   

    super.paint()可以刷新背景色,否则背景色是透明的,里面的子组件不可见。