AWT

AWT介绍

  • 包含了很多类和接口。用于GUI编程:图形用户界面编程
  • 元素:窗口,按钮,文本框
  • java.awt

组件和容器

Frame

package com.akita.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, 300);//设置背景颜色frame.setBackground(new Color(59, 63, 65));//设置弹出的初始位置frame.setLocation(200, 200);//设置大小固定frame.setResizable(false);}
}

运行效果

问题:发现窗口无法关闭

简单封装一下快速建立三个窗口

package com.akita.lesson01;import java.awt.*;public class TestFrame2 {public static void main(String[] args) {MyFrame myFrame = new MyFrame(100, 100, 200, 200, Color.blue);MyFrame myFrame1 = new MyFrame(400, 400, 200, 200, Color.red);MyFrame myFrame2 = new MyFrame(600, 600, 200, 200, Color.gray);}}class MyFrame extends Frame {static int id = 0;public MyFrame(int x, int y, int w, int h, Color color) {super("MyFrame" + (++id));setVisible(true);setBounds(x, y, w, h);setBackground(color);}
}

运行效果

面板

解决了窗口关闭事件

package com.akita.lesson01;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();//new一个面板Panel panel = new Panel();//设置布局frame.setLayout(null);//坐标frame.setBounds(300, 300, 500, 500);frame.setBackground(new Color(24, 154, 74));//panel设置坐标,相对于framepanel.setBounds(50, 50, 400, 400);panel.setBackground(new Color(243, 20, 20));//frame添加一个panelframe.add(panel);frame.setVisible(true);//监听事件,监听窗口关闭事件frame.addWindowListener(new WindowAdapter() {//窗口关闭要做的事情@Overridepublic void windowClosing(WindowEvent e) {super.windowClosing(e);System.exit(0);}});}
}

运行效果

三种布局管理器

流式布局

package com.akita.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");Button button4 = new Button("button4");//设置为流式布局frame.setLayout(new FlowLayout(FlowLayout.LEFT));frame.setSize(200, 200);//添加按钮frame.add(button1);frame.add(button2);frame.add(button3);frame.add(button4);frame.setVisible(true);}
}

运行效果

东南西北中(BorderLayout)

package com.akita.lesson01;import java.awt.*;public class TestBorderLayout {public static void main(String[] args) {Frame frame = new Frame("TestBorderLayout");frame.setBackground(new Color(1, 1, 1));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(500, 500);frame.setVisible(true);}
}

运行效果

表格布局

package com.akita.lesson01;import java.awt.*;public class TestGridLayout {public static void main(String[] args) {Frame frame = new Frame("TestGridLayout");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(200, 200);frame.setBackground(new Color(1, 1, 1));frame.setVisible(true);}
}

运行效果

总结

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到一个容器中
  3. 布局管理器:
  • 流式布局
  • 东西南北中
  • 表格布局
  1. 大小,定位,背景颜色,可见性,监听。
  2. 综合运用代码如下:
package com.akita.lesson01;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class TestHomework {public static void main(String[] args) {Frame frame = new Frame("布局的综合使用");frame.setSize(400, 400);frame.setLocation(300, 300);frame.setLayout(new GridLayout(2, 1));frame.setBackground(new Color(1, 1, 1));Panel panel1 = new Panel(new BorderLayout());Panel panel2 = new Panel(new GridLayout(2, 1));Panel panel3 = new Panel(new BorderLayout());Panel panel4 = new Panel(new GridLayout(2, 2));panel2.add(new Button("p2-btn-1"));panel2.add(new Button("p2-btn-2"));panel1.add(new Button("East-1"), BorderLayout.EAST);panel1.add(new Button("West-1"), BorderLayout.WEST);panel1.add(panel2, BorderLayout.CENTER);for (int i = 0; i < 4; i++) {panel4.add(new Button("p4-btn-" + i));}panel3.add(new Button("East-3"), BorderLayout.EAST);panel3.add(new Button("West-3"), BorderLayout.WEST);panel3.add(panel4, BorderLayout.CENTER);frame.add(panel1);frame.add(panel3);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});frame.setVisible(true);}
}

运行效果

事件监听

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

事件监听代码演示

