GUI编程-Frame和Panel

我的第一个窗口

package com.zwxxGUI.lemsson01;

import java.awt.*;

public class lemsson01 {
    
    
    public static void main(String[] args) {
    
    

        Frame frame = new Frame("我的第一个Java图像界面窗口");

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

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

        //设置背景颜色
        frame.setBackground(Color.RED);

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

        //设置大小固定
        frame.setResizable(false);
    }
}

面板和窗口

package com.zwxxGUI.lemsson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TextPamel {
    
    
    public static void main(String[] args) {
    
    

        //窗口和面板
        Frame frame = new Frame();
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);

        //坐标   bounds:界限
        frame.setBounds(300,300,500,500);
        frame.setBackground(Color.RED);

        //panel 设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(Color.BLUE);

        frame.add(panel);
        frame.setVisible(true);

        //监听时间,监听窗口关闭事件 System.exit(0)
        //适配器模式:  Listnenr:监听器  Adapter:适配
        //添加监听事件 参数是实例化的方法
        frame.addWindowListener(new WindowAdapter() {
    
    
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //结束程序
                System.exit(0);
            }
        });

    }
}

猜你喜欢

转载自blog.csdn.net/anxious333/article/details/117307235