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

How to use Java delegated event model

WBOY
Release: 2023-04-21 18:25:18
forward
1064 people have browsed it

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);  //结束程序
 
}
 
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template