• 欢迎光临~

第一个Frame窗口

开发技术 开发技术 2022-01-28 155次浏览
//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args){
        //Frame,JDK,看源码
        Frame frame = new Frame("我的第一个Java图像界面窗口");

        //需要设置可见性 w h
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400, 400);

        //设置背景颜色
        frame.setBackground(new Color(85, 150, 68));

        //弹出的初始位置
        frame.setLocation(200, 200);
    }
}

第一个Frame窗口
问题:发现窗口关闭不掉,停止java程序!
尝试回顾封装:

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口 new
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
        MyFrame myFrame2 = new MyFrame(200, 200, 200, 200, Color.blue);
        MyFrame myFrame3 = new MyFrame(300, 300, 200, 200, Color.blue);
        MyFrame myFrame4 = new MyFrame(400, 400, 200, 200, Color.blue);
    }
}

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);
        setVisible(true);
    }
}

第一个Frame窗口

程序员灯塔
转载请注明原文链接:第一个Frame窗口
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com