Java -- GUI编程(笨蛋笔记)

GUI编程

1、简介

Gui的核心技术:Swing AWT

  1. 界面不美观
  2. 需要 jre 环境!

为什么要学习?

  1. 可以写出自己心中想要的一些小工具
  2. 工作时候,也可能需要维护到swing界面,概率极小 !
  3. 了解MVC架构,了解监听!

2、AWT

2.1、Awt介绍

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-98qy3IJr-1587823721952)(F:\Typora\JavaGUI编程\1.png)]

2.2、组件和容器

1、Frame
package com.lan.gui.lessen01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
   
    public static void main(String[] args) {
   

        //Frame , JDK , 看源码
        Frame frame = new Frame("我的第一个JAVA图像界面窗口");

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

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

        //设置背景颜色 Color
        frame.setBackground(new Color(22,44,230));

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

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

问题:不能关闭窗口,只能点停止运行关闭!

用封闭来实现:

package com.lan.gui.lessen01;

import java.awt.*;

public class TestFrame2 {
   
    public static void main(String[] args) {
   
        //展示多个窗口 new
        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.magenta);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200,Color.red);
    }
}

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.面板Panel

解决了关闭事件!

package com.lan.gui.lessen01;

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

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

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

        //frame.add(panle) 添加到里面
        frame.add(panel);

        frame.setVisible(true); //可见

        //监听事件,监听窗口关闭事件 System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
   
            @Override
            public void windowClosing(WindowEvent e) {
   
                //结束程序
                System.exit(0);
            }
        });
    }
}

2.3、 布局管理器

  • 流式布局
package com.lan.gui.lessen01;

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(200,200);

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

        frame.setVisible(true);
    }
}
  • 东西南北中
package com.lan.gui.lessen01;

import java.awt.*;

public class TestBorderLayout {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();

        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); //可见
    }
}
  • 表格布局
package com.lan.gui.lessen01;

import java.awt.*;

public class TestGridLayout {
   
    public static void main(String[] args) {
   
        Frame frame = new Frame();

        Button bun1 = new Button("bun1");
        Button bun2 = new Button("bun2");
        Button bun3 = new Button("bun3");
        Button bun4 = new Button("bun4");
        Button bun5 = new Button("bun5");
        Button bun6 = new Button("bun6");

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

        frame.add(bun1);
        frame.add(bun2);
        frame.add(bun3);
        frame.add(bun4);
        frame.add(bun5);
        frame.add(bun6);

        frame.pack(); //java函数自动生成窗口大小(开始)
        frame.setVisible(true);
    }
}
.练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sOTz0wb5-1587823721957)(F:\Typora\JavaGUI编程\2.png)]

package com.lan.gui.lessen01;

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

//练习
public class ExDemo {
   
    public static void main(String[] args) {
   
        //总 Fram
        Frame frame = new Frame();

        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.BLACK);
        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));

        //上面部分
        p1.add(new Button("East-1"),BorderLayout.EAST);//东
        p1.add(new Button("West-1"),BorderLayout.WEST);//西
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2,BorderLayout.CENTER); //添加到p1面板中

        //下面
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p3.add(new Button("West-2"),BorderLayout.WEST);
        //中间4个
        for (int i = 0; i < 4; i++) {
   
            p4.add(new Button("for-"
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值