GUI编程--1

  • GUI是什么 (Graphical User Interface),即用户图形界面编程。
  • 怎么玩
  • 平时怎么运用

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件

1.简介

GUI技术的核心:Swing AWT,因为界面不美观,需要jre环境! 所以不流行。

为啥要学习?

  1. 写出一些想要的小工具。
  2. 工作时候,可能需要维护到swing界面,很低概率。
  3. 了解MVC架构,了解监听。

2.AWT

​ AWT(Abstract Window Toolkit),中文译为抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的图形用户界面的基本工具。

2.1 AWT介绍

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

组件(component):

1. button(按钮)、TextArea(文本框)、Label(标签)....
1. 容器(Container):1中的需要放到里面

​ 2.1. Window(窗口) : Frame(框架)、Dialog(弹窗)

​ 2.2. Panel(面板):Applet(小程序)

2.2 窗口--Frame框架

1.第一个frame窗口

package com.ssl.lesson01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("我的第一个java图形界面"); //设置可见性
frame.setVisible(true); //设置窗口大小
frame.setSize(400,400); //设置背景颜色
frame.setBackground(new Color(22, 222, 93)); //弹出的初始位置
frame.setLocation(400,400); //设置大小固定
frame.setResizable(false); }
}

问题:发现窗口关闭不掉!!

2.生成多个窗口

package com.ssl.lesson01;

import com.sun.beans.editors.ColorEditor;

import java.awt.*;

public class TestFrame2 {
public static void main(String[] args) {
new MyFrame(0,400,400,400,Color.red);
new MyFrame(400,400,400,400,Color.blue);
new MyFrame(800,400,400,400,Color.green);
new MyFrame(1200,400,400,400,Color.orange);
}
} //利用继承写自己的方法。
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);
//setLocation(x,y);
//setSize(w,h);
setVisible(true);
setBackground(color);
setResizable(false);
}
}

2.3 窗口--面板Panel

面板需要添加到frame中,其中解决了窗口关闭的事件监听。

package com.ssl.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.event.WindowListener; //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(13, 234, 92)); //panel 设置坐标 相对于frame
panel.setBounds(50,50,400,400); //panel 设置颜色
panel.setBackground(Color.red); //将面板Panel放到frame上
frame.add(panel); frame.setVisible(true); //添加监听事件,监听窗口关闭事件,System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时要做的事情。
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}

2.4 三种布局管理器

frame.setLayout(null);
  • 流式布局Flow
package com.ssl.lesson01;

import java.awt.*;

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.setLayout(new FlowLayout(FlowLayout.LEFT)); //左
//frame.setLayout(new FlowLayout(FlowLayout.CENTER)); //中
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //右 frame.setSize(800,800); frame.setVisible(true); frame.add(button1);
frame.add(button2);
frame.add(button3); }
}
  • 东西南北中布局border
package com.ssl.lesson01;

import java.awt.*;

public class TestBorderLayout {
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.setVisible(true);
frame.setSize(800,400); }
}
  • 表格布局Grid
package com.ssl.lesson01;

import java.awt.*;

public class TestGridLayout {
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");
Button button5 = new Button("button5");
Button button6 = new Button("button6"); frame.setLayout(new GridLayout(3,2)); frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6); frame.setSize(400,400); frame.setVisible(true); }
}

2.5 小练习

package com.ssl.lesson01;

import java.awt.*;

public class TestTest {
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");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
Button button7 = new Button("button7");
Button button8 = new Button("button8");
Button button9 = new Button("button9");
Button button10 = new Button("button10"); frame.setLayout(new GridLayout(2,3)); Panel panel1 = new Panel();
Panel panel2 = new Panel(); frame.add(button1);
frame.add(panel1);
panel1.setLayout(new GridLayout(2,1));
panel1.add(button2);
panel1.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(panel2);
panel2.setLayout(new GridLayout(2,2));
panel2.add(button6);
panel2.add(button7);
panel2.add(button8);
panel2.add(button9);
frame.add(button10); frame.setSize(400,400); frame.setVisible(true); }
}

总结

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中
  3. 布局管理器

