java监听有哪些_java中的四种监听类用法

java监听有哪些_java中的四种监听类⽤法在此列举四种⽅法:
⾃⾝类实现ActionListener接⼝,作为事件
通过匿名类处理
通过内部类处理
薛定谔猫态通过外部类处理
下⾯依次介绍:
第⼀种:⾃⾝类实现ActionListener接⼝,作为事件。
这种⽅法是最基本的,也是初学者经常使⽤的,我当初即是如此。
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class EventListener1 extends JFrame
implements ActionListener {
private JButton btBlue, btDialog;
// 构造⽅法
public EventListener1() {
// 设置标题栏内容
setTitle("Java GUI 事件监听处理");
// 设置初始化窗⼝位置
setBounds(100, 100, 500, 350);
// 设置窗⼝布局
setLayout(new FlowLayout());
// 创建按钮对象
btBlue = new JButton("蓝⾊");
// 将按钮添加事件
btBlue.addActionListener(this);
// 创建按钮对象
btDialog = new JButton("弹窗");
/
/ 将按钮添加事件
btDialog.addActionListener(this);
// 把按钮容器添加到JFrame容器上
add(btBlue);
add(btDialog);
// 设置窗⼝可视化
setVisible(true);
// 设置窗⼝关闭
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
//
***************************事件处理***************************
@Override
public void actionPerformed(ActionEvent e)
{
// 判断最初发⽣Event事件的对象
if (e.getSource() == btBlue) {
// 获得容器
Container c = getContentPane();
// 设置容器背景颜⾊
c.setBackground(Color.BLUE);
}
else if (e.getSource() == btDialog)
{
// 创建JDialog窗⼝对象
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400,
300);
dialog.setVisible(true);
}
}
//
新技术新工艺
***************************主⽅法*************************** public static void main(String[] args)
new EventListener1();
宁资理
}
}
第⼆种,通过匿名类处理。
这是⽐较好的⼀种⽅法,我是在2011年开始使⽤这种⽅法的。import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class EventListener2 extends JFrame
{
private JButton btBlue, btDialog;
// 构造⽅法
public EventListener2() {
// 设置标题栏内容
setTitle("Java GUI 事件监听处理");
// 设置初始化窗⼝位置
setBounds(100, 100, 500, 350);
// 设置窗⼝布局
setLayout(new FlowLayout());
// 创建按钮对象
btBlue = new JButton("蓝⾊");
// 添加事件(此处即为匿名类)
btBlue.addActionListener(new ActionListener()
{
// 事件处理
@Override
public void actionPerformed(ActionEvent e)
// 获得容器,设置容器背景颜⾊
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
});
// 创建按钮对象,并添加事件
btDialog = new JButton("弹窗");
btDialog.addActionListener(new
ActionListener() {
// 事件处理
@Override
public void actionPerformed(ActionEvent e)
{
/
/ 创建JDialog窗⼝对象
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400,
300);
dialog.setVisible(true);
}
});
// 把按钮容器添加到JFrame容器上
add(btBlue);
add(btDialog);
// 设置窗⼝可视化
setVisible(true);
// 设置窗⼝关闭
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
//
***************************主⽅法*************************** public static void main(String[] args)
{
new EventListener2();
}会计研究
第三种:通过内部类处理。
该种⽅法更符合⾯向对象编程的思想。
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class EventListener3 extends JFrame {
private JButton btBlue, btDialog;
// 构造⽅法
public EventListener3() {
// 设置标题栏内容
setTitle("Java GUI 事件监听处理");黑止血钳
// 设置初始化窗⼝位置
setBounds(100, 100, 500, 350);
在太空中理家// 设置窗⼝布局
setLayout(new FlowLayout());
// 创建按钮对象
btBlue = new JButton("蓝⾊");
// 添加事件对象(⾯向对象思想)
btBlue.addActionListener(new ColorEventListener());
btDialog = new JButton("弹窗");
btDialog.addActionListener(new DialogEventListener());
// 把按钮容器添加到JFrame容器上
add(btBlue);

本文发布于:2024-09-22 01:25:13,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/516664.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   容器   事件   对象   添加
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议