我有一个关于AWT的初级问题,是这样的
****************Container*******************************
public class MyBrickList extends Container{
/** Creates a new instance of MyBrickList */
private MyBrick[] list;
Container c;
public MyBrickList(Container c,int xx,int yy) {
list = new MyBrick[81];
int z=0;
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++){
int posx = xx+x * KKGame.sizeFactorW * 9;
int posy = yy+y * KKGame.sizeFactorH * 9;
list[z]= new MyBrick(posx,posy,1);
this.add(list[z]);
z++;
}
}
this.c=c;
}
public void addCom(Component com)
{
this.add(com,this);
}
public void AddToFrame()
{
c.add(this);
}
}
************************Component******************************************
public class MyBrick extends Component{
private int s_x,s_y;
private int type;
/**
* Creates a new instance of MyBrick
*/
public MyBrick(int x,int y,int type) {
this.type=type;
s_x = x;
s_y = y;
setBounds(s_x,s_y,70,50);
}
public void Move(int x,int y)
{
setBounds(x,y,70,50);
}
public void paint(Graphics g)
{
if(type==1){
g.setColor(Color.BLACK);
g.drawRect(0,0,70,50);
g.setColor(new Color(Def.RGB_FORE));
g.drawRect(1,1,69,49);
g.setColor(Color.GRAY);
g.fillRect(2,2,66,46);
}
if(type==2)
{
g.setColor(Color.PINK);
g.fillRect(0,0,70,50);
}
}
}
然后我在一个Frame中, MyBrickList list = new MyBrickList(this,xx,yy);
再往这个Container里添加新的一个Component,
MyBall nn = new MyBall(401,89);
list.addCom(nn);
最后list.AddToFrame();
setVisible(true);
为设么最后添加的那个nn 的 Component在屏幕上显示不出来呢?
谢谢大家了
****************Container*******************************
public class MyBrickList extends Container{
/** Creates a new instance of MyBrickList */
private MyBrick[] list;
Container c;
public MyBrickList(Container c,int xx,int yy) {
list = new MyBrick[81];
int z=0;
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++){
int posx = xx+x * KKGame.sizeFactorW * 9;
int posy = yy+y * KKGame.sizeFactorH * 9;
list[z]= new MyBrick(posx,posy,1);
this.add(list[z]);
z++;
}
}
this.c=c;
}
public void addCom(Component com)
{
this.add(com,this);
}
public void AddToFrame()
{
c.add(this);
}
}
************************Component******************************************
public class MyBrick extends Component{
private int s_x,s_y;
private int type;
/**
* Creates a new instance of MyBrick
*/
public MyBrick(int x,int y,int type) {
this.type=type;
s_x = x;
s_y = y;
setBounds(s_x,s_y,70,50);
}
public void Move(int x,int y)
{
setBounds(x,y,70,50);
}
public void paint(Graphics g)
{
if(type==1){
g.setColor(Color.BLACK);
g.drawRect(0,0,70,50);
g.setColor(new Color(Def.RGB_FORE));
g.drawRect(1,1,69,49);
g.setColor(Color.GRAY);
g.fillRect(2,2,66,46);
}
if(type==2)
{
g.setColor(Color.PINK);
g.fillRect(0,0,70,50);
}
}
}
然后我在一个Frame中, MyBrickList list = new MyBrickList(this,xx,yy);
再往这个Container里添加新的一个Component,
MyBall nn = new MyBall(401,89);
list.addCom(nn);
最后list.AddToFrame();
setVisible(true);
为设么最后添加的那个nn 的 Component在屏幕上显示不出来呢?
谢谢大家了
KKGame.sizeFactorW ,KKGame.sizeFactorH,MyBall又是什么?
还有就是你那个Frame设置它的大小了吗?(setSize(300,300))否则只是一个标题栏,什么也没有,肯定看不过你所说的Component!