GUI编程--02--AWT.事件监听

事件监听

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

addActionListener() 设置监听

案例1 :点击一个button ,显示台打印相关信息

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 TsetActionEvent {


    public static void main(String[] args) {

        //按下按钮发生一些事件
        Frame frame = new Frame();
        Button button = new Button("please!");
        //
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button);
        frame.pack();
        windowClose(frame);

        frame.setBounds(300,300,500,500);
        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("叮叮叮当");
    }
}

GUI编程--02--AWT.事件监听_第1张图片

案例2 : 多个按钮,实现同一个接口

  1. setActionCommand( ) 可以设置,显示的自定义,触发会返回的命令.
  2. 如果不显示定义,则会走默认的值!可以多个按钮只写一个监听器
  3. getActionCommand(),获取按钮上的信息
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 TsetActionEvent {


    public static void main(String[] args) {
        Frame frame = new Frame();
        //两个按钮,实现同一个监听
        //开始----停止
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!可以多个按钮只写一个监听器
        button2.setActionCommand("stop!!!!!!!!!!!!!");
        mymoniter mymoniter = new mymoniter();
        button1.addActionListener(mymoniter);
        button2.addActionListener(mymoniter);
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.pack();
        frame.setVisible(true);
    }
}


class  mymoniter implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取按钮上的信息
        System.out.println("按钮被点击了:"+e.getActionCommand());
    }
}

GUI编程--02--AWT.事件监听_第2张图片

输入框

new TextField()

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Testfield {
    public static void main(String[] args) {
        //启动
        MyFrame myFrame = new MyFrame();
        myFrame.setVisible(true);
    }
}

class MyFrame extends Frame {

    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        ActionListener1 actionListener1 = new ActionListener1();
        //监听文本框输入的文字
        textField.addActionListener(actionListener1);
        //设置替换编码
        textField.setEchoChar('*');
    }
}

class ActionListener1 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField fisld =(TextField) e.getSource(); //获得一些资源,返回一个对象
        System.out.println(fisld.getText());        //enter 即可获取
        fisld.setText("");
    }
}

GUI编程--02--AWT.事件监听_第3张图片

计算器

GUI编程--02--AWT.事件监听_第4张图片

oop原则:组合,大于继承!

面向过程

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 TestCalculator {
    public static void main(String[] args) {
        //运行
        new Calculator();
    }
}

//计算器类
class Calculator extends Frame {

    public Calculator(){
        //三个文本框
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(20);
        //一个按钮
        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);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

//监听类
class MyCalculatorListener implements ActionListener {
    private TextField textField1;
    private TextField textField2;
    private TextField textField3;

    public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3) {
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(textField1.getText());
        int n2 = Integer.parseInt(textField2.getText());
        //2.将
        textField3.setText(""+(n1+n2));
        //3.清除前两个框
        textField1.setText("");
        textField2.setText("");
    }
}

GUI编程--02--AWT.事件监听_第5张图片

改造为面向对象

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 TestCalculator {
    public static void main(String[] args) {
        //运行
        new Calculator();
    }
}

//计算器类
class Calculator extends Frame {

    //三个文本框
    TextField textField1 ;
    TextField textField2 ;
    TextField textField3 ;

    public  Calculator(){
        load();
    }


    public void load(){

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

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

//监听类
class MyCalculatorListener implements ActionListener {
    private Calculator calculator;


    public MyCalculatorListener( Calculator calculator) {
        this.calculator=calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(calculator.textField1.getText());
        int n2 = Integer.parseInt(calculator.textField2.getText());
        //2.将
        calculator.textField3.setText(""+(n1+n2));
        //3.清除前两个框
        calculator.textField1.setText("");
        calculator.textField2.setText("");
    }
}

内部类

内部类好处,畅通无阻的访问外部类

package awt;

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 TestCalculator {
    public static void main(String[] args) {
        //运行
        new Calculator();
    }
}

//计算器类
class Calculator extends Frame {

    //三个文本框
    TextField textField1 ;
    TextField textField2 ;
    TextField textField3 ;

    public  Calculator(){
        load();
    }

    public void load(){

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

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //监听器类---》内部类
    //内部类好处,畅通无阻的访问外部类
  private   class MyCalculatorListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int n1 = Integer.parseInt(textField1.getText());
            int n2 = Integer.parseInt(textField2.getText());
            //2.将
            textField3.setText(""+(n1+n2));
            //3.清除前两个框
            textField1.setText("");
            textField2.setText("");
        }
    }

}

画笔 paint

package awt;

import java.awt.*;

public class TestPanint {
    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);
        //画笔用完,还原到最初的颜色
    }
}

