Java中GUI编程总结—AWT中的Frame容器、panel面板、布局管理

本文介绍了Java图形用户界面GUI的基础,主要关注AWT库的使用,包括窗口、按钮、文本框等组件,以及容器、面板的概念。通过实例展示了如何创建和定制窗口,如设置大小、颜色和位置。此外,文章还探讨了布局管理器,如流式布局、东西南北中布局和表格布局,并给出了相应代码示例。最后,文章提供了一个综合练习,展示了如何组合使用不同的布局管理器创建复杂的界面布局。
摘要由CSDN通过智能技术生成

AWT中的容器、面板、布局管理

GUI

1.简介

  • GUI核心技术:Swing AWT
    1.因为界面不美观,2.需要jre环境
  • 用处:1.可以写一些小工具。2.工作时候,维护swing界面,概率概率!3.了解MVC架构,了解监听。

2、AWT

2.1 AWT介绍

  1. 包含了很多的类和接口!GUI!
  2. 元素:窗口、按钮、文本框
  3. java.awt
    在这里插入图片描述

2.2 组件和容器

容器
public static void main(String[] args) {
    //Frame
    Frame frame= new Frame("我的第一个窗口界面");
    //需要设置可见性
    frame.setVisible(true);
    //设置窗口的大小
    frame.setSize(400,400);
    //设置窗口的颜色color
    frame.setBackground(new Color(63, 121, 48));
    //弹出的初始位置
    frame.setLocation(200,200);
    //设置大小固定
    frame.setResizable(true);
}

在这里插入图片描述
问题:发现程序不能关掉,停止java程序的运行

  • 尝试回顾封装

    public class testFrame {
        public static void main(String[] args) {
            Myframe myframe1=new Myframe(100,100,200,200,Color.blue);
            Myframe myframe2=new Myframe(300,100,200,200,Color.yellow);
            Myframe myframe3=new Myframe(100,300,200,200,Color.pink);
            Myframe myframe4=new Myframe(300,300,200,200,Color.gray);
        }
    }
    //继承封装
    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);
        }
    }
    

在这里插入图片描述

面板panel

解决了关闭事件

public static void main(String[] args) {
        Frame frame=new Frame();
        Panel panel=new Panel();
        //设置布局
        frame.setLayout(null);
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(41, 122, 41));
        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(122, 123, 165));
        //frame.add(panel)
        frame.add(panel);
        frame.setVisible(true);
        //设置监听事件 System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

在这里插入图片描述

布局管理

  • 流式布局

    public static void main(String[] args) {
            Frame frame=new Frame();
            Button button1=new Button("button1");
            Button button2=new Button("button2");
            Button button3=new Button("button3");
            //设置为流式布局
            //frame.setLayout(new FlowLayout());
            frame.setLayout(new FlowLayout(FlowLayout.LEFT));
            frame.setSize(200,200);
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            frame.setVisible(true);
        }
    

在这里插入图片描述

  • 东西南北中布局

    public static void main(String[] args) {
            Frame frame=new Frame("TestBorderLayout");
            Button east=new Button("East");
            Button west=new Button("West");
            Button south=new Button("South");
            Button north=new Button("North");
            Button center=new Button("Center");
            
            frame.add(east,BorderLayout.EAST);
            frame.add(west,BorderLayout.WEST);
            frame.add(south,BorderLayout.SOUTH);
            frame.add(north,BorderLayout.NORTH);
            frame.add(center,BorderLayout.CENTER);
            frame.setSize(200,200);
            frame.setVisible(true);
        }
    

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ubGWCtpp-1619522717576)(GUI笔记.assets/image-20210427172346261.png)]

  • 表格布局Grid

    public static void main(String[] args) {
            Frame frame=new Frame("TestBorderLayout");
            Button bnt1=new Button("bnt1");
            Button bnt2=new Button("bnt2");
            Button bnt3=new Button("bnt3");
            Button bnt4=new Button("bnt4");
            Button bnt5=new Button("bnt5");
            Button bnt6=new Button("bnt6");
            frame.setLayout(new GridLayout(3,2));
            frame.add(bnt1);
            frame.add(bnt2);
            frame.add(bnt3);
            frame.add(bnt4);
            frame.add(bnt5);
            frame.add(bnt6);
            frame.pack();//java函数,选择最优的方式去布局
            frame.setVisible(true);
        }
    

在这里插入图片描述

  • 小练习

    public static void main(String[] args) {
            //总
            Frame frame=new Frame();
            frame.setSize(400,300);
            frame.setLocation(300,400);
            frame.setBackground(Color.gray);
            frame.setVisible(true);
            frame.setLayout(new GridLayout(2,1));
            //分,4个面板
            Panel p1=new Panel(new BorderLayout());
            Panel p2=new Panel(new GridLayout(2,1));
            Panel p3=new Panel(new BorderLayout());
            Panel p4=new Panel(new GridLayout(2,2));
            //上面ok
            p1.add(new Button("east-1"),BorderLayout.EAST);
            p1.add(new Button("west-1"),BorderLayout.WEST);
            p2.add(new Button("p2-btn1"));
            p2.add(new Button("p2-btn2"));
            p1.add(p2,BorderLayout.CENTER);
            //下面
            p3.add(new Button("east-2"),BorderLayout.EAST);
            p3.add(new Button("west-2"),BorderLayout.WEST);
            p4.add(new Button("p4-btn1"));
            p4.add(new Button("p4-btn2"));
            p4.add(new Button("p4-btn3"));
            p4.add(new Button("p4-btn4"));
            p3.add(p4,BorderLayout.CENTER);
    
            frame.add(p1);
            frame.add(p3);
            frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit();
                    }
                });
        }
    

在这里插入图片描述

总结

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中
  3. 布局管理器
    1. 流式
    2. 东西南部中
    3. 表格
  4. 大小、定位、背景颜色、可见性、监听!
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西瓜程序设计

您的打赏将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值