package technology;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFirstActionListener extends JFrame {
final static long serialVersionUID = 1L;
Container container = getContentPane();
JButton button = new JButton("点击我");
class ButtonListener implements ActionListener {
int x = 0;
public void actionPerformed(ActionEvent arg0) {
MyFirstActionListener.this.button.setText("我被点机了" + (++x) + "次");
public MyFirstActionListener()
super("JFrame窗体");
this.setBounds(200, 100, 200, 200);
button.addActionListener(new ButtonListener());
container.add(button);
this.setVisible(true);
public static void main(String[] args)
new MyFirstActionListener();
效果图如下:
文章目录1、什么是ActionListener1.1 了解ActionEvent类2、ActionListener作用3、ActionListener用法
1、什么是ActionListener
与KeyListener相似,ActionListener也是一个继承了Event接口的接口,我们需要一个类来实现ActionListener接口或者继承一个实现了ActionListener接口的类,...
消费者的链接可以使用c1.andThen(c2).accept(x)完成
不返回数据
BiConsumer: BiConsumer <整数,整数> con1 =(a,b)-> System.out.println(a + b);
事件处理机制的分析
swing的事件处理机制其实就是一个委托事件处理,是由事件源(组件)、事件(Event)、监听器(Listener)和 事件处理器(引发事件后的处理方式)组成。
产生事件的组件就叫事件源
事件是一个操作(通常使用各种输入设备,如键盘、鼠标等来完成)。
包含事件处理,并负责检查事件是否发生,若发生就激活事件处理器进行处理的类叫做事件监听类,其实例就是事件监听对象。事件监听器必须实现事件监听器接口或者继承事件监听器适配类。
事件处理器
事件处理器是一个.
/*class LabelListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label.setTex...
λ表达式的目标类型是“函数接口(functional
interface)”,这是Java8新引入的概念。
它的定义是:一个接口,如果只有一个显式声明的抽象方法,那么它就是一个函数接口。一般用@FunctionalInterface标注出来(也可以不标)。
@FunctionalInterface
public
interface
Runnable
run();
public
interface
Callable<V>
call()
throws
Exception;
public
interface
ActionListener
actionPerformed(ActionEvent
public
interface
Comparator<T>
compare(T
boolean
equals(Object
obj);
注意最后这个
1、 自定义类说明: (1)类名:MainTest 作用:主类,构建主界面 继承的父类:Jframe 主要实现接口:ActionListener 主要成员变量: private JPanel ContentPane; private JButton Insert = new JButton(); private JButton Select = new JButton(); private JButton Update = new JButton(); 内部类:MainTest_actionPerformed,主页面的事件监听。 jenter_ actionPerformed,按钮的事件监听。 主要成员方法: public MainTest() public static void main(String[] args) (2)类名:Insert 作用:实现添加个人信息 继承的父类:Jframe 主要实现接口:ActionListener 主要成员变量: private JPanel ContentPane; private JLabel jlab1 = new JLabel(); private JLabel jlab2 = new JLabel(); private JLabel jlab3 = new JLabel(); private JLabel jlab4 = new JLabel(); private JLabel jlab5 = new JLabel(); private JLabel jlab6= new JLabel(); private JLabel jlab7 = new JLabel(); private JLabel jlab8 = new JLabel(); private JTextField jtextfield1 = new JTextField(); private JTextField jtextfield2 = new JTextField(); private JTextField jtextfield3 = new JTextField(); private JTextField jtextfield4 = new JTextField(); private JTextField jtextfield5 = new JTextField(); private JComboBox jcombobox1 = new JComboBox(); private JComboBox jcombobox2 = new JComboBox(); private JTextArea jtextarea = new JTextArea(); private JButton sure = new JButton(); 内部类:sure_actionperformed,按钮的事件监听,实现对数据的插入。 主要成员方法:public Insert(); (3)类名:Update 作用:对个人信息进行修改。 继承的父类:Jframe 主要实现接口:ActionListener 主要成员变量: private JPanel ContentPane; private JLabel jlab1 = new JLabel(); private JLabel jlab2 = new JLabel(); private JLabel jlab3 = new JLabel(); private JLabel jlab4 = new JLabel(); private JLabel jlab5 = new JLabel(); private JTextField jtextfield1 = new JTextField(); private JTextField jtextfield2 = new JTextField(); private JTextField jtextfield3 = new JTextField(); private JTextField jtextfield4 = new JTextField(); private JButton sure = new JButton(); private JButton upda = new JButton(); 主要成员方法: public Update() public void actionPerformed(ActionEvent e) (4)类名:Select 作用:查询学生个人信息 继承的父类:Jframe 主要实现接口:ActionListener 主要成员变量: private JTextArea taInfo=new JTextArea(); private JPanel panel=new JPanel(); private JTextField jtextfield1=new JTextField(); private JButton jbutton=new JButton("查询"); private JLabel jlabel=new JLabel("学号"); 主要成员方法: public Select() public void actionPerformed(ActionEvent e)
1.按钮的事件通过监听按钮接口,是不是,监听就是解释你的动作呢?按下,释放,按下不放代表什么“意义”。
2.事件来源:用Java语言来解释,就是可以把用户操作(鼠标,键盘,关闭窗口)变成事件的对象。
3.事件:用对象来表示,在java.awt.event会有很多事件的类Mouseevent,keyevent,windowsevent.
4.监听:当事件来源发出动作变为事件对象,程序大多时候都是
一、匿名监听器
监听器类是特意为创建一个GUI组件(例如,一个按钮)而设计的监听对象。监听器类不被其他应用程序所共享,因此,正确的做法是将它作为一个内部类定义在框架中。
可以使用匿名内部类简化内部类监听器。匿名内部类时没有名字的内部类。它进一步完成定义内部类和创建一个该类的实例。
内部类EnlargeListener:public ControlCircle2(){
//omitted