GUI编程--1的更多相关文章

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

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

  2. 1.JAVA之GUI编程概述

          下列内容为本人看毕向东老师java视频教程学习笔记! JAVA GUI图形用户界面编程: Windows 操作系统提供两种操作方式:                             ...

  3. 2.JAVA之GUI编程布局

    布局管理器 容器中的组件排放方式,就是布局 常见的布局管理器: **************************************************** 1.FlowLayout(流式 ...

  4. 3.JAVA之GUI编程Frame窗口

    创建图形化界面思路: 1.创建frame窗体: 2.对窗体进行基本设置: 比如大小.位置.布局 3.定义组件: 4.将组件通过add方法添加到窗体中: 5.让窗体显示,通过setVisible(tur ...

  5. 4.JAVA之GUI编程事件监听机制

    事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...

  6. 5.JAVA之GUI编程窗体事件

    我们回顾下第三篇时的内容: 在3.JAVA之GUI编程Frame窗口中窗体是无法直接关闭的,想要关闭须进程管理器结束进程方式关掉. 现在我们就来解决下这个问题. ******************* ...

  7. 6.JAVA之GUI编程Action事件

    功能:单击一个按钮实现关闭窗口: import java.awt.*; import java.awt.event.*; public class StudyAction { // 定义该图形所需的组 ...

  8. 7.JAVA之GUI编程鼠标事件

    鼠标事件: 功能: 1.基本窗体功能实现 2.鼠标移动监听,当鼠标移动到按钮上时,触发打印事件. 3.按钮活动监听,当按钮活动时,触发打印事件. 4.按钮被单击时触发打印事件. 源码如下: impor ...

  9. 8.JAVA之GUI编程键盘码查询器

    程序使用说明: 1.本程序由于是java代码编写,所以运行需安装jdk并配置好环境变量. 2. 复制java代码到记事本内,另存为Keyboard_events.java: 3.复制批处理代码到记事本 ...

  10. 9.JAVA之GUI编程列出指定目录内容

    代码如下: /*列出指定目录内容*/ import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import ...

随机推荐

  1. MVC内置对象

    MVC内置函数 ----HTML页 <!DOCTYPE html> <html> <head>     <meta charset="utf-8&q ...

  2. 五一训练包E-5

    题目链接:https://vjudge.net/contest/436484#problem/E 题目的大致意思就是给俩数,分别是小数组的大小N和数目K,给的数组是递增的,方便后续的判断,将大数组分成 ...

  3. ubuntu | virtualbox报错:不能为虚拟电脑打开一个新任务

    百度了几个办法 都不行. 还得是gxd,说在vmware虚拟机设置勾上这个就行了

  4. 文献学习——A Deep Dive into Conflict Generating Decisions

    A Deep Dive into Conflict Generating Decisions Md. Solimul Chowdhury, Martin Müller, Jia-Huai You:A  ...

  5. Linux基础知识2

    目录和文件管理 linux以目录形式挂载(通过目录访问存储设备)文件系统,目录结构分层的树形结构. 链接:在共享文件和访问它的用户的若干目录项之间建立联系的方法,包括硬链接和软链接两种方式 linux ...

  6. go语言读取文件的简单使用

    注意:打开文件记得一定要关闭 file, err := os.Open("文件名称") defer file.Close() 一.打开文件 1. file, err := os.O ...

  7. 8.Vuex状态管理

    一.Vuex 概述 1.1 组件之间共享数据的方式 父传子: v-bind 属性绑定 子传父: v-on 事件绑定 兄弟组件之间共享数据: EventBus $on 接收数据的那个组件 (数据接收方) ...

  8. openssl 全面支持国密SM2/SM3/SM4加密算法

    sm4展示 代码 /** 文件名: https://github.com/liuqun/openssl-sm4-demo/blob/cmake/src/main.c */ #include <s ...

  9. Jmeter读取Csv文件,字段中有逗号分隔,读取不成功

    Jmeter读取Csv文件,字段中有逗号分隔,读取不成功

  10. HTML笔记(二) HTML标签元素

    一 常用的头部元素标签 <head>元素包含了所有的头部标签元素. 1.<title> <title>标签定义了HTML文档的标题,在HTML/XHTML文档中是必 ...