第一个Frame窗口

第一个Frame窗口

写出单个frame窗口:

package com.zhang.Study;

import java.awt.*;

public class FirstFrameTest {
    public static void main(String[] args) {
        //;创建窗口用frame类,窗口在内存中创建,并不会显示,需要用可见方法显示
        Frame frame = new Frame("我的第一个java图像界面窗口");
        //设置窗口可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(500,500);
        //设置窗口颜色
        frame.setBackground(new Color(211, 99, 99));
        //设置初始位置
        frame.setLocation(400,400);
        //设置窗口大小固定
        frame.setResizable(false);

    }
}
}

frame窗口优化:

package com.zhang.Study;

import java.awt.*;

public class FewFrameTest {
    public static void main(String[] args) {
    MyFrame mf1 = new MyFrame(200,400,400,400,Color.BLUE);
    MyFrame mf2 = new MyFrame(800,400,400,400,Color.YELLOW);
    MyFrame mf3 = new MyFrame(1200,400,400,400,Color.green);
    MyFrame mf4 = new MyFrame(1600,400,400,400,Color.PINK);


    }


}
class  MyFrame extends Frame {
    static int id =0;//可能存在多个窗口,需要一个计数器
    public  MyFrame(int x,int y,int w, int h,Color color){
        super("myframe+"+(++id));
        setBackground(color);
        setBounds(x,y,w,h);//设置起始位置和宽高,是setlocation和setsize方法的集合。
        setVisible(true);//设置可见性为true;

    }
}

 

posted on 2021-11-26 10:20  张铁蛋666  阅读(60)  评论(0)    收藏  举报

导航