GUI编程

本文介绍了GUI编程的核心技术,包括Java的AWT和Swing库。AWT提供了基本的组件如Frame、Panel,以及布局管理器、事件监听等。Swing则在AWT基础上提供更丰富的组件和更好的外观,如窗口、面板、标签、按钮、列表等,且Swing组件默认带有关闭事件。
摘要由CSDN通过智能技术生成

GUI编程

1、简介

GUI核心技术:Swing和AWT

为什么没有流行起来?

  • 界面不美观
  • 需要jre环境

2、AWT

Awt介绍:

  • 包含了很多类和接口,GUI即用户图形界面
  • 元素:窗口,按钮,文本框
  • 有两个核心类:组件component和容器container

在这里插入图片描述

组件和容器:

2.1Frame

public static void main(String[] args) {
   
        Frame frame = new Frame("我的第一个Java图形界面窗口");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小width,height
        frame.setSize(500,500);
        //设置背景颜色
        frame.setBackground(new Color(59, 87, 225));
        //弹出的初始位置
        frame.setLocation(250,250);
        //设置大小固定
        frame.setResizable(false);
    }

在这里插入图片描述

问题:

窗口无法关闭,只能通过java结束程序运行

展示多个窗口:

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.pink);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.gray);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.green);

    }
}
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);
    }

在这里插入图片描述

2.2面板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(166, 248, 165));
        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(232, 232, 126));
        //frame.add(panel)
        frame.add(panel);
        //设置可见性,默认为false
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件 System.exit(e)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
   
            //窗口关闭时候要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
   
                //结束程序
                System.exit(0);
            }
        });
    }

在这里插入图片描述

2.3布局管理器

  • 流式布局

    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(500,500);
    
            //把按钮添加上去
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
    
            frame.setVisible(true);
    
            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("East");
            Button button2 = new Button("West");
            Button button3 = new Button("South");
            Button button4 = new Button("North");
            Button button5 = new Button("Center");
    
            frame.add(button1,BorderLayout.EAST);
            frame.add(button2,BorderLayout.WEST);
            frame.add(button3,BorderLayout.SOUTH);
            frame.add(button4,BorderLayout.NORTH);
            frame.add(button5,BorderLayout.CENTER);
    
            frame.setSize(500,500);
            frame.setVisible(true);
            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");
            Button button4 = new Button("button4");
            Button button5 = new Button("button5");
            Button button6 = new Button("button6");
    
            frame.setLayout(new GridLayout(3,2));
    
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            frame.add(button4);
            frame.add(button5);
            frame.add(button6);
    
            frame.pack();//Java函数,自动选择一个优秀布局确定
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter() {
         
                @Override
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值