其实repaint()方法,好像是这样的,如果有paint()方法则调用paint()方法,没有paint()方法
而有paintComponent()方法则调用paintComponent()方法,下面代码就是为了测试。你可以测试有Paint()方法,和注释掉
paint()方法两种情况。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics2D;
public class TestPanel extends JFrame {
  TTT panel = new TTT();
  JPanel pnlCtl = new JPanel();
  JButton button = new JButton("Draw line");
  JButton button2 = new JButton("Clear all");
  public TestPanel() {
    Container cp = this.getContentPane();
    cp.add(panel,BorderLayout.CENTER);
    button.addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent me)
      {
        Graphics2D g2 = (Graphics2D) panel.getGraphics();
        g2.drawRect(20,20,100,300);
        panel.repaint();
        
      }
    });
    button2.addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent me)
      {
        panel.getGraphics().clearRect(0,0,700,400);
      }
    });
    pnlCtl.add(button);
    pnlCtl.add(button2);
    cp.add(pnlCtl,BorderLayout.NORTH);
    setSize(800,600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.show();
  }
  public static void main(String[] args) {
    TestPanel testPanel = new TestPanel();
  }
}
class TTT extends JPanel
{
  public TTT()
  {
    super();
  }
  public void paintComponent(Graphics g)
  {
    JOptionPane.showConfirmDialog(null,"paintComponent() called");
    Graphics2D g2 = (Graphics2D) g;
    g2.drawLine(10,10,300,300);
  }
//如果要试验没有paint()的情况注释掉下面的方法
  public void paint(Graphics g)
  {
    JOptionPane.showConfirmDialog(null,"paint() called");
    Graphics2D g2 = (Graphics2D) g;
    g2.drawRect(20,20,300,300);
  }
}

解决方案 »

  1.   

    楼上的,你是在JPanel中画的呀,我现在要在JFrame中(在JApplet中也没问题)import javax.swing.*;class Test extends JFrame implements Runnable
    {
    private int i = 10;

    public void display()
    {
    this.setSize(500,500);
    this.setVisible(true);
    }

    public static void main(String args[])
        {
         Test t = new Test();
         t.display();
         Thread thread = new Thread(t);
         thread.start();
        }
        
        public void run()
        {
         while(true)
         {
         try 
         {
         Thread.currentThread().sleep(1000);
        }
        catch (Exception ex) 
        {
         System.out.println (ex.toString());
        }
        System.out.println ("excute"); 
        this.repaint();
    }
        }
     /****************************************************************************/
        public void paintComponent(java.awt.Graphics g)//这儿用paint方法就没有问题,用paintComponent就不行,为什么?
        {
         System.out.println (i);
         g.drawString("i am test",10,i);
         i= i +10;
        }
    }
      

  2.   

    原因是这样的,JFrame继承自Frame类,而其他的如JPanel等继承自JComponent,在JFrame类中并没有paintComponent方法,只有paint()方法,所以你调用repaint()时,并不会执行paintComponent方法,在其他的类中如JPanel等,调用repaint()则会执行paintComponent方法,具体的你可以看一下JFrame和JPanel所继承的类的源代码。我只能理解到这个地步了。
      

  3.   

    是不是要调用父类的paint()方法?
      

  4.   

    本类中没有的Paint()方法,应该调用夫类中的方法吧
      

  5.   

    没发现你说的情况
    public class TestFrame extends JFrame
    {
    public void paint(Graphics g)
    {
    super.paint(g);
    g.setColor(Color.black);
    g.drawString("aaaaaa", 50, 50);
    }

    public static void main(String[] args)
    {
    JFrame f = new TestFrame();
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.show();
    }
    }