package com.akita.lesson02;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class TestActionEvent {public static void main(String[] args) {//按下按钮,触发一些事件Frame frame = new Frame();frame.setBackground(new Color(1, 1, 1));frame.setSize(200, 200);windowClose(frame);Button button = new Button("test");//因为,addActionListener需要一个ActionListener,所以我们构造了一个ActionListenerMyActionListener myActionListener = new MyActionListener();button.addActionListener(myActionListener);frame.add(button, BorderLayout.CENTER);frame.setVisible(true);}private static void windowClose(Frame frame) {frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("aaaa");}
}

多个按钮共享一个事件监听器

package com.akita.lesson02;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class TestActionEvent2 {public static void main(String[] args) {Frame frame = new Frame("开始和停止");frame.setSize(400, 400);frame.setBackground(new Color(1, 1, 1));//两个按钮实现同一个监听Button button1 = new Button("开始");Button button2 = new Button("停止");//可以显示的定义触发返回的命令,如果不定义会走默认值//这个可以实现多个按钮只用一个监听类button2.setActionCommand("button2-stop");MyMonitor myMonitor = new MyMonitor();button1.addActionListener(myMonitor);button2.addActionListener(myMonitor);frame.add(button1, BorderLayout.NORTH);frame.add(button2, BorderLayout.SOUTH);frame.setVisible(true);}
}class MyMonitor implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {//获得按钮的信息System.out.println("按钮被点击了" + e.getActionCommand());}
}

输入框TextFiled的事件监听

package com.akita.lesson02;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class TestText01 {public static void main(String[] args) {MyFrame myFrame = new MyFrame();}
}class MyFrame extends Frame {public MyFrame() {setSize(400, 400);TextField textField = new TextField();add(textField);//监听这个文本框输入的文字MyActionListener2 myActionListener2 = new MyActionListener2();//按下回车,就会触发这个输入框的事件textField.addActionListener(myActionListener2);//替换文字的演示textField.setEchoChar('*');setVisible(true);}
}class MyActionListener2 implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {TextField textField = (TextField) e.getSource();   //获得一些资源System.out.println(textField.getText());//获得输入框中的文本textField.setText("");}
}

画笔

package com.akita.lesson02;import java.awt.*;public class TestPaint {public static void main(String[] args) {new MyPaint().loadFrame();}
}class MyPaint extends Frame {public void loadFrame() {setBounds(500, 200, 600, 400);setVisible(true);}@Overridepublic void paint(Graphics g) {//画笔需要有颜色,画笔可以画画g.setColor(Color.red);//g.drawOval(100, 100, 100, 100);g.fillOval(100, 100, 100, 100);g.setColor(Color.GREEN);g.fillRect(250, 250, 175, 100);//养成习惯,画笔用完,将它恢复成默认的颜色}
}

鼠标监听

package com.akita.lesson02;import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;//鼠标监听事件
public class TestMouseListener {public static void main(String[] args) {MyFrame frame = new MyFrame("画图");}
}class MyFrame extends Frame {//画画需要画笔,需要监听鼠标当前位置,需要集合来存储这些点private ArrayList points;public MyFrame(String title) {super(title);points = new ArrayList<>();setBounds(200, 200, 700, 550);this.addMouseListener(new MyMouseListener());setBackground(new Color(1, 1, 1));setVisible(true);}@Overridepublic void paint(Graphics g) {Iterator iterator = points.iterator();while (iterator.hasNext()) {Point point = (Point) iterator.next();g.setColor(Color.blue);g.fillOval(point.x, point.y, 10, 10);}}//添加一个点public void addpaint(Point point) {points.add(point);}private class MyMouseListener extends MouseAdapter {//鼠标点击  按下  弹起  按住不放@Overridepublic void mousePressed(MouseEvent e) {//按钮点击事件MyFrame myFrame = (MyFrame) e.getSource();//这个点是鼠标点击的点myFrame.addpaint(new Point(e.getX(), e.getY()));myFrame.repaint();}}
}

窗口监听

package com.akita.lesson02;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class TestWindow {public static void main(String[] args) {new WindowFrame();}
}class WindowFrame extends Frame {public WindowFrame() {setBounds(200, 200, 500, 350);setBackground(Color.CYAN);this.addWindowListener(new WindowAdapter() {//关闭窗口@Overridepublic void windowClosing(WindowEvent e) {System.out.println("windowClosing");}//激活窗口@Overridepublic void windowActivated(WindowEvent e) {System.out.println("windowActivated");}});setVisible(true);}
}

键盘监听

package com.akita.lesson02;import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;//按键监听
public class TestKeyListener {public static void main(String[] args) {new KeyFrame();}
}class KeyFrame extends Frame {public KeyFrame() {setBounds(200, 200, 600, 450);setBackground(Color.CYAN);this.addKeyListener(new KeyAdapter() {//键盘按下触发@Overridepublic void keyPressed(KeyEvent e) {//获得键盘上的哪个按键按下,获得当前按键按下的键盘码int keyCode = e.getKeyCode();     //不需要记住键盘码,使用KeyEvent.VK_UP静态属性System.out.println(keyCode);if (keyCode == KeyEvent.VK_UP) {System.out.println("你按下了上键");}//根据不同的按键,产生不同的结果}});setVisible(true);}
}

Java的GUI编程---AWT介绍相关推荐

  1. Java的GUI编程---Swing介绍

    Swing 窗口,面板 package com.akita.lesson03;import javax.swing.*; import java.awt.*;public class JFrameDe ...

  2. Java之GUI编程学习笔记六 —— AWT相关(画笔paint、鼠标监听事件、模拟画图工具)

    Java之GUI编程学习笔记六 -- AWT相关(画笔paint) 参考教程B站狂神https://www.bilibili.com/video/BV1DJ411B75F 了解paint Frame自 ...

  3. Java基础-GUI入门-AWT详解

    AWT 2.1.AWT介绍 其包含了很多的类和接口. 元素:窗口,按钮,文本框. Java.awt: 2.2.组件和容器 1.Frame 代码: package com.edwin.lession01 ...

  4. Java:GUI编程

    文章目录 GUI编程 AWT 一.AWT介绍 二.组件和容器(Component和Container) 2.1.Frame 2.2.Panel 三.布局管理器 3.1.第一种布局管理器--FlowLa ...

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

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

  6. 13.JAVA之GUI编程将程序打包jar

    jar基本命令: 目标:将下列MyMenuDemo.java代码打包成jar. 方法如下: 1.把java代码放到d:\myclass目录下. 2.按下快捷键ctrl+r,打开运行窗口,输入cmd后回 ...

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

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

  8. pythonguitkinter编程入门_Python Tkinter GUI编程入门介绍

    一.Tkinter介绍 Tkinter是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口.Tkinter不是唯一的python图形编程接口,但是是其中比较流行的一个 ...

  9. python的gui编程用途_Python19-03_GUI编程----GUI编程的介绍(第一个程序)

    GUI编程的介绍(第一个程序) GUI图形用户界面编程 我们前面实现的都是基于控制台程序, 程序和用户交互通过控制台来完成. GUI(Graphics User Interfance)即图形用户界面编 ...

最新文章

  1. CSS-3 Animation 的使用
  2. SourceTree安装破姐添加SSH KEY以及拉取代码教程(附资源下载)
  3. UVA - 400 Unix ls
  4. python web开发 网络编程 HTTP协议、Web服务器、WSGI接口
  5. 匹配正则_程序员入门基础:python正则表达式贪婪匹配和非贪婪匹配
  6. java代码鸟飞_180行原生js代码实现简易版飞行的小鸟游戏
  7. python3 抽象基类 abc.abstractmethod
  8. 一起学设计模式 - 桥接模式
  9. 浏览器渲染页面的原理及流程---------重绘与重排(回流)--优化
  10. 通达OA 太牛了!工作流表单设计中级联菜单原来可以这样实现(图文)
  11. avr单片机教程 csdn_从古老的attiny85升级到新的AVR 1系列attiny412教程
  12. JS打开新页面的两种方式:当前页面打开和新页面打开
  13. 2022年T电梯修理报名考试及T电梯修理最新解析
  14. 在Excel中快速选择数据
  15. SNIP算法详解(极端尺寸目标检测)
  16. html文本框自动宽度,input文本框宽度自适应
  17. UVa 11223 - O: dah dah dah!
  18. cent7 安装 notepadqq
  19. pwd python 安装 模块_在windows上安装pwd模块时出错
  20. 小米6-谷歌全家桶安装

热门文章

  1. 张正友标定法几个坐标系的意思
  2. synplify pro 201203分享下载
  3. iOS CoreAnimation专题——实战篇(四)基于拖动手势的视图3D旋转效果
  4. 启用数据空间:让VirtualBox虚拟机中的Ubuntu 10.10和XP主机互通有无
  5. 电路仿真软件LTspice 使用教程
  6. 华为一碰传nfc标签_一碰传连接失败,触碰标签无反应
  7. 长生生物事件的反思:质量是生命
  8. DataGrip入门小tips
  9. java是编译型语言还是解释型语言?
  10. 嘀嘀打车起家的辛苦历程