zoukankan      html  css  js  c++  java
  • JAVA GUI 图形界面编程 简易入门笔记 【学习更新中】

    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 组件和容器

    2.2.1 Frame

    public class TestFrame{
      psvm(String[] args){
        // Frame JDK 原码
        Frame frame = new Frame(title:"我的第一个Java GUI");
        
        // 需要设置可见性 
        frame.setVisible(true);
        
        // 设置窗口耨大小
        frame.setSize(400, height:300);
        
        // 设置背景色 color
        frame.setBackground(new Color(r:85, g:39, b:42));
        
        // 弹出的初始位置
        fram.setLocation(x:300, y:400);
       
        // 设置大小固定
        fram.setResizable(false);
      }
    }
    

    此时问题:发现窗口关闭不了,只能停止java程序

    回顾:尝试封装

    public class TestFrame2{
      psvm(String[] args){
      	// 展示多个窗口 new
        MyFrame myFrame = new MyFrame(x:100, y:100, w:200, h:200, Color.blue);
    		MyFrame myFrame2 = new MyFrame(x:300, y:100, w:200, h:200, Color.yellow);
        MyFrame myFrame3 = new MyFrame(x:100, y:300, w:200, h:200, Color.red);
        MyFrame myFrame4 = new MyFrame(x:300, y:300, w:200, h:200, Color.green);
      }
    }
    
    class MyFrame extengds Frame{
      static int id = 0; // 存在多个窗口 计数器
      
      public MyFrame(int x, int y, int w, int h, Color color){
        super(title:"MyFrame" + (++id));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
      }
    }
    

    2.2.2 面板 panel

    解决了面板关闭问题

    // panel 可以看成是一个空间,但是不能单独存在
    public class TestFrame2{
      psvm(String[] args){
      	Frame frame = new Frame();
        Panel panel = new Panel();
        // 设置布局
        frame.setLayout(null);
        
        // 坐标
        frame.setBounds(x: 300, y: 30,  400, height: 500);
        frame.setBackground(new Color(r:40, g:33, b: 23));
        
        // panel 设置坐标,相对于frame
        panel.setBounds(x:40, y: 30, 422, height:400);
        panel.setBackground(new Color(r:40, g:13, b:13));
        
        // frame.add(panel)
        frame.add(panel);
        frame.setVisible(true);
        
        // 监听事件,监听窗口关闭事件 System.exit(0)
        // 适配器模式
        frame.addWindowListener(new WindowAdapter(){
          // 窗口点击关闭的时候需要做的事情
          @Override
          public void windowClosing(WindowEVent e){
            // 结束程序
            System.exit(status:0);
          }
        });
      }
    }
    

    2.2.3 布局管理器

    • 流式布局
    public class TestLayout{
      psvm(String[] args){
    		Frame frame = new Frame();
        
        // 组件-按钮
        Button button1 = new Button(label:"button1");
        Button button2 = new Button(label:"button2");
        Button button3 = new Button(label:"button3");
        
        // 设置为流式布局
        frame.setLayout(new FlowLayout());
        
        frame.setSize(200, height:300);
        
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        
        frame.setVisibale(true);
      }
    }
    
    • 东西南北中
    public class TestBorderLayout{
      psvm(String[] args){
    		Frame frame = new Frame(tiele:"TestBorderLayout");
        
        // 组件-按钮
        Button east = new Button(label:"East");
       	Button west = new Button(label:"West");
        Button south = new Button(label:"South");
        Button north = new Button(label:"North");
        Button center = new Button(label:"Center");
        
        //把按钮添加上去
        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(east,BorderLayout.NORTH);
        frame.add(east,BorderLayout.CENTER);
        
        frame.setSize(widht:399, height:399);
        frame.setVisibale(true);
      }
    }
    
    • 表格布局
    public class TestBorderLayout{
      psvm(String[] args){
    		Frame frame = new Frame(tiele:"TestGridLayout");
        
        // 组件-按钮
        Button btn1 = new Button(label:"btn1");
       	Button btn2 = new Button(label:"btn2");
        Button btn3 = new Button(label:"btn3");
        Button btn4 = new Button(label:"btn4");
        Button btn5 = new Button(label:"btn5");
        Button btn6 = new Button(label:"btn6");
        
        //把按钮添加上去
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);
        
        frame.pack(); // java 函数
        frame.setVisibale(true);
      }
    }
    

    3、 Swing

  • 相关阅读:
    ESP8266 wifi钓鱼
    九,ESP8266 判断是断电上电(强制硬件复位)之后运行的内部程序还是内部软件复位之后运行的程序(基于Lua脚本语言)
    关于Http 传输二维json
    Android6.0权限大全和权限分类
    Android 多线程-----AsyncTask详解
    关于加密(转载文章)
    java基础之:匿名内部类
    系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    java多线程之:SynchronousQueue队列
    Hibernate之:各种主键生成策略与配置详解
  • 原文地址:https://www.cnblogs.com/coderzjz/p/13768513.html
Copyright © 2011-2022 走看看