package test1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test2 extends JFrame implements ActionListener{
//定义我需要的组件
JButton jb;
JLabel jl;
JTextField jtf;
public static void main(String[] args) {
test2 t2 = new test2();
}
//构造器
public test2(){
//创建组件
jtf = new JTextField(10);
jl = new JLabel();
jb = new JButton("OK");
//设置布局管理
this.setLayout(new GridLayout(3, 1));
//将组件加入到JFrame
this.add(jtf,BorderLayout.NORTH);
this.add(jl,BorderLayout.CENTER);
this.add(jb,BorderLayout.SOUTH);
//注册监听
jb.addActionListener(this);
//设置大小和初始位置
this.setSize(200, 300);
this.setLocation(200, 200);
this.setVisible(true);
}
//事件处理的方法
public void actionPerformed(ActionEvent e) {
String s = jtf.getText();
jl.setText(s);
}
}
Java — 事件监听、事件处理 初体验
原创
©著作权归作者所有:来自51CTO博客作者peerslee的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【Android 事件处理】基于监听的事件处理机制
文章目录基于监听的事件处理机制本节引言1.基于监听的时间处理机制模型2.五种不同的使用形
android 事件处理 bundle ide