吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1499|回复: 1
收起左侧

[Java 转载] GUI编程AWT,面板,布局管理器笔记

[复制链接]
黑白客 发表于 2021-2-9 12:19

GUI编程
GUI编程
1.简介
2.AWT
面板Panel
布局管理器
事件监听

GUI编程

  • 告诉大家该怎么学?
    • 这是什么
    • 它怎么玩
    • 该如何去在我们平时运用
  • 组件
    • 窗口
    • 弹窗
    • 面板
    • 文本框
    • 列表框
    • 按钮
    • 图片
    • 监听事件
    • 鼠标
    • 键盘事件
    • 破解工具

1.简介

  • Gui的核心技术:Swing AWT
    • 因为界面不美观
    • 需要jre环境 ---逐渐被淘汰
  • 为什么我们要学习
    • 可以写出自己心中想要的一些小工具
    • 工作的时候,也可能需要维护到swing界面,概率极小
    • 了解MVC架构,了解监听!

2.AWT

  • Awt介绍

    • 包含了很多类和接口!GUI:图形用户界面
    • 元素:窗口,按钮,文本框
    • java.awt
    • 组件:包含一些基本组件(button TextArea  Label ...) 还有容器(Container)基本组件就是存放在容器中
  • 组件和容器

    package com.wang.lesson01;
    
    import java.awt.*;
    
    /**
    * @Auther: 王海新
    * @Date: 2021/2/6 11:21
    * @Description: GUUI的第一个界面
    */
    public class TestFrame {
      public static void main(String[] args) {
    
          //Frame,JDK,看源码(不懂了就直接点进去看源码就可以)
          Frame frame = new Frame("我的第一个java图形界面窗口");
    
          //需要设置可见性
          frame.setVisible(true);
    
          //设置窗口大小
          frame.setSize(400,400);
    
          //设置背景颜色
          frame.setBackground(new Color(104, 135, 219));
    
          //设置初始化位置
          frame.setLocation(200,200);
    
          //设置大小固定
          //frame.setResizable(false);
      }
    }
    

    image-20210206112937251

    回顾封装:

    package com.wang.lesson01;
    
    import java.awt.*;
    
    /**
    * @Auther: 王海新
    * @Date: 2021/2/6 11:44
    * @Description:
    */
    public 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);
      }
    }
package com.wang.lesson01;

import java.awt.*;

/**
 * @Auther: 王海新
 * @Date: 2021/2/6 11:40
 * @Description: 展示多个窗口 new
 */
public class TestFrame2 {
    public static void main(String[] args) {
    new MyFrame(100,100,200,200,Color.BLACK);
    new MyFrame(300,100,200,200,Color.BLUE);
    new MyFrame(100,300,200,200,Color.yellow);
    new MyFrame(300,300,200,200,Color.red);
    }
}

面板Panel

package com.wang.lesson01;

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

/**
 * @Auther: 王海新
 * @Date: 2021/2/6 16:17
 * @Description:
 */
public class TestPannel {
    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(85, 186, 85));

        //panel 设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(193,15,63));

        //frame.add(panel)
        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件System.exit(0)  0 是正常关闭,1是有异常的关闭
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

问题:发现窗口关闭不了

package com.wang.lesson01;

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

/**
 * @Auther: 王海新
 * @Date: 2021/2/6 16:17
 * @Description:
 */
public class TestPannel {
    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(85, 186, 85));

        //panel 设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(193,15,63));

        //frame.add(panel)
        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件System.exit(0)  0 是正常关闭,1是有异常的关闭
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

布局管理器

  • 流式布局
package com.wang.lesson01;

import java.awt.*;

/**
 * @Auther: 王海新
 * @Date: 2021/2/6 17:02
 * @Description:流式布局
 */
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.setSize(200,200);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

    }
}
  • 东南西北中

    package com.wang.lesson01;
    
    import java.awt.*;
    
    /**
    * @Auther: 王海新
    * @Date: 2021/2/7 18:03
    * @Description: 东西南北中
    */
    public class TestBorderLayout {
      public static void main(String[] args) {
          //创建了一个面板
          Frame frame = new Frame("TestBorderLayout");
          //创建了东西南北中四个按钮
          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);
      }
    }
  • 表格布局Grid

    package com.wang.lesson01;
    
    import java.awt.*;
    
    /**
    * @Auther: 王海新
    * @Date: 2021/2/7 18:16
    * @Description: 表格布局
    */
    public class TestGridLayout {
      public static void main(String[] args) {
          Frame frame = new Frame("TestGridLayout");
    
          //创建了东西南北中四个按钮
          Button but1 = new Button("but1");
          Button but2 = new Button("but2");
          Button but3 = new Button("but3");
          Button but4 = new Button("but4");
          Button but5 = new Button("but5");
          Button but6 = new Button("but6");
    
          frame.setLayout(new GridLayout(3,2));
    
          frame.add(but1);
          frame.add(but2);
          frame.add(but3);
          frame.add(but4);
          frame.add(but5);
          frame.add(but6);
    
          frame.pack();//java函数
          frame.setVisible(true);
      }
    }
  • 总结:小demo

    image-20210208151738160

    制作这样一个面板

    package com.wang.lesson01;
    
    import java.awt.*;
    
    /**
    * @Auther: 王海新
    * @Date: 2021/2/8 14:50
    * @Description:  小结作业。
    */
    public class ExDemo {
      public static void main(String[] args) {
          //总 Frame
          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));
    
          //四个面板
          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));
          //上面ok
          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);
          //下面OK
          p3.add(new Button("East-1"),BorderLayout.EAST);
          p3.add(new Button("West-1"),BorderLayout.WEST);
          for (int i = 0; i < 4; i++) {
              p4.add(new Button("for-" + i));
          }
          p3.add(p4,BorderLayout.CENTER);
    
          frame.add(p1);
          frame.add(p3);
                  frame.addWindowListener(new WindowAdapter() {
              @Override
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });
      }
    }

    总结:

    1. Frame是一个顶级窗口
    2. Panel无法单独显示,必须添加到某个容器中。
    3. 布局管理器
      1. 流式
      2. 东西南北中
      3. 表格
    4. 大小,定位,背景颜色,可见性,监听!

事件监听

事件监听:当某个事情发生的时候,干什么?

package com.wang.lesson01;

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

/**
 * @Auther: 王海新
 * @Date: 2021/2/8 15:32
 * @Description:  监听器
 */
public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();
        //因为addActionListener();需要一个ActionListener(),所以我们去构造一个
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

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

class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aa");
    }
}

多个按钮,共享一个监听

package com.wang.lesson01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Auther: 王海新
 * @Date: 2021/2/9 11:54
 * @Description: 两个按钮公用一个监听器
 */
public class TestActionTwo {
    public static void main(String[] args) {
        // 两个按钮,实现同一个监听器
        // 开始   停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("start");

        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
        //可以多个按钮,只写一个监听器。
        button2.setActionCommand("button-stop");

        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        //自适应
        frame.pack();
        //显示面板
        frame.setVisible(true);
    }
}

class MyMonitor implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了:msg"+ e.getActionCommand());
        if (e.getActionCommand().equals("start")) {
            //这里实现某个按钮的单独功能
        }
    }
}

免费评分

参与人数 2吾爱币 +2 热心值 +1 收起 理由
默君 + 1 我很赞同!
沙华云乐 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

E式丶男孩 发表于 2021-2-9 13:02
使用maven构建的话会好点吧,就不用搞那么多包来实现想要的功能了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-5 05:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表