Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动)

 /**
* Java事件监听处理——自身类实现ActionListener接口,作为事件监听器
*
* @author codebrother
*/
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("蓝色");
btDialog = new JButton("弹窗"); // 将按钮添加事件监听器
btBlue.addActionListener(this);
btDialog.addActionListener(this); add(btBlue);
add(btDialog); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// ***************************事件处理***************************
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btBlue) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
else if (e.getSource() == btDialog) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} } /**
* Java事件监听处理——内部类处理
*
* @author codebrother
*/ class EventListener3 extends JFrame {
private JButton btBlue, btDialog; // 构造方法
public EventListener3() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 添加事件监听器对象(面向对象思想)
btBlue.addActionListener(new ColorEventListener());
btDialog.addActionListener(new DialogEventListener()); add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 内部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
}
// 内部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} } /**
* Java事件监听处理——匿名内部类处理
*
* @author codebrother
*/
class EventListener2 extends JFrame {
private JButton btBlue, btDialog; public EventListener2() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout()); btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗"); // 添加事件监听器(此处即为匿名类)
btBlue.addActionListener(new ActionListener() {
// 事件处理
@Override
public void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
}); // 并添加事件监听器
btDialog.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}); add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} } /**
* Java事件监听处理——外部类处理
*
* @author codebrother
*/
class EventListener4 extends JFrame {
private JButton btBlue, btDialog; public EventListener4() {
setTitle("Java GUI 事件监听处理");
setBounds(100, 100, 500, 350);
setLayout(new FlowLayout());
btBlue = new JButton("蓝色");
btDialog = new JButton("弹窗");
// 将按钮添加事件监听器
btBlue.addActionListener(new ColorEventListener(this));
btDialog.addActionListener(new DialogEventListener()); add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} }
// 外部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {
private EventListener4 el;
ColorEventListener(EventListener4 el) {
this.el = el;
}
@Override
public void actionPerformed(ActionEvent e) {
Container c = el.getContentPane();
c.setBackground(Color.BLUE);
}
}
// 外部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
} public class ActionListenerTest
{
public static void main(String args[])
{
new EventListener2();
}
}

Java添加事件的四种方式的更多相关文章

  1. SWT组件添加事件的四种方式

    在我们CS日常开发过程中会经常去为组件添加事件,我们常用的为AWT与SWT.SWT的事件模型是和标准的AWT基本一样的.下面将按照事件的四种写法来实现它. 一.匿名内部类的写法 new MouseAd ...

  2. Java添加事件的几种方式(转载了codebrother的文章)

    /** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author codebrother */ class EventListener1 exte ...

  3. JAVA解析XML的四种方式

    java解析xml文件四种方式 1.介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这 ...

  4. android点击事件的四种方式

    android点击事件的四种方式 第一种方式:创建内部类实现点击事件 代码如下: package com.example.dail; import android.text.TextUtils; im ...

  5. Java创建线程的四种方式

    Java创建线程的四种方式 1.继承Thread类创建线程 定义Thread类的子类,并重写该类的run方法,run()方法的内容就是该线程执行的内容 创建Thread子类的实例,即创建了线程对象. ...

  6. jQuery绑定事件的四种方式:bind、live、delegate、on

    1.jQuery操作DOM元素的绑定事件的四种方式 jQuery中提供了四种事件监听方式,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undele ...

  7. java 遍历Map的四种方式

      java 遍历Map的四种方式 CreationTime--2018年7月16日16点15分 Author:Marydon 一.迭代key&value 第一种方式:迭代entrySet 1 ...

  8. java 20 -10 字节流四种方式复制mp3文件,测试效率

    电脑太渣,好慢..反正速率是: 高效字节流一次读写一个字节数组 > 基本字节流一次读写一个字节数组 > 高效字节流一次读写一个字节 > 基本字节流一次读写一个字节 前两个远远快过后面 ...

  9. 给TextView添加超链接的四种方式

    因为在上上篇博客中介绍了SpannableString的使用(SpannableString使用详解),由此想到给TextView添加超链接究竟有多少种方式?经过个人总结,现在一共发现四种,如果还有其 ...

随机推荐

  1. 字母A-Z写法

    #大写的a-z,ASCII编码 65..90|%{[char]$_}     #小写的A-Z  97..122|%{[char]$_}    方法二: ([char[]](97..122) -as [ ...

  2. 60款开源云应用【Part 2】(60 Open Source Apps You Can Use in the Cloud)

    60款开源云应用[Part 2](60 Open Source Apps You Can Use in the Cloud) 本篇翻译自http://www.datamation.com/open-s ...

  3. Java连接MYSQL【转载】

    这篇文章主要以MySQL为例讲下Java如何连接到数据库的. 当然,首先要安装有JDK(一般是JDK1.5.X).然后安装MySQL,这些都比较简单,具体过程就不说了.配置好这两个环境后,下载JDBC ...

  4. shell修改文件名(一)

    假如文件名是:time_filename.txt 改成filename_time.txt.例如20111111_me.txt改成me_201111111.txt要如何修改? #! /bin/sh fo ...

  5. 基于新浪sae使用php生成图片发布图文微博

    1.生成图片的代码: <?php header ("Content-type: image/png"); mb_internal_encoding("UTF-8&q ...

  6. UIAlertController 简单修改title以及按钮的字体颜色

    苦逼的开发者,最终败给了一个任性的UI,系统原生UIAlertController的按纽颜色必须改.于是,开始了不归路.之前的版本是自己用view写的一个仿系统UIActionSheet,动画感觉都挺 ...

  7. Google stuff

    Google Pro Tip: Use Back-of-the-envelope-calculations to Choose the Best Design - High Scalability - ...

  8. IBM Rational ClearCase 部署指南

    引言 随着时间的推移,可视化设计与软件配置管理(SCM)已经逐渐成为现代软件项目成功的关键因素.IBM Rational 是 IBM Rational XDE 和 IBM Rational Clear ...

  9. Apache的RewriteRule规则详细介绍

    R[=code](force redirect) 强制外部重定向 (rkyW z强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定, ...

  10. 命令行一键清除IE记录

    清除Internet临时文件 RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 清除Cookies RunDll32.exe InetCpl.cpl, ...