面板(PaneI)與框架類似,也是一種容器,可以容納其他GUI組件。
面板可以通過構(gòu)造方法Panel()進(jìn)行創(chuàng)建。當(dāng)一個Panel對象被創(chuàng)建之后,還需要使用Container類的add()方法將它加入到某個Window對象或Frame對象中,這樣它才能變?yōu)榭梢姷摹?BR> 下面的程序創(chuàng)建了一個黃色面板,并且將該面板加到一個Frame對象中。
程序7—2
import java.a(chǎn)wt.*:
public class FrameWithPanel extends Frame{
∥構(gòu)造函數(shù)
public FrameWithPanel(String str){
super(str);
}
public static void main(String args[]){
FrameWithPanel fr=new FrameWithPanel(”Frame with Panel");
Panel pan=new Panel();
fr.setSize(300,200);
fr.setBackground(Color.blue);
fr.setLayout(null);
pan.setSize(100,100);
pan.setBackground(Color.yellow);
fr.a(chǎn)dd(pan);
fr.setVisible(true);
}
}
面板可以通過構(gòu)造方法Panel()進(jìn)行創(chuàng)建。當(dāng)一個Panel對象被創(chuàng)建之后,還需要使用Container類的add()方法將它加入到某個Window對象或Frame對象中,這樣它才能變?yōu)榭梢姷摹?BR> 下面的程序創(chuàng)建了一個黃色面板,并且將該面板加到一個Frame對象中。
程序7—2
import java.a(chǎn)wt.*:
public class FrameWithPanel extends Frame{
∥構(gòu)造函數(shù)
public FrameWithPanel(String str){
super(str);
}
public static void main(String args[]){
FrameWithPanel fr=new FrameWithPanel(”Frame with Panel");
Panel pan=new Panel();
fr.setSize(300,200);
fr.setBackground(Color.blue);
fr.setLayout(null);
pan.setSize(100,100);
pan.setBackground(Color.yellow);
fr.a(chǎn)dd(pan);
fr.setVisible(true);
}
}