【Java GUI编程】002-AWT学习笔记(二):事件监听、输入框事件监听、画笔、鼠标监听、窗口监听、键盘监听

一、事件监听

1、事件监听

当某件事发生时,做些什么;

 

2、按钮点击监听

代码演示:

package com.zibo.lession02;

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

public class TestAction {
    public static void main(String[] args) {
        Frame frame = new Frame("TestAction");
        Button btn = new Button("btn");
        //因为addActionListener()需要ActionListener,所以我们写一个实现类
        MyActionListener myActionListener = new MyActionListener();
        btn.addActionListener(myActionListener);
        frame.add(btn,BorderLayout.CENTER);
        frame.setBounds(300,300,500,500);
        frame.setVisible(true);
    }
}
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("AAA");
    }
}

运行结果:

 

3、两个按钮共用一个监听事件

代码演示:

package com.zibo.lession02;

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

public class TestAction2 {
    public static void main(String[] args) {
        Frame frame = new Frame("TestAction2");
        Button start = new Button("start");
        Button stop = new Button("stop");
        //因为addActionListener()需要ActionListener,所以我们写一个实现类
        MyActionListener2 myActionListener = new MyActionListener2();
        start.addActionListener(myActionListener);
        stop.addActionListener(myActionListener);
        stop.setActionCommand("stop-go");
        frame.add(start,BorderLayout.EAST);
        frame.add(stop,BorderLayout.WEST);
        frame.setBounds(300,300,500,500);
        frame.setVisible(true);
    }
}

class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了,ActionCommand:" + e.getActionCommand());
    }
}

运行结果:

 

二、输入框事件监听

代码演示:

package com.zibo.lession02;

import java.awt.*;

public class TestTest01 {
    public static void main(String[] args) {
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        setBounds(300,300,500,500);
        TextField textField = new TextField();
        //密码输入
//        textField.setEchoChar('*');
        textField.addActionListener(e -> {
            TextField field = (TextField)e.getSource();
            String text = field.getText();
            System.out.println(text);
        });
        add(textField);
        setVisible(true);
    }

}

运行结果:

 

三、简易计算器

代码演示:

package com.zibo.lession02;

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

public class EasyCalculator {
    public static void main(String[] args) {
        new MyEasyCalculator();
    }
}
class MyEasyCalculator extends Frame{
    public MyEasyCalculator(){
        setBounds(500,500,400,80);
        setLayout(new FlowLayout());
        TextField field1 = new TextField(10);//10为字符数
        Label add = new Label("+");
        TextField field2 = new TextField(10);
        Label is = new Label("=");
        TextField field3 = new TextField(10);
        Button go = new Button("go");
        go.addActionListener(e -> {
            if(!"".equals(field1.getText()) && !"".equals(field2.getText())){
                field3.setText(String.valueOf(Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText())));
            }
        });
        add(field1);
        add(add);
        add(field2);
        add(is);
        add(field3);
        add(go);
        setVisible(true);
    }
}

运行结果:

 

四、画笔paint

代码演示:

package com.zibo.lession02;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame {
    public void loadFrame(){
        setBounds(300,300,500,500);
        setVisible(true);
    }
    //画笔

    @Override
    public void paint(Graphics g) {
        //设置画笔颜色
        g.setColor(Color.RED);
        //画空心圆
//        g.drawOval(50,50,100,100);
        //画实心圆
        g.fillOval(50,50,100,100);
        //画矩形
        g.fillRect(200,200,100,150);
    }
}

运行结果:

 

五、鼠标监听事件

代码演示:

package com.zibo.lession03;

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

public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("TestMouseListener");
    }
}
class MyFrame extends Frame{
    List<Point> pointList = new ArrayList<>();
    public MyFrame(String title){
        setTitle(title);
        setBounds(300,300,500,500);
        setVisible(true);

        //鼠标监听事件
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                MyFrame myFrame = (MyFrame) e.getSource();
                myFrame.addPaint(new Point(e.getX(),e.getY()));
                //重置画笔
                myFrame.repaint();
            }
        });
    }

    private void addPaint(Point point){
        pointList.add(point);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for (Point point : pointList) {
            g.setColor(Color.RED);
            g.fillOval(point.x, point.y, 10, 10);
        }
    }
}

运行结果:

 

六、窗口监听

代码演示:

package com.zibo.lession03;

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

public class TestWindowFrameListener {
    public static void main(String[] args) {
        new WindowFrame("TestWindowFrameListener");
    }
}
class WindowFrame extends Frame{
    public WindowFrame(String title) throws HeadlessException {
        setTitle(title);
        setBounds(300,300,500,500);
        setBackground(Color.CYAN);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            //常用4个
            @Override
            public void windowOpened(WindowEvent e) {
                super.windowOpened(e);
                System.out.println("窗口打开");
            }

            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.out.println("窗口关闭中(常用)");
                setVisible(false);//隐藏
                System.exit(0);//结束程序
            }

            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosing(e);
                System.out.println("窗口关闭后");
            }

            @Override
            public void windowActivated(WindowEvent e) {
                super.windowActivated(e);
                System.out.println("窗口激活(常用)");
            }

        });
    }
}

运行结果:

 

七、键盘监听

代码演示:

package com.zibo.lession03;

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

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame("TestKeyListener");
    }
}
class KeyFrame extends Frame{
    public KeyFrame(String title){
        setTitle(title);
        setBounds(300,300,500,500);
        setVisible(true);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if(e.getKeyCode() == KeyEvent.VK_Q){
                    System.out.println("你按了Q键!");
                    System.out.println(e.getKeyCode());
                }
            }
        });
    }
}

运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值