要做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);
}
}
源码是: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);
}
}
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);
}
}
绘制容器。该方法将 paint 转发给任意一个此容器子组件的轻量级组件。如果重新实现此方法,那么应该调用 super.paint(g) 方法,从而可以正确地呈现轻量级组件。如果通过 g 中的当前剪切设置完全剪切某个子组件,则不会将 paint() 转发给这个子组件。
我想做透明。。难道没有接口??