AWT


1.简介

GUI的核心技术:Swing AWT(使用的较少)

原因

  1. 界面不美观

  2. 需要JRE环境

学习原因

  1. 可以写出自己想要的一些工具

  2. 工作的时候,可能需要维护到Swing界面,小概率事件

  3. 了解MVC架构,了解监听

AWT

AWT介绍

  1. 包含了很多类和接口 GUI:图形用户界面

Eeclipes :java写的(很老了)

  1. 元素:窗口、按钮、文本框

  2. java.awt包

窗口

import java.awt.*;

public class Frame01 {
  public static void main(String[] args) {
      //Frame
  //     public Frame() throws HeadlessException{//源码
//               public Frame(String title) throws HeadlessException {
//               init(title, null);
//           }
      Frame frame=new Frame("阿娆");
      //需要设置可见性操作
      frame.setVisible(true);
      //设置窗口大小
      frame.setSize(400,440);
      //设置颜色
      frame.setBackground(new Color(218, 102, 250));
      //弹出的初始位置
      frame.setLocation(200,020);
      //设置大小固定
      frame.setResizable(false);//true可以改变窗口大小
  }
}

image-20211202150552660

不能点击×关闭,可以用停止运行关闭窗口

可以自己做一个Frame减少代码量,通过调用来建立窗口 class继承

import java.awt.*;

public class Frame02 {
  public static void main(String[] args) {
      KimiFrame kimiFrame1=new KimiFrame(100,100,400,400,Color.blue);
      KimiFrame kimiFrame2=new KimiFrame(400,400,400,400,Color.red);
      KimiFrame kimiFrame3=new KimiFrame(100,400,400,400,Color.black);
      KimiFrame kimiFrame4=new KimiFrame(400,400,400,400,Color.orange);
  }
}

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

}

image-20211202162744592

面板

Frame frame = new Frame();
      //布局的概念
      Panel panel = new Panel();
      //布局设置
      frame.setLayout(null);
      //坐标
      frame.setBounds(300,300,330,330);
      frame.setBackground(new Color(0xF306F3));
      //panel设置坐标和颜色,相对于frame
      panel.setBounds(50,50,200,200);
      panel.setBackground(new Color(0x0C0CEE));
      //frame.add(panel)
      frame.add(panel);
      frame.setVisible(true);//可见
      //监听实践,监听创就关闭事件 System.exit(0);
      //适配器模式,可以解决关闭问题
      frame.addWindowListener(new WindowAdapter() {
          //窗口关闭是需要做的事情
          @Override
          public void windowClosing(WindowEvent e) {
              //结束程序
              super.windowClosing(e);
              System.exit(0);
          }
      });
  }

image-20211202165837278

布局管理

  • 流式布局

    image-20211202172937321

    Frame frame = new Frame();
    //组件——按钮
    Button button01 = new Button("button01");
    Button button02 = new Button("button02");
    Button button03 = new Button("button03");
    Button button04 = new Button("button04");
    //流式布局
    //frame.setLayout(new FlowLayout());
    frame.setLayout(new FlowLayout(FlowLayout.CENTER));
    frame.setSize(200,300);
    //添加按钮
    frame.add(button01);
    frame.add(button02);
    frame.add(button03);
    frame.add(button04);

    frame.setVisible(true);

    image-20211202173044361

  • 东西南北中

    Frame frame = new Frame("东西南北中");
    //组件——按钮中是Center
    Button east = new Button("east");
    Button west = new Button("west");
    Button south = new Button("south");
    Button north = new Button("north");
    //东西南北中

    //添加按钮
    frame.add(east,BorderLayout.EAST);
    frame.add(west,BorderLayout.WEST);
    frame.add(south,BorderLayout.SOUTH);
    frame.add(north,BorderLayout.NORTH);

    frame.setVisible(true);

    image-20211202173948859

  • 表格式布局

