Java图形界面——事件监听与处理
2014-04-29 14:56
507 查看
/*
* 事件监听与处理
*/
package com.test.tank;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Test2 extends JFrame implements ActionListener{
JPanel jp = null;
JButton jbt1 = null;
JButton jbt2 = null;
public static void main(String[] args) {
Test2 test = new Test2();
}
public Test2(){
jp = new JPanel();
jbt1 = new JButton("红色");
jbt1.addActionListener(this); //注册监听 ; this代表Test2这个类(对象)
jbt1.setActionCommand("red"); //设置命令
jbt2 = new JButton("蓝色");
jbt2.addActionListener(this); //注册监听
jbt2.setActionCommand("blue"); //设置命令
Other other = new Other();
jbt2.addActionListener(other); //让Other类的对象也注册监听jbt2
//添加组件并布局
this.add(jbt1, BorderLayout.WEST);
this.add(jp, BorderLayout.CENTER);
jp.setBackground(Color.black);
this.add(jbt2, BorderLayout.EAST);
this.setSize(300, 200);
this.setTitle("事件监听与处理");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override //对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("red")){//命令相同,则执行对应操作
jp.setBackground(Color.red);
}
if(e.getActionCommand().equals("blue")){
jp.setBackground(Color.blue);
}
}
}
/*
* 测试:同一个事件可让多个监听者(对象)
*/
class Other implements ActionListener{
@Override //也必须重写对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().endsWith("blue")){
System.out.println("其他对象也监听必处理了jbt2事件源");
}
}
}
* 事件监听与处理
*/
package com.test.tank;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Test2 extends JFrame implements ActionListener{
JPanel jp = null;
JButton jbt1 = null;
JButton jbt2 = null;
public static void main(String[] args) {
Test2 test = new Test2();
}
public Test2(){
jp = new JPanel();
jbt1 = new JButton("红色");
jbt1.addActionListener(this); //注册监听 ; this代表Test2这个类(对象)
jbt1.setActionCommand("red"); //设置命令
jbt2 = new JButton("蓝色");
jbt2.addActionListener(this); //注册监听
jbt2.setActionCommand("blue"); //设置命令
Other other = new Other();
jbt2.addActionListener(other); //让Other类的对象也注册监听jbt2
//添加组件并布局
this.add(jbt1, BorderLayout.WEST);
this.add(jp, BorderLayout.CENTER);
jp.setBackground(Color.black);
this.add(jbt2, BorderLayout.EAST);
this.setSize(300, 200);
this.setTitle("事件监听与处理");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override //对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("red")){//命令相同,则执行对应操作
jp.setBackground(Color.red);
}
if(e.getActionCommand().equals("blue")){
jp.setBackground(Color.blue);
}
}
}
/*
* 测试:同一个事件可让多个监听者(对象)
*/
class Other implements ActionListener{
@Override //也必须重写对事件的处理方法
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().endsWith("blue")){
System.out.println("其他对象也监听必处理了jbt2事件源");
}
}
}
相关文章推荐
- [已验证]双卡手机下两种来电监听方法的一些问题。
- jQuery中的常用事件总结
- c#事件使用示例详解
- extjs 为某个事件设置拦截器
- jQuery阻止同类型事件小结
- jQuery bind事件使用详解
- SQLSERVER中得到执行计划的两种方式
- javascript下利用for( in )语句 获得所有事件名称的代码
- 关于JavaScript与HTML的交互事件
- JavaScript 学习笔记(十六) js事件
- 通过event对象的fromElement属性解决热区设置主实体的一个bug
- JavaScript Event学习第十章 一些可替换的事件对
- JavaScript Event学习第九章 鼠标事件
- JavaScript Event学习补遗 addEventSimple
- javascript event 事件解析
- JavaScript Event学习第四章 传统的事件注册模型
- JS event使用方法详解
- 表单Form的submit事件不响应的解决方法
- 利用Keydown事件阻止用户输入实现代码
- javascript中window.event事件用法详解