GUI编程--02--AWT.事件监听_第6张图片

鼠标监听

  • 当点击鼠标按钮时,有三个监听器方法可以调用:①鼠标按下时调用mousePressed;②鼠标被释放时调用mouseReleased;③关注鼠标的最终点击事件时调用mouseClicked
  • 鼠标点击可以由mouseClicked过程报告,该方法是MouseListener接口的一部分。通常大部分应用程序只对鼠标点击事件感兴趣,而对鼠标移动及拖动并不感兴趣,但是鼠标移动及拖动事件又经常发生,因此,鼠标移动与拖动事件定义在一个MouseMotionListener接口中。

目的:实现鼠标画画

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouse {

    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

//鼠标类
class MyFrame extends Frame{
    //画画需要画笔,需要监听鼠标当前的位置
    // 需要集合来存储这个点
    ArrayList points;
    public MyFrame(String title)  {
        super(title);
        setBounds(200,200,400,400);
        //存鼠标的点
        points = new ArrayList<>();
        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouse());
        setVisible(true);
    }
    @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,5,5);//画点时显示的其实是实心圆
        }
    }
    //添加一个点到界面上,写一个方法
    public void addPoint(Point point){
        points.add(point);
    }
    //适配器模式
    private class MyMouse extends MouseAdapter {
        //鼠标   按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //当我们点击的时候就会在界面上产生一个点
            //这个点就是鼠标的点
            myFrame.addPoint(new Point(e.getX(),e.getY()));
            //每一次点击鼠标都需要重新画一遍
            myFrame.repaint();//刷新
        }
    }
}

GUI编程--02--AWT.事件监听_第7张图片

窗口监听

窗口退出

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(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
        addWindowListener(new MyWindowListener());
    }
    class MyWindowListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);//正常退出
        }
    }
}

点击右上角的x,窗口关闭
GUI编程--02--AWT.事件监听_第8张图片

匿名内部类

package awt;

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

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    public WindowFrame(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
       this.addWindowListener(new WindowAdapter() {
           @Override
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });
    }

}

WindowAdapter 相关方法

//窗口监听
public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame{
    public WindowFrame(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowOpened(WindowEvent e) {
                        System.out.println("windowOpened");
                    }
                    //关闭窗口
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                    }
                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println("windowClosed");
                    }
                    //最小化
                    @Override
                    public void windowIconified(WindowEvent e) {
                        System.out.println("windowIconified");
                    }
                    @Override
                    public void windowDeiconified(WindowEvent e) {
                        System.out.println("windowDeiconified");
                    }
                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        System.out.println("windowActivated");
                    }
                }
        );
    }
}

GUI编程--02--AWT.事件监听_第9张图片

键盘监听

import javafx.animation.KeyFrame;

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

public class TestKey {
    public static void main(String[] args) {
        new KeyFrame1();
    }
}

class KeyFrame1 extends Frame {
    public KeyFrame1(){
        setBounds(100,100,300,300);
        setBackground(Color.CYAN);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的是哪一个键,当前的码
                int keyCode = e.getKeyCode();//不需要记录这个码值,可直接使用静态属性
                System.out.println(keyCode);
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("上键");
                }
                //根据按下的不同操作,产生不同的结果
            }
        });
    }
}

GUI编程--02--AWT.事件监听_第10张图片

你可能感兴趣的:(前端,java)