Frame frame = new Frame("GridLayout");
//组件——按钮
Button button01 = new Button("button01");
Button button02 = new Button("button02");
Button button03 = new Button("button03");
Button button04 = new Button("button04");
Button button05 = new Button("button05");
Button button06 = new Button("button06");
frame.setLayout(new GridLayout(3,2));
frame.add(button01);
frame.add(button02);
frame.add(button03);
frame.add(button04);
frame.add(button05);
frame.add(button06);
frame.pack();//java的,自动选择一个合适的位置
frame.setVisible(true);

image-20211202183146009

练习

  1. 分析过程

 

  1. 代码实现

import java.awt.*;
public class TEXT {
   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) );
       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.setVisible(true);

  }
}

image-20211202185852421

总结:

  1. Frame是一个顶级窗口

  2. Panel无法单独显示,必须添加到某个容器中。

  3. 布局管理器

    1. 流式

    2. 东西南北中

    3. 表格

    4. 大小,定位,背景颜色,可见性,监听!

事件监听

  • 事件的监听、处理流程:(参考了某位大大的csdn,我觉得他说的很好) 在这里插入图片描述

  • 事件源、事件、监听器之间的关系:

    • 事件源通过事件监听器注册方法,为自身添加事件监听器

    • 事件监听器中,实现了事件的handler方法:根据传入的事件或事件的事件源,执行具体操作

    • 事件源的某个动作,将(主动)触发事件监听器的handler方法处理事件

    • 在这里插入图片描述

      公用的可以不需要再写直接调用

一个按钮写一个监听
//关闭窗口事件
private static void windowClose(Frame frame){
  frame.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
          System.exit(0);
      }
  });
}
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 Actional01 {
   public static void main(String[] args) {
       //按下按钮,触发一些事件
       Frame frame= new Frame();
       Button button=new Button();
       //因为.addActionListener()需要一个ActionListener所以我们需要构造一个ActionListener
       MyListen myListen=new MyListen();
       button.addActionListener(myListen);
       frame.add(button,BorderLayout.CENTER);
       frame.pack();
       frame.setVisible(true);

       windowClose(frame);


  }
   //关闭窗口事件
   private static void windowClose(Frame frame){
       frame.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
          }
      });
  }
}
//事件监听关闭窗口事件
class MyListen implements ActionListener{
   @Override
   public void actionPerformed(ActionEvent e) {
       System.out.println("ar");//动作,点击窗口的时候,会输ar
  }
}

点击面板会打印ar

一个按钮写多个按钮
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 Action02 {
  public static void main(String[] args) {
      //两个按钮实现同一个监听,开始结束
      Frame frame = new Frame("开始结束");
      Button button = new Button("start");
      Button button1 = new Button("stop");
      button1.setActionCommand("stop");
      MyListener myListener = new MyListener();
      button.addActionListener(myListener);
      button1.addActionListener(myListener);
      frame.add(button,BorderLayout.NORTH);
      frame.add(button1,BorderLayout.CENTER);
      frame.setVisible(true);
      windowClose(frame);

  }

  //关闭窗口事件
  private static void windowClose(Frame frame) {
      frame.addWindowListener(new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
      });
  }
}
class MyListener implements ActionListener{
  @Override
  public void actionPerformed(ActionEvent e) {
      System.out.println("点击"+e.getActionCommand());
      e.getActionCommand();
  }
}

image-20211203123405893

点击start 打印 点击start

点击stop 打印 点击stop

