随笔- 37  文章- 0  评论- 0  阅读- 1013 

GUI编程

组件

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

1.简介

GUI的核心技术 Swing AWT

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

为什么需要学习?

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

2.AWT

2.1Awt介绍

  1. 包含了很多类和接口! GUI:

  2. 元素:窗口,按钮,文本框

  3. java.awt

image-20220504152407011

component 组件

botton 按钮

TextArea 文本域

Label 标签

container 容器

paner 面板

frame 框架

dialog 弹窗

Applet 小程序

2.2 组件和容器

1. Frame

package com.yu.lesson01;

import java.awt.*;

public abstract class TestFrame {
    public static void main(String[] args) {

        //Frame 不需要看JDK,按住CTRL单击对象名直接看源码即可
        Frame frame = new Frame("我的第一个Java图像窗口");
        //需要设置可见性
        frame.setVisible(true);

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

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

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

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


}

image-20220504152916546

窗口无法关闭?

​ 结束程序运行即可

多窗口

package com.yu.lesson01;

import java.awt.*;

public class Test2 {
    public static void main(String[] args) {
        Myframe myframe = new Myframe(100, 100, 200, 200, Color.CYAN);
        Myframe myframe1 = new Myframe(300, 100, 200, 200, Color.yellow);
        Myframe myframe2 = new Myframe(100, 300, 200, 200, Color.red);
        Myframe myframe3 = new Myframe(300, 300, 200, 200, Color.pink);

    }
}
class Myframe extends Frame{
    static  int id = 0;
    public Myframe(int x, int y, int w, int h, Color color){
        super("Myframe+"+(++id));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);

    }
}

image-20220504154118950

2.Panel 面板

package com.yu.lesson01;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

//Panel 可以看成是一个空间,但不能单独存在
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(new Color(0x00FF99));

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

        frame.add(panel);

        frame.setVisible(true);

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

}

image-20220504190721077

问题
  1. 在调用对象时没有的话可以自己调包即可.
  2. 结束程序,用的方法不要选错,我写成了这样
//窗口已打开
frame.addWindowListener(new WindowAdapter(){
    @Override
    public void windowOpened(WindowEvent e) {
        //结束程序
        System.exit(0);
        System.out.println("2222");
    }
});

造成结果,无法执行结束命令,敲代码时要细心

3.布局管理器

  • 流式布局 FlowLayout

    • 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(FlowLayout.TRAILING));
              //将想要布局的方向FlowLayout.放到这即可
      //        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
      
              frame.setSize(200,200);
              //将按钮添加上去
              frame.add(button1);
              frame.add(button2);
              frame.add(button3);
              frame.setVisible(true);
          }
      
  • 东西南北中

    • east 东

    • west 西

    • south 南

    • north 北

    • 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(400,400);
          frame.setVisible(true);
          //关闭
          frame.addWindowListener(new WindowAdapter(){
              @Override
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });
      }
      
  • 表格布局 Grid

    •  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");
      
              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.setSize(400,400);
              //自动最小展示
              frame.pack();
              frame.setVisible(true);
              frame.addWindowListener(new WindowAdapter(){
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
          }
      

练习

image-20220504210050495

public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,400);
//        frame.setBackground(Color.BLACK);
        frame.setLayout((new GridLayout(2,1)));
        //四个面板
        Panel pane1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2, 1));
        Panel pane3 = new Panel(new BorderLayout());
        Panel panel4= new Panel(new GridLayout(2, 2));
        //上面
        pane1.add(new Button("East-1"),BorderLayout.EAST);
        pane1.add(new Button("west-1"),BorderLayout.WEST);
        panel2.add(new Button("p2-btn-1"));
        panel2.add(new Button("p2-btn-2"));
        pane1.add(panel2,BorderLayout.CENTER);
        //下面
        pane3.add(new Button("East-2"),BorderLayout.EAST);
        pane3.add(new Button("west-2"),BorderLayout.WEST);
        //中间4
        panel4.add(new Button("p4-btn-1"));
        panel4.add(new Button("p4-btn-2"));
        panel4.add(new Button("p4-btn-3"));
        panel4.add(new Button("p4-btn-4"));
        pane3.add(panel4,BorderLayout.CENTER);
        frame.setVisible(true);
        frame.add(pane1);
        frame.add(pane3);
        frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

总结

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

3.Swing

 posted on   yulll  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示