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

Posted 高高for 循环

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("叮叮叮当");
    }
}

在这里插入图片描述

案例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());
    }
}

在这里插入图片描述

输入框

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("");
    }
}

在这里插入图片描述

计算器

在这里插入图片描述

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("");
    }
}

在这里插入图片描述

改造为面向对象

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

以上是关于GUI编程--02--AWT.事件监听的主要内容,如果未能解决你的问题,请参考以下文章

GUI编程事件监听以及简易加法计算器的实现

java GUI编程(swing)之七swing事件监听

Java实验5 GUI编程

js-基础总结3

JavaSE学习56:GUI编程之事件模型

java GUI编程 内含swing基础知识 小游戏开发基础