制作计算器

  1. 基础

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 Cal {
  public static void main(String[] args) {
      //启动main里面正常只有一行启动代码
      new MyFrame();
  }
}
class MyFrame extends Frame{
  public MyFrame(){
      TextField textField =new TextField();
      add(textField);
      //监听这个文本框上输入的内容
      MyActionListener01 myActionListener01 = new MyActionListener01();
      //按enter后会将输入框中的内容打印输出
      textField.addActionListener(myActionListener01);
      //设置替换编码
      textField.setEchoChar('*');//前端获取的是*号,后端获取的是输入的内容密码将常会用哦
      setVisible(true);
      pack();
    // windowClose(0);
  }
  //关闭窗口事件
  private static void windowClose(Frame frame){
      frame.addWindowListener(new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
      });
  }
}
class MyActionListener01 implements ActionListener{
  @Override
  public void actionPerformed(ActionEvent e) {
      TextField file=(TextField) e.getSource();//获取一些资源,强制转换成TextFild类型
      System.out.println(file.getText());//获得文本框中的内容
      file.setText(" ");//null回车后清除
  }
}

image-20211203125442941image-20211203125454956

  1. 计算器编写

oop原则:组合大于继承(狂神yyds)

狂神yyds
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Cal02 {
  public static void main(String[] args) {
      new Calculator();

  }
}
//计算器类
class Calculator extends Frame{
  public Calculator(){
      //分析结构
      //3个文本框输入两个数,输出两个数
      TextField textField1 = new TextField(10);
      TextField textField2 = new TextField(10);
      TextField textField3 = new TextField(20);
      //一个按钮等号,给button添加监听时间能够计算
      Button button = new Button("=");
      button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));//传参
      //一个标签+号
      Label label = new Label("+");
      //布局流式布局
      setLayout(new FlowLayout());
      add(textField1);
      add(label);
      add(textField2);
      add(button) ;
      add(textField3);
      pack();
      setVisible(true);
  }
}
//主要需要做的事,给等号一个监听时间保证能等于数
class MyCalculatorListener implements ActionListener{
  private TextField textField1,textField2,textField3;
  public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3){
      this.textField1=textField1;
      this.textField2=textField2;
      this.textField3=textField3;
  }
  @Override
  public void actionPerformed(ActionEvent e) {
      //获取变量,构造里获得加数
      int n1 = Integer.parseInt(textField1.getText());
      int n2 = Integer.parseInt(textField2.getText());
      //加法运算后返回到第三个
      textField3.setText(""+(n1+n2));
      //清楚第三个
      textField1.setText("");
      textField2.setText("");
  }
}

image-20211203155828100点击等号前俩加数会清除,等号后计算出结果

面向对象写法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Cal02 {
  public static void main(String[] args) {
      new Calculator().loadFrame();

  }
}
//计算器类
class Calculator extends Frame {
  //属性
  TextField textField1, textField2, textField3;

  public void loadFrame() {
      //组件加布局
      textField1 = new TextField(10);
      textField2 = new TextField(10);
      textField3 = new TextField(20);
      Button button = new Button("=");
      button.addActionListener(new MyCalculatorListener(this));//把自己丢给自己
      Label label = new Label("+");
      setLayout(new FlowLayout());
      add(textField1);
      add(label);
      add(textField2);
      add(button);
      add(textField3);
      pack();
      setVisible(true);
  }
}

//主要需要做的事,给等号一个监听时间保证能等于数
class MyCalculatorListener implements ActionListener{
  //在一个类中用另一个类
      Calculator calculator=null;

  public MyCalculatorListener( Calculator calculator){
      this.calculator=calculator;
  }
  @Override
  public void actionPerformed(ActionEvent e) {
      //获取变量,构造
      int n1 = Integer.parseInt(calculator.textField1.getText());
      int n2 = Integer.parseInt(calculator.textField2.getText());
      calculator.textField3.setText(""+(n1+n2));
      calculator.textField1.setText("");
      calculator.textField2.setText("");
  }
}
内部类(更好的包装)可以更好的访问外部类
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Cal02 {
   public static void main(String[] args) {
       new Calculator().loadFrame();

  }
}
//计算器类
class Calculator extends Frame {
   //属性
   TextField textField1, textField2, textField3;

