GUI编程---AWT(续)

本文介绍了JavaGUI编程中的事件监听机制,包括按钮的点击事件、输入框的文本变化、简易计算器的实现以及鼠标和键盘监听。通过实例展示了如何使用ActionListener处理多个按钮共享事件,以及如何利用内部类优化代码结构。此外,还涉及了窗口关闭监听和键盘事件的响应,以增强用户界面的交互性。
摘要由CSDN通过智能技术生成

GUI编程

1. 事件监听

package com.GUI.lesson02;

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.setBounds(400,400,500,500);
        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("aaaa");
    }
}
多个按钮,共享一个事件
package com.GUI.lesson02;

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 TestActionEvent02 {
    public static void main(String[] args) {
        
        Frame frame = new Frame("开始-停止");
        Button start = new Button("start");
        Button stop = new Button("stop");
        
        //当你有两个按钮或者多个按钮的属性名称相同时,就需要给每个按钮另外设置一个属性的字符串的值
        //来进行判断你究竟选择的是哪个按钮。就要用到setActioncommand();
        stop.setActionCommand("sacadfa");

        MyActionListener1 myActionListener = new MyActionListener1();
        start.addActionListener(myActionListener);
        stop.addActionListener(myActionListener);

        frame.setBounds(400,400,400,400);
        frame.add(start,BorderLayout.NORTH);
        frame.add(stop,BorderLayout.SOUTH);
        frame.pack();//自适应界面
        frame.setVisible(true);

    }

    //关闭窗口事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}
class MyActionListener1 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被按了:信号是=》"+e.getActionCommand());
        if (e.getActionCommand()=="start"){
            System.out.println("进入下一个界面");
        }
    }
}

在这里插入图片描述

2. 输入框TextField

package com.GUI.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();//监听这个文本框输入的文字
        textField.addActionListener(myActionListener2);
        textField.setEchoChar('*');//设置替换编码,输入显示星号,后台显示实际数字
        pack();
        setVisible(true);

    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField)e.getSource();//获得资源[强转]
        System.out.println(field.getText());
        field.setText("");//清空文档
    }
}

在这里插入图片描述

3. 简易计算器

组合方法
package com.GUI.lesson02;

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

public class TestCalculator {
    public static void main(String[] args) {
        new Calcilator();

    }
}
//计算器类
class Calcilator extends Frame{
    public Calcilator(){

        TextField field1 = new TextField(10);//字符数 //3个文本框
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        Button button = new Button("=");//1个按钮
        Label label = new Label("+"); //1个标签
        setLayout(new FlowLayout()); //流式布局
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
        setVisible(true);
        button.addActionListener(new MyCalcilatorListener(field1,field2,field3));
    }
}
//监听器类
class MyCalcilatorListener implements ActionListener{
    private TextField field1,field2,field3;
    public MyCalcilatorListener(TextField field1, TextField field2, TextField field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = Integer.parseInt(field1.getText());
        int j = Integer.parseInt(field2.getText());
        field3.setText(Integer.toString(i+j));
        field1.setText("");
        field2.setText("");
    }
}

在这里插入图片描述

完全改造为面向对象
package com.GUI.lesson02;

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

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

    }
}
//计算器类
class Calcilator extends Frame{
    //属性
    TextField field1,field2,field3;
    Button button;
    Label label;
    //方法
    public void loadFrame(){
        field1 = new TextField(10);//字符数 //3个文本框
        field2 = new TextField(10);
        field3 = new TextField(20);
        button = new Button("=");//1个按钮
        label = new Label("+"); //1个标签

        setLayout(new FlowLayout()); //流式布局
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
        setVisible(true);

        button.addActionListener(new MyCalcilatorListener(this));
    }
}
//监听器类
class MyCalcilatorListener implements ActionListener{
    //获取计算器这个对象,在一个类中组合另一个类
    Calcilator calcilator = null;
    public MyCalcilatorListener(Calcilator calcilator) {
        this.calcilator = calcilator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = Integer.parseInt(calcilator.field1.getText());
        int j = Integer.parseInt(calcilator.field2.getText());
        calcilator.field3.setText(Integer.toString(i+j));
        calcilator.field1.setText("");
        calcilator.field2.setText("");
    }
}
内部类
  • 内部类最大的好处就是可以畅通无阻的访问外部的属性和方法
package com.GUI.lesson02;

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

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

    }
}
//计算器类
class Calcilator extends Frame{
    //属性
    TextField field1,field2,field3;
    Button button;
    Label label;
    //方法
    public void loadFrame(){
        field1 = new TextField(10);//字符数 //3个文本框
        field2 = new TextField(10);
        field3 = new TextField(20);
        button = new Button("=");//1个按钮
        label = new Label("+"); //1个标签

        setLayout(new FlowLayout()); //流式布局
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
        setVisible(true);

        button.addActionListener(new MyCalcilatorListener());
    }
    //监听器类
    private class MyCalcilatorListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int i = Integer.parseInt(field1.getText());
            int j = Integer.parseInt(field2.getText());
            field3.setText(Integer.toString(i+j));
            field1.setText("");
            field2.setText("");
        }
    }
}

4. 画笔

package com.GUI.lesson03;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{
    public void loadFrame(){
        setBounds(400,400,400,400);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
        //super.paint(g);
        g.setColor(Color.green);
        g.drawOval(100,100,100,100);
        g.setColor(Color.cyan);
        g.fillRect(150,200,200,200);
    }
}

在这里插入图片描述

5. 鼠标监听

package com.GUI.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.EventListener;
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.black);
            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();//给谁加监听器,就返回谁作为source,返回一个窗口
            frame.addPaint(new Point(e.getX(),e.getY()));//我们点击的时候就会产生一个点,这就是鼠标的点
            frame.repaint();//刷新//每次点击鼠标要重画一次
        }
    }
}

在这里插入图片描述
在这里插入图片描述

6. 窗口监听

package com.GUI.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.cyan);
        setBounds(200,200,200,200);
        setVisible(true);
        this.addWindowListener(new MyWindowListener());
    }

    private class MyWindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);//隐藏窗口
            System.exit(0);//退出
        }
    }
}
改进的方法【匿名内部类】
package com.GUI.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.cyan);
        setBounds(200,200,200,200);
        setVisible(true);
        this.addWindowListener(
                new WindowAdapter() {//匿名内部类
                    @Override//关闭窗口
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                    }
                    @Override//激活窗口
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("被激活了");
                        System.out.println("windowActivated");
                    }
                });
    }
}

在这里插入图片描述

7. 键盘监听

package com.GUI.lesson03;

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

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
//自己的类
class KeyFrame extends Frame{
    public KeyFrame() {
        setBounds(1,2,300,400);
        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("你按下了上键");
                    
                }
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值