首页 技术 正文
技术 2022年11月12日
0 收藏 903 点赞 4,428 浏览 3141 个字

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); }
    }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,107
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,583
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,430
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,201
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919