   public void loadFrame() {
       //组件加布局
       textField1 = new TextField(10);
       textField2 = new TextField(10);
       textField3 = new TextField(20);
       Button button = new Button("=");
       button.addActionListener(new MyCalculatorListener());//把自己丢给自己
       Label label = new Label("+");
       setLayout(new FlowLayout());
       add(textField1);
       add(label);
       add(textField2);
       add(button);
       add(textField3);
       pack();
       setVisible(true);
  }
   class MyCalculatorListener implements ActionListener{
       //在一个类中用另一个类

       @Override
       public void actionPerformed(ActionEvent e) {
           //获取变量,构造
           int n1 = Integer.parseInt(textField1.getText());
           int n2 = Integer.parseInt(textField2.getText());
           textField3.setText(""+(n1+n2));
           textField1.setText("");
           textField2.setText("");
      }
  }
}

画笔

package com.ar.chapter02;

import java.awt.*;

public class Paint01{
   public static void main(String[] args) {
       new MyPaint().loadFrame();
  }
}
class MyPaint extends Frame {
   public void loadFrame() {
       setBounds(100, 100, 400, 300);
       setVisible(true);
  }
   //画笔
   @Override
   public void paint(Graphics g) {
       g.setColor(Color.RED);
       g.drawOval(100,100,100,100);
       //养成习惯 画笔用完 还原到最初的颜色
       g.setColor(Color.BLACK);
  }
}

鼠标监听

点击

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class MouseListener {
   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.BLACK);
           g.fillOval(point.x,point.y,10,10);
      }
  }
   //添加一个点到界面上
   public void addPoint(Point point){
       points.add(point);
  }
   //适配器模式
   private class MyMouseListener extends MouseAdapter {
       //鼠标点击 按下,弹起,长按
       @Override
       public void mousePressed(MouseEvent e){
           MyFrame myFrame = (MyFrame) e.getSource();
           //点击时产生一个点,这个点就是鼠标的点
           myFrame.addPoint(new Point(e.getX(),e.getY()));
           new Point(e.getX(),e.getY());
           myFrame.repaint();
      }
  }
}

image-20211203175303035

image-20211203175447680

 

窗口监听

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Window011 {
   public static void main(String[] args) {
       new WindowFrame();
  }
}
class WindowFrame extends Frame{
   public WindowFrame(){
       setBackground(Color.BLUE);
       setBounds(100,100,800,400);
       setVisible(true);
       //addWindowListener(new MyWindowListener());

       // 匿名内部类
       this.addWindowListener(new WindowAdapter() {
           @Override
           public void windowOpened(WindowEvent e) {
               System.out.println("windowOpened");
          }

           @Override
           public void windowClosed(WindowEvent e) {
               System.out.println("windowClosed");
               System.exit(0);
          }

           @Override
           public void windowActivated(WindowEvent e) {
               System.out.println("windowActivated");
          }

           @Override
           public void windowClosing(WindowEvent e) {
               System.out.println("windowClosing");
               System.exit(0);
          }
      });

  }
//   class MyWindowListener extends WindowAdapter{
//       @Override
//       public void windowClosing(WindowEvent e) {
//           setVisible(false);//隐藏但是并没有消失
//           System.exit(0);
//       }
//   }
}

image-20211203181138097

键盘监听

image-20211203181709796

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Key01 {

  public static void main(String[] args) {
      new Key();
  }
}

  class Key extends Frame {
      public Key() {
          setBounds(100,100,300,300);
          setVisible(true);

          this.addKeyListener(new KeyAdapter() {
              @Override
              public void keyPressed(KeyEvent e) {
                  //获得键盘按下的键是哪一个
                  int keyCode = e.getKeyCode();
                  if(keyCode==KeyEvent.VK_2){
                      System.out.println("2");
                  }
              }
          });
      }
  }

按2的时候输出System.out.println("2");

2

image-20211203181957094

 

 

 

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM