GUI编程学习日志day01

GUI编程学习日志day01

1.简介

图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。[1]

图形用户界面是一种人与计算机通信的界面显示格式,允许用户使用鼠标等输入设备操纵屏幕上的图标或菜单选项,以选择命令、调用文件、启动程序或执行其它一些日常任务。与通过键盘输入文本或字符命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。

2. AWT

2.1 Awt介绍

  1. 包含了哼多类和接口
  2. 元素:窗口,按钮,文本框

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ud7THiii-1598885988737)(E:\图片\GUI编程截图\awt框架截图.png)]

2.2 组件和容器

1. Frame
package com.dai.lesson01;

import java.awt.*;

//创建一个Gui的普通界面
public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("这是我的第一个Java图像界面窗口");

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

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

        //设置背景的颜色
        frame.setBackground(new Color(77, 87, 121));

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

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



    }
}

  1. 回顾封装
package com.dai.lesson01;


import java.awt.*;

//创建多个Gui界面
public class TestFrame02 {
    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.red);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.MAGENTA);
    }
}
class MyFrame extends Frame{
    static int id = 0 ; //可能存在多个窗口,需要一个计数器
    public MyFrame(int x,int y ,int w,int h,Color color){
        super("Myframe"+(++id));
        Frame frame = new Frame("这是我的第"+id+"个Java图像界面窗口");
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }
}

2. 面板Panel
package com.dai.lesson01;

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

//Panel 可以看成是一个空间,但是不能单独存在
public class TextPanel {
    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(0x47D29F));

        //panel设置坐标,先对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(0xA65952));

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

        //监听事件,监听关闭窗口  System.exit(0)
        //适配器模式

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭窗口
                System.exit(0);
            }
        });
    }
}

3. 布局管理器
  • 线式布局
package com.dai.lesson01;

import java.awt.*;

public class TestFlowLayout {
    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.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setSize(500,500);

        //添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

    }
}

  • 东西南北中
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TextBorder");

        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(500,500);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //关闭窗口
                System.exit(0);
            }
        });
    }
}

  • 表格布局
public class TextGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TextGridLayout");
        Button btn1 = new Button("bnt1");
        Button btn2 = new Button("bnt2");
        Button btn3 = new Button("bnt3");
        Button btn4 = new Button("bnt4");
        Button btn5 = new Button("bnt5");
        Button btn6 = new Button("bnt6");

        frame.setLayout(new GridLayout(3,2));

        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();
        frame.setVisible(true);
    }
}

3. 总结

现在已经开学,暑假的时间已经过去,马上就迎来了九月。自己原本打算在暑假的时候好好的学习,结果计划赶不上变化。想要学很多很多的东西结果没有完成,这个学期呢课也有很多。没有什么多余的时间来学习,自己也该准备四级,六级相关的知识。上课要认真的听讲,不要留下问题等要后面来解决。现在已经大三了,该准备考研相关的东西。比如了解专业课,储备相关的知识。总之,一切看自己,加油!!! 未来可期

​ 2020/8/31 22:58

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值