Nldmg 2016-11-09 23:03 采纳率: 100%
浏览 3212
已结题

Java JFrame 使用 paint 方法不显示绘制图像。

 public class Show extends JFrame{
    public Show(){
        setTitle("Calcutor");
        setSize(90*3,160*3);
        setVisible(true);
    }
}

public class Main {
    public static void main(String args[]){
        Show show=new Show();
        Graphics g= show.getGraphics(); 
        g.setColor(Color.pink);
        g.fillRect(0, 0, show.getWidth(), show.getHeight()); 
    }
}

上面这个不能显示图片。

 public class Main {
    public static void main(String args[]){
        Show show=new Show();
        Graphics g= show.getGraphics(); 
        try   
        {   
        Thread.currentThread().sleep(1000);//毫秒   
        }   
        catch(Exception e){} 
        g.setColor(Color.pink);
        g.fillRect(0, 0, show.getWidth(), show.getHeight()); 

    }
}

当我加上

 try   
    {   
        Thread.currentThread().sleep(1000);//毫秒   
    }   
    catch(Exception e){};
时就会显示图像,这是怎么回事?能不能 不加这个也能显示图像呢?
  • 写回答

2条回答 默认 最新

  • dengping663 2016-11-09 23:55
    关注

    jframe的setvisible放后面调用,即画完后

    评论

报告相同问题?