drawstring() 应写在 paint()中

解决方案 »

  1.   

    应该调用repait():
     public void init(){
       repaint();
    }
     
      

  2.   

    在paint方法中使用drawString然后不论何时更新需显示的内容都要调用repaint
    不过你的情况是不用调用repaint的
      

  3.   

    如果是继承awt中的Panel,将drawString()写在paint中,重载update方法:
    public void update(Graphics g) {
      paint(g);
    }
    可防止闪烁。如果是JPanel就不必。因为Panel中update方法是先擦后画,有闪烁。而JPanel中用双缓冲画。
      

  4.   

    // Properties               /**               * Paints this component using the given graphics context.     * This is a standard Java AWT method which typically gets called     * by the AWT to handle painting this component. It paints this component     * using the given graphics context. The graphics context clipping region     * is set to the bounding rectangle of this component and its [0,0]     * coordinate is this component's top-left corner.     *     * @param g the graphics context used for painting     * @see java.awt.Component#repaint     * @see java.awt.Component#update               */              public void paint(Graphics g)              {                            Dimension dim = size();                            if (image != null)                            {                                          int imageWidth = image.getWidth(this);                                          int imageHeight = image.getHeight(this);                                          switch(imageStyle)                                          {                                                        default:                                                        case IMAGE_TILED:                                                        {                                                                      //Calculate number of images that should be drawn horizontally                                                                      int numHImages = dim.width / imageWidth;                                                                       //Don't forget remainders                                                                      if (dim.width % imageWidth != 0)                                                                                    numHImages++;                                                                       //Calculate number of images that should be drawn vertically                                                                      int numVImages = dim.height / imageHeight;                                                                       //Don't forget remainders                                                                      if (dim.height % imageHeight != 0)                                                                                    numVImages++;                                                                       int h;                                                                      int v = 0;                                                                      for (int vCount = 0;vCount < numVImages;vCount++)                                                                      {                                                                                    h = 0;                                                                                    for (int hCount = 0;hCount < numHImages;hCount++)                                                                                    {                                                                                                  g.drawImage(image, h, v, imageWidth, imageHeight, this);                                                                                                   //Increment to next column                                                                                                  h += imageWidth;                                                                                    }                                                                                     //Increment to next row                                                                                    v += imageHeight;                                                                      }                                                                       break;                                                        }                                                        case IMAGE_CENTERED:                                                        {                                                                      g.drawImage                                                                                    (image,                                                                                     (dim.width - imageWidth) / 2,                                                                                     (dim.height - imageHeight) / 2,                                                                                     imageWidth,                                                                                     imageHeight,                                                                                     this);                                                                       break;                                                        }                                                         case IMAGE_SCALED_TO_FIT:                                                        {                                                                      g.drawImage(image, 0, 0, dim.width, dim.height, this);                                                                       break;                                                        }                                                         case IMAGE_NORMAL:                                                        {                                                                      g.drawImage(image, 0, 0, this);                                                                       break;                                                        }                                          }//switch                            }                            else                            {                                g.clearRect(0, 0, dim.width, dim.height);                            }                            super.paint(g);              }