要做Java的生命球程序,正处于框架阶段。但是形成的窗口是透明的,为什么呢?还有如何解决这个问题!
源码是:Ball包---Main类:
public class Main 
{   
     public static void main(String[] args)
     {     
        WinFrame frame=new WinFrame();      
    }
}Ball包---LifeBall类:
public class LifeBall 
{  
    static int radius=50;
    public int centreX=100,centreY=100;
    private double angle=30; //与十二点的夹角
}Ball包---WinFrame类:
public class WinFrame extends JFrame
{   
    LifeBall newBall=new LifeBall(); 
    Container c;
    JPanel p;
    public WinFrame() 
    {           
        addWindowListener(new MyWindowAdapter());  
        init();        
    }
    
    public void init()
    {     
        this.setTitle("生命球程序");
        this. setSize(500,500);
        this.setLocation(100,100);
                  
        c=getContentPane();
        
        p=new JPanel();
        c.add(p);
        this.setVisible(true); 
         
    }   
        
    public void paint(Graphics g)
    {
        g=c.getGraphics();
        g.setColor(Color.RED);
        g.fillOval(newBall.centreX,newBall.centreY,newBall.radius,newBall.radius);      
    }   
    
}events包---MyWindowAdapter类:public class MyWindowAdapter extends WindowAdapter 
{
    public void windowClosing(WindowEvent we) 
    {
        System.exit(0);
    }
}

解决方案 »

  1.   

    你是不是要这样?    我把你public的类 的public 都删掉了  你再自己加下import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;import javax.swing.JFrame;
    import javax.swing.JPanel;public class Main 
    {  
        public static void main(String[] args) 
        {    
            WinFrame frame=new WinFrame();      
        } 
    } class LifeBall 
    {  
        static int radius=50; 
        public int centreX=100,centreY=100; 
        private double angle=30; //与十二点的夹角 

    class WinFrame extends JFrame 
    {  
        LifeBall newBall=new LifeBall(); 
        Container c; 
        JPanel p; 
        public WinFrame() 
        {          
            addWindowListener(new MyWindowAdapter());  
            init();        
        } 
        
        public void init() 
        {    
            this.setTitle("生命球程序"); 
            this.setSize(500,500); 
            this.setLocation(100,100); 
                      
            c=getContentPane(); 
            
            p=new JPanel(); 
            c.add(p); 
            this.setVisible(true); 
            
        }  
            
        public void paint(Graphics g) 
        { 
         super.paint(g);
            g=c.getGraphics(); 
            g.setColor(Color.RED); 
            g.fillOval(newBall.centreX,newBall.centreY,newBall.radius,newBall.radius);  
        }  
        
    }  class MyWindowAdapter extends WindowAdapter 

        public void windowClosing(WindowEvent we) 
        { 
            System.exit(0); 
        } 
      

  2.   

    晕了   忘标记出来了   就加了 这句super.paint(g);我不太会这个画图的这个     你自己看下api好了   我也是在其中找的public void paint(Graphics g)从类 Container 复制的描述 
    绘制容器。该方法将 paint 转发给任意一个此容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.paint(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 paint() 转发给这个子组件。 
      

  3.   

    因为没有调用super.paint(g);所以导致没有绘制,谁能告诉我怎么使用java直接在屏幕上绘制不是窗口
    我想做透明。。难道没有接口??