public void setBounds(int x, int y, int width, int height)
设置窗体位置和大小,x和y表示窗体左上角距离屏幕的水平和垂直距离,width和height是窗体本身的宽度和高度
public void setSize(int width, int height)
设置窗体的大小,width和height是窗体本身的宽度和高度
public void setVisible(boolean flag)
设置窗体是否可见,true表示可见,false表示不可见
public void set setBackground(Color c)
设置窗体的背景色
import java.awt.*;classMyFrameextendsFrame{publicstaticint id =0;MyFrame(int x,int y,int w,int h, Color color){super("MyFrame "+(++id));setBackground(color);setLayout(null);setBounds(x, y, w, h);setVisible(true);}}publicclassTestFrame{publicstaticvoidmain(String[] args){
MyFrame f1 =newMyFrame(100,100,200,200, Color.BLUE);
MyFrame f2 =newMyFrame(300,100,200,200, Color.YELLOW);
MyFrame f3 =newMyFrame(100,300,200,200, Color.GREEN);
MyFrame f4 =newMyFrame(300,300,200,200, Color.MAGENTA);}}
Panel
panel 是容纳其他组件的组件
panel是容器
panel不能单独存在,必须得被添加到其他容器中
panel类拥有从其父类继承来的
public void setBounds(int x, int y, int width, int height)
public void setSize(int width, int height)
public void setVisible(boolean flag)
public void set setBackground(Color c)
setLayout(LayoutManager mgr)等方法
import java.awt.*;publicclassTestPanel{publicstaticvoidmain(String[] args){
Frame f =newFrame("Java Frame with Panel");
Panel p =newPanel();
f.setLayout(null);
f.setBounds(300,300,500,500);
f.setBackground(newColor(100,100,102));
p.setBounds(150,150,250,250);// p 相对于 f 的位置
p.setBackground(newColor(204,204,255));
f.add(p);
f.setVisible(true);}}