浅谈在Java中的Event driven programming

Event driven programming, 顾名思义,就是通过事件去驱动程序运行。某一事件的发生可以驱动另一个事件。在JavaFX中,是这样使用的。

  1. 确定source event (通常是button)
  2. Implements eventHandler(T extends Event)
  3. Instantiate the handler, 然后注册(register)这个handler,通过setOnAction / SetOnMouseClicked …
button.setOnAction( e-> {
	/** Method goes here */
});

创建eventHandler 一般有三种办法:

  1. 先写一个inner class, 然后用 new handler() 去instantiate(实例化)
  2. Anonymous inner class (combine defining an inner class and creating an instance of it into one step)
  3. Lambda expression

JavaFX中,source event的概念很好理解,一般就是button。但是在Java中,我们一般怎么运用呢。这里需要调用到 Java.util.Consumer

Consumer<T> // This interface is similar to 
// the EventHander<T extends Event> interface
// When implementing this interface, we must implement the 
// its accept(T t) {} method just like we have to implement
// the handler(T extends Event evnet)

具体如何使用看下一段代码

class ConsumerHandler implements Consumer<Integer> {
            public void accept(Integer i) {
   				System.out.println("It is valued at $" + i + ". Hold on what you have.");
            }
        }

以上便是对Event driven programming的一些小结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Event-driven programming is a programming paradigm that allows the software to respond to events or user actions in a asynchronous manner. Here are the general steps to implement an event-driven program: 1. Identify the events: Determine the events that the program needs to respond to. These could be user actions, system events, or external signals. 2. Define event handlers: Create functions or methods to handle each event. These functions will be executed when the event occurs. 3. Register event handlers: Associate each event handler with the corresponding event. This is usually done using a framework or library. 4. Run the program: Once the event handlers are registered, the program is ready to execute. As events occur, the associated event handlers will be executed. 5. Clean up: After the program finishes executing, clean up any resources used or allocated during the program. Here's an example of an event-driven program in Python using the Tkinter GUI library: ```python import tkinter as tk def button_click(event): print("Button clicked") root = tk.Tk() button = tk.Button(root, text="Click me") button.bind("<Button-1>", button_click) button.pack() root.mainloop() ``` In this program, we identify the event as a button click, define an event handler function `button_click` to handle the event, register the event handler with the button using the `bind` method, and run the program using the `mainloop` method. When the button is clicked, the `button_click` function will be executed and print "Button clicked" to the console.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值