Home  >  Article  >  Java  >  How to use Java delegated event model

How to use Java delegated event model

WBOY
WBOYforward
2023-04-21 18:25:181041browse

1. The event source initiates a specific event and sends the event to one or more event monitors.

2. The monitor remains in a waiting state during this process until it receives the event, then processes the event and returns.

Implement the code, register (or deactivate) the listener as a receiver of a specific event type, and trigger the event at the appropriate time.

Example

import java.awt.*;
 
import java.awt.event.*;
 
public class MyFrame implements ActionListener{
 
//实现ActionListener监听器接口
 
Frame f;
 
Button bt;
 
public MyFrame()
 
{
 
f=new Frame("My Frame"); //实例化窗口
 
f.setSize(400, 300);  //设置窗口大小
 
f.setLocationRelativeTo(null);
 
//设置窗口居中显示
 
bt=new Button("OK"); //实例化Button按钮
 
bt.addActionListener(this);
 
// 注册监听器对象
 
f.add(bt); //将按钮bt添加到窗口f上
 
f.setVisible(true); //设置窗口可显示
 
}
 
 
public static void main(String[] args) {
 
// TODO Auto-generated method stub
 
new MyFrame();
 
}
 
@Override
 
//实现监听器接口方法
 
public void actionPerformed(ActionEvent e) {
 
// TODO Auto-generated method stub
 
System.exit(0);  //结束程序
 
}
 
}

The above is the detailed content of How to use Java delegated event model. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete