Swing中事件处理过程

// 说明:当点击按钮时会触发动作事件,清空文本框的数据
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test {

    static final int WIDTH = 200;
    static final int HEIGHT = 300;
    static JTextField l = new JTextField(20);
    
    public static void main(String[] args) {
        JFrame jf = new JFrame("测试程序");
        jf.setSize(WIDTH, HEIGHT);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        jf.setContentPane(contentPane);
        JButton jb = new JButton("清空文本框中的信息");
        contentPane.add(l, "North");
        contentPane.add(jb, "South");
        
        ActionListener ac = new ActionHandler();  // 创建一个事件监听器
        jb.addActionListener(ac);                 // 向事件源注册


    }

}
// 定义实现事件监听类
class ActionHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        new test().l.setText("");
    }

}


将事件处理过程进行总结:

(1)定义事件事件监听接口类:class ActionHandler implements ActionListener{ }

(2)创建事件监听器:ActionListener ac = new ActionHandler();

(3)向源事件注册:jb.addActionListener(ac);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值