java代码对按钮进行监听---------------打印出每次点击按钮的次数

其实,我真不会写嗯?

package com.a.b;

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

import javax.swing.*;

public class helloworld extends JFrame implements ActionListener {
	JButton jb = new JButton("请按下");
	JTextField tf = new JTextField(20);
	int count;// 点击次数

	public void actionPerformed(ActionEvent a) {

		count++;
		tf.setText("Button clicked " + count + "times");
	}

	public static void main(String[] args) {
		helloworld h = new helloworld("谢谢你对我的帮助");
		h.setSize(500, 500);
		h.setVisible(true);
		h.setLocation(null);

	}

	public helloworld(String title) {// 看到没?凡是构造方法里的参数它是可以决定构造方法实例化的
		super(title);
		this.setLayout(null);
		jb.setBounds(20, 40, 120, 20);// 这个是按钮的e
		tf.setBounds(20, 60, 140, 20);
		// 这个是文本框
		jb.addActionListener(this);// 注册监听
		add(jb);
		add(tf);
		this.setDefaultCloseOperation(3);

	}
}

  

原文地址:https://www.cnblogs.com/langlove/p/3477763.html