日期:2014-05-20  浏览次数:20642 次

初学java,向各位高手请教个问题
add(button3);
add(button4);
pnl.setBounds(20,40,200,190);  
button1.setBounds(230,60,50,30);  
button2.setBounds(230,100,50,30);  
button3.setBounds(230,140,50,30);  
button4.setBounds(230,180,50,30);  
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
cvs=new   Cvs[11];
for(int   i=1;   i <=10;   i++)
{
cvs[i]=new   Cvs(i,i);
pnl.add( " "+i,cvs[i]);
}
addWindowListener(
new   WindowAdapter(){
public   void   windowClosing(WindowEvent   e){
setVisible(false);
System.exit(0);}});
          }

public   void   actionPerformed(ActionEvent   e)
{
if(e.getSource()==button1)
{
crd.next(pnl);
crd.first(pnl);
}
else   if(e.getSource()==button2)
{
crd.next(pnl);
}
else   if(e.getSource()==button3)
{
crd.previous(pnl);
}
else   if(e.getSource()==button4)
{
crd.last(pnl);
}
}

class   Cvs   extends   Canvas
{
int   x,y;
Cvs(int   x0,int   y0)
{
x=x0;   y=y0;
setBackground(Color.cyan);
}
public   void   paint(Graphics   g)
{
g.setColor(Color.blue);
g.fillRect(50,25,10*x,8*y);
g.drawString( "这是第 "   +   x   +   "个画面 ",20,150);
}
}

  public   static   void   main(String   args[])
{
layoutcard   frm=new   layoutcard();
}
}

------解决方案--------------------
请问其中有“pnl.add( " "+i,cvs[i]);”这个程序语句,能否改成“pnl.add(cvs[i]);”为什么?