我在JPanel面板试这样实现的,是用定时器实现的,实现过程如下:
protected void paintComponent(Graphics g) {
     // draw the current time as the text
    g.setColor(Color.BLACK);
    g.drawString(hour + ":" + minute + ":" + second, 10, 15);
}定时器事件函数如下:
public void actionPerformed(ActionEvent e) {
    setCurrentTime();
    repaint();
}我想实现动态显示时间 格式是这样的 23:27:01
可是我的运行的时候 比如说一开始为 20:27:01 后来为20:27:02 他们重叠了 。
我试着去擦除原来的数字,可是还是不行。请问如何来擦除原来的数字啊?或者有什么更好的解决方法?求大家帮下忙!谢谢!

解决方案 »

  1.   

    在覆盖protected void paintComponent(Graphics g) 方法时请先调用super.paintComponent(g);
      

  2.   

    先调用super.paintComponent(g)有什么作用啊?super.paintComponent(g)它的用途是什么啊?
      

  3.   

    晕,还在这讨论啊。你可以这么理解这个问题,不调用super.paintComponent(g)的话,rePaint()的时候就会把你写的paintComponent中的内容绘制上去。如果你调用super.paintComponent(g),那么就会把整个组件彻底清空,然后依次再绘制。所以你要是增量绘制就别写super.paintComponent(g)如果是新图,就写上super.paintComponent(g)。你要是要了解细节,其实只要去看看JPanel的源代码就知道了。
      

  4.   

    protected void paintComponent(Graphics g) {
    if (ui != null) {
    Graphics scratchGraphics = (g == null) ? null : g.create();
    try {
    ui.update(scratchGraphics, this);
    } finally {
    scratchGraphics.dispose();
    }
    }
    }
    找到代码了 ,发来给大家共享下