GUI编程

前言:告诉大家应该怎么学?

  • 这是什么?
  • 它怎么玩?
  • 该如何在我们平时运用?

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具

一、是什么

  1. GUI是图形界面编程

  2. GUI的核心技术:Swing AWT

  3. GUI缺点:界面不美观;需要jar环境

二、为什么

为什么我们要学习

  1. 可以写出自己心中想要的一些小工具

  2. 工作的时候,也可能需要维护到swing界面,(概率极小!)

  3. 了解MVC架构,了解监听!

三、怎么做

1、AWT

1.1 AWT介绍

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

1.2 组件和容器

1.2.1. Frame(容器)
package com.gui;

import java.awt.*;

public class TestFrame {
public static void main(String[] args) {
Frame frame =new Frame("我的第一个JAVA图像界面窗");
frame.setVisible(true);
frame.setSize(200,200);
frame.setBackground(Color.BLUE);
frame.setLocation(200,200);
frame.setResizable(false);
}
}

展示多个窗口

package com.gui;

import java.awt.*;

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.GREEN);
MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.MAGENTA);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.YELLOW); } }
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);
}
}

1.2.2. Panel(面板)
package com.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class TestPanel {
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(Color.GREEN);
//相对于frame的面板坐标
panel.setBounds(50,50,400,400);
panel.setBackground(Color.BLUE);
//将面板添加到窗口
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//结束程序
}
}); } }

1.2.3. 布局管理器
  1. 流式布局

    package com.gui;
    
    import com.sun.media.jfxmedia.events.NewFrameEvent;
    
    import java.awt.*;
    
    public class TestLayout {
    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");
    //设置为流式布局
    frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    frame.setVisible(true);
    frame.setSize(200,200);
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4); }
    }

  1. 东西南北中

    package com.gui;
    
    import java.awt.*;
    
    public class TestLayoutBorder {
    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); }
    }

  1. 表格布局

    package com.gui;
    
    import java.awt.*;
    
    public class TestLayoutGrid {
    public static void main(String[] args) {
    Frame frame = new Frame();
    Button btn1 = new Button("btn1");
    Button btn2 = new Button("btn2");
    Button btn3 = new Button("btn3");
    Button btn4 = new Button("btn4");
    Button btn5 = new Button("btn5");
    Button btn6 = new Button("btn6");
    //设置表格布局 3*2
    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); }
    }

GUI编程学习笔记——day01的更多相关文章

  1. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  2. 多线程编程学习笔记——async和await(一)

    接上文 多线程编程学习笔记——任务并行库(一) 接上文 多线程编程学习笔记——任务并行库(二) 接上文 多线程编程学习笔记——任务并行库(三) 接上文 多线程编程学习笔记——任务并行库(四) 通过前面 ...

  3. C++ GUI Qt4学习笔记03

    C++ GUI Qt4学习笔记03   qtc++spreadsheet文档工具resources 本章介绍创建Spreadsheet应用程序的主窗口 1.子类化QMainWindow 通过子类化QM ...

  4. Linux Shell编程学习笔记——目录(附笔记资源下载)

    LinuxShell编程学习笔记目录附笔记资源下载 目录(?)[-] 写在前面 第一部分 Shell基础编程 第二部分 Linux Shell高级编程技巧 资源下载 写在前面 最近花了些时间学习She ...

  5. DirectX 11游戏编程学习笔记之8: 第6章Drawing in Direct3D(在Direct3D中绘制)(习题解答)

            本文由哈利_蜘蛛侠原创,转载请注明出处.有问题欢迎联系2024958085@qq.com         注:我给的电子版是700多页,而实体书是800多页,所以我在提到相关概念的时候 ...

  6. 多线程编程学习笔记——async和await(二)

    接上文 多线程编程学习笔记——async和await(一) 三.   对连续的异步任务使用await操作符 本示例学习如何阅读有多个await方法方法时,程序的实际流程是怎么样的,理解await的异步 ...

  7. 多线程编程学习笔记——async和await(三)

    接上文 多线程编程学习笔记——async和await(一) 接上文 多线程编程学习笔记——async和await(二) 五.   处理异步操作中的异常 本示例学习如何在异步函数中处理异常,学习如何对多 ...

  8. 多线程编程学习笔记——使用异步IO(一)

    接上文 多线程编程学习笔记——使用并发集合(一) 接上文 多线程编程学习笔记——使用并发集合(二) 接上文 多线程编程学习笔记——使用并发集合(三) 假设以下场景,如果在客户端运行程序,最的事情之一是 ...

  9. 多线程编程学习笔记——编写一个异步的HTTP服务器和客户端

    接上文 多线程编程学习笔记——使用异步IO 二.   编写一个异步的HTTP服务器和客户端 本节展示了如何编写一个简单的异步HTTP服务器. 1.程序代码如下. using System; using ...

随机推荐

  1. bzoj5312 冒险(吉司机线段树)题解

    题意: 已知\(n\)个数字,进行以下操作: \(1.\)区间\([L,R]\) 按位与\(x\) \(2.\)区间\([L,R]\) 按位或\(x\) \(3.\)区间\([L,R]\) 询问最大值 ...

  2. javascript & call & apply & bind & new

    javascript & call & apply & bind & new Javascript call() & apply() vs bind()? ht ...

  3. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  4. js & Input & Paste & Clipboard & upload & Image

    js & Input & Paste & Clipboard & upload & Image input paste upload image js Clip ...

  5. free online code editor

    free online code editor online vscode https://stackblitz.com/ https://codesandbox.io/ https://codesh ...

  6. Chrome & console.log & color & js

    Chrome & console.log & color & js console.log & color // OK log(`%cchat_list =\n%c${ ...

  7. 如何成为NGK超级节点?

    NGK这个 "超级节点" 到底是什么?什么是超级节点呢? 区块链网络中的每一个节点,其实就是存储数据的每一台电脑或者服务器终端.节点要完成新区块的生产.交易的验证与记帐.因此节点之 ...

  8. Docker中配置MySQL并实现远程访问

    Docker配置MySQL容器 拉取MySQL镜像 docker pull mysql:5.6 有可能会因为网络问题失败,重复尝试. 创建容器 docker run -d --name selfdef ...

  9. 🎊 Element UI 新春快报

    新年好,Element UI 开发团队给各位支持我们的开发者们拜个晚年,祝大家在新的一年里工作没 bug, 天天不加班. 在过去一年里,Element UI 团队在稳定维护 Vue 2.x 版本的同时 ...

  10. Redis集群简介及部署

    1简介 在 Redis 3.0 之前,使用 哨兵(sentinel)机制来监控各个节点之间的状态.Redis Cluster 是 Redis 的 分布式解决方案,在 3.0 版本正式推出,有效地解决了 ...