GUI编程—AWT
2020-01-15 11:31
896 查看
1、简介
Gui的核心技术: Swing AWT
- 因为界面不美观。
- 需要 jre 环境!
2、AWT
2.1、Awt定义
Awt(Abstract Window Toolkit)全称抽象窗口工具集。是基本的GUI类库,AWT也是窗口框架,包含不同平台的窗口系统改中的公共组件。
Awt程序执行时,通过JVM调用OS本地的图形界面来创建与平台一致的对等体。
优点:1. 界面风格与OS窗口 2. 一次编写,多平台运行
缺点:界面丑陋,功能有限
2.2、组件和容器
1、Frame案例
问题: 发现窗口关闭不掉,可以通过停止java程序!
package com.kuang.lesson01; import java.awt.*; //GUI的第一个界面 public class TestFrame { public static void main(String[] args) { //Frame,JDK, 看源码! Frame frame = new Frame("我的第一个Java图像界面窗口"); //需要设置可见性 w h frame.setVisible(true); //设置窗口大小 frame.setSize(400,400); //设置背景颜色 Color frame.setBackground(new Color(85, 150, 68)); //弹出的初始位置 frame.setLocation(200,200); //设置大小固定 frame.setResizable(false); } }
展示多个窗口
package com.kuang.lesson01; import java.awt.*; public class TestFrame2 { public static void main(String[] args) { //展示多个窗口 new MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue); MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow); MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red); MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.MAGENTA); } } 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); } }
2、面板Panel
解决了关闭事件!
frame.addWindowListener(new WindowAdapter() { //窗口点击关闭的时候需要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } });
package com.kuang.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(40, 161, 35)); //panel设置坐标,相对于frame panel.setBounds(50,50,400,400); panel.setBackground(new Color(193, 15, 60)); //frame.add(panel) frame.add(panel); frame.setVisible(true); //监听事件,监听窗口关闭事件 System.exit(0) //适配器模式 : frame.addWindowListener(new WindowAdapter() { //窗口点击关闭的时候需要做的事情 @Override public void windowClosing(WindowEvent e) { //结束程序 System.exit(0); } }); } }
2.3、布局管理器
-
流式布局FlowLayout()
package com.kuang.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.RIGHT)); frame.setSize(200,200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中BoderLayout()
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); } }
public class TestBorderLayout { //放在画板上的东西南北中 public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); frame.setSize(400,300); frame.setLocation(300,400); frame.setBackground( Color.BLUE); frame.setVisible(true); 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"); Panel panel = new Panel(); panel.setLayout(new BorderLayout()); panel.add(east,BorderLayout.EAST); panel.add(west,BorderLayout.WEST); panel.add(south,BorderLayout.SOUTH); panel.add(north,BorderLayout.NORTH); panel.add(center,BorderLayout.CENTER); frame.add(panel); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); /* 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);*/ } }
-
表格布局 GridLayout()
package com.kuang.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.pack(); //Java函数! frame.setVisible(true); } }
public class TestGridLayout { //画在画板上表格布局 public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); Panel panel = new Panel(); panel.setLayout(new GridLayout(3,2)); 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));*/ panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5); panel.add(btn6); frame.add(panel); frame.pack(); //Java函数! frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
课堂练习:
分析过程:
注意:
在写代码前要先思考程序的架构,在去用代码实现,切勿直接动手。
按钮命名时要统一字符长度,要用英文命名。
代码实现:
package com.kuang.lesson01; import java.awt.*; //练习的demo讲解 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)); //4个面板 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); //下面 p3.add(new Button("East-2"),BorderLayout.EAST); p3.add(new Button("West-2"),BorderLayout.WEST); //中间4个 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是一个顶级窗口
-
Panel 无法单独显示,必须添加到某个容器中。
-
布局管理器
流式 - 东西南北中
- 表格
大小,定位,背景颜色,可见性,监听!
5.注意:当将按钮放在Panel上时,Frame就不用再写布局方式,因为将Panel放在Frame里面,只需要设置Panel的布局方式即可。
2.4、事件监听
事件监听:当某个事情发生的时候,干什么?
package com.kuang.lesson02; import javax.swing.*; 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(); Button button = new Button(); //因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button,BorderLayout.CENTER); frame.pack(); windowClose(frame); //关闭窗口 frame.setVisible(true); } //关闭窗体的事件 private 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("aaa"); } }
多个按钮,共享一个事件
package com.kuang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestActionTwo { public static void main(String[] args) { // 两个按钮,实现同一个监听 // 开始 停止 Frame frame = new Frame("开始-停止"); Button button1 = new Button("start"); Button button2 = new Button("stop"); //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值! //可以多个按钮只写一个监听类 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.pack(); frame.setVisible(true); } } class MyMonitor implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // e.getActionCommand() 获得按钮的信息 System.out.println("按钮被点击了:msg=> "+e.getActionCommand()); if (e.getActionCommand().equals("start")){ } } }
2.5、输入框 TextField 监听
package com.kuang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestText01 { public static void main(String[] args) { //启动! new MyFrame(); } } class MyFrame extends Frame{ public MyFrame(){ TextField textField = new TextField(); add(textField); //监听这个文本框输入的文字 MyActionListener2 myActionListener2 = new MyActionListener2(); //按下enter 就会触发这个输入框的事件 textField.addActionListener(myActionListener2); //设置替换编码 textField.setEchoChar('*'); setVisible(true); pack(); } } class MyActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField field = (TextField) e.getSource(); //获得一些资源,返回的一个对象 System.out.println(field.getText()); //获得输入框的文本 field.setText(""); //null "" } }
2.6、简易计算器,组合+内部类回顾复习!
oop原则:组合,大于继承!
目前代码
package com.kuang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class TestCalc { public static void main(String[] args) { new Calculator(); } } //计算器类 class Calculator extends Frame{ public Calculator(){ //3 个文本框 TextField num1 = new TextField(10);//字符数 TextField num2 = new TextField(10);//字符数 TextField num3 = new TextField(20);//字符数 //1 个按钮 Button button = new Button("="); button.addActionListener(new MyCalculatorListener(num1,num2,num3)); //1 个标签 Label label = new Label("+"); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //监听器类 class MyCalculatorListener implements ActionListener{ //获取三个变量 private TextField num1,num2,num3; public MyCalculatorListener(TextField num1,TextField num2,TextField num3) { this.num1 = num1; this.num2 = num2; this.num3 = num3; } @Override public void actionPerformed(ActionEvent e) { //1. 获得加数和被加数 int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); //2. 将这个值 + 法运算后,放到第三个框 num3.setText(""+(n1+n2)); //3. 清除前两个框 num1.setText(""); num2.setText(""); } }
完全改造为面向对象写法
package com.kuang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); } } //计算器类 class Calculator extends Frame{ //属性 TextField num1,num2,num3; //方法 public void loadFrame(){ num1 = new TextField(10);//字符数 num2 = new TextField(10);//字符数 num3 = new TextField(20);//字符数 Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener(this)); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //监听器类 class MyCalculatorListener implements ActionListener{ //获取计算器这个对象,在一个类中组合另外一个类; Calculator calculator = null; public MyCalculatorListener(Calculator calculator) { this.calculator = calculator; } @Override public void actionPerformed(ActionEvent e) { //1. 获得加数和被加数 //2. 将这个值 + 法运算后,放到第三个框 //3. 清除前两个框 int n1 = Integer.parseInt(calculator.num1.getText()); int n2 = Integer.parseInt(calculator.num2.getText()); calculator.num3.setText(""+(n1+n2)); calculator.num1.setText(""); calculator.num2.setText(""); } }
内部类:
- 更好的包装
package com.kuang.lesson02; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //简易计算器 public class TestCalc { public static void main(String[] args) { new Calculator().loadFrame(); } } //计算器类 class Calculator extends Frame{ //属性 TextField num1,num2,num3; //方法 public void loadFrame(){ num1 = new TextField(10);//字符数 num2 = new TextField(10);//字符数 num3 = new TextField(20);//字符数 Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener()); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } //监听器类 //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法! private class MyCalculatorListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //1. 获得加数和被加数 //2. 将这个值 + 法运算后,放到第三个框 //3. 清除前两个框 int n1 = Integer.parseInt(num1.getText()); int n2 = Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); num1.setText(""); num2.setText(""); } } }
2.7、画笔
package com.kuang.lesson03; import java.awt.*; import java.util.Date; public class TestPaint { public static void main(String[] args) { new MyPaint().loadFrame(); } } class MyPaint extends Frame{ public void loadFrame(){ setBounds(200,200,600,500); setVisible(true); } //画笔 @Override public 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(150,200,200,200); //养成习惯,画笔用完,将他还原到最初的颜色 } }
2.8、鼠标监听
目的:想要实现鼠标画画!
package com.kuang.lesson03; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Iterator; //鼠标监听事件 public class TestMouseListener { public static void main(String[] args) { new MyFrame("画图"); } } //自己的类 class MyFrame extends Frame{ //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点 ArrayList points; public MyFrame(String title) { super(title); setBounds(200,200,400,300); //存鼠标点击的点 points = new ArrayList<>(); setVisible(true); //鼠标监听器,正对这个窗口 this.addMouseListener(new MyMouseListener()); } @Override public 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{ //鼠标 按下,弹起,按住不放 @Override public void mousePressed(MouseEvent e) { MyFrame frame = (MyFrame) e.getSource(); //这个我们点击的时候,就会在界面上产生一个点!画 //这个点就是鼠标的点; frame.addPaint(new Point(e.getX(),e.getY())); //每次点击鼠标都需要重新画一遍 frame.repaint();//刷新 } } }
2.9、窗口监听
package com.kuang.lesson03; 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(){ setBackground(Color.blue); setBounds(100,100,200,200); setVisible(true); //addWindowListener(new MyWindowListener()); this.addWindowListener( //匿名内部类 new WindowAdapter() { //关闭窗口 @Override public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); System.exit(0); } //激活窗口 @Override public void windowActivated(WindowEvent e) { WindowFrame source = (WindowFrame) e.getSource(); source.setTitle("被激活了"); System.out.println("windowActivated"); } } ); } }
2.10、键盘监听
public class TextKeyFrame { public static void main(String[] args) { new MyKeyFrame(); } } class MyKeyFrame extends Frame{ public MyKeyFrame(){ setBounds(1,2,300,400); setVisible(true); this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { char keyCode = (char) e.getKeyCode(); System.out.println(keyCode); } }); } }
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- Java GUI编程中AWT/swing/SWT的优缺点
- Java GUI编程中AWT/swing/SWT的优缺点
- 基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类
- 基于AWT、Swing的GUI编程 - 多个命令共享同一个监听器类
- Java界面编程(GUI,AWT)
- Java GUI编程中AWT/Swing/SWT的优缺点
- Java GUI编程中AWT/swing/SWT的优缺点
- Java GUI编程中AWT/swing/SWT的优缺点
- GUI编程(AWT)
- Java的GUI编程(1)--- AWT
- JavaGUI编程之awt和swing包的概述
- Python的GUI编程(九)Menu(菜单)OptionMenu(为可选菜单)
- java的GUI界面编程
- java之 22天 GUI 图形界面编程(一)
- java笔记-GUI编程
- Python的GUI编程
- JAVA GUI 编程指南
- python应用系列教程——python的GUI界面编程Tkinter全解
- 【java编程】GUI之综合练习(二)
- java语言gui编程之内部类和鼠标事件经典2之两个问题的解决