GUI之Panel面板

package day14;

 

import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

 

public class AwtDemo3 {

    //panel可以看成是一个空间,但是不能单独存在

    public static void main(String[] args) {

        Frame frame = new Frame();

        Panel panel = new Panel();

        //设置布局

        frame.setLayout(null);

        //坐标

        frame.setBounds(300, 400, 500, 500);

        frame.setBackground(new Color(140, 190, 100));

        //panel设置坐标,相对于frame

        panel.setBounds(40, 40, 300, 300);

        panel.setBackground(new Color(89, 90, 100));

        //将面板放到frame中

        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件 System.exit(0)

        //适配器模式

        frame.addWindowListener(new WindowAdapter() {

            //窗口点击关闭的时候需要做的事情

            @Override

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            }

        });

    }

}

执行结果:

欢迎批评指正,提出问题,谢谢!
原文地址:https://www.cnblogs.com/xxeleanor/p/15110586.html