当前位置: 技术问答>java相关
关于监听器
来源: 互联网 发布时间:2015-09-10
本文导语: 书上说可以通过建立任何监听器类对象,实现监听器接口,我不知道监听器类是什么,我知道监听器接口是:WindowListener,MouseListener,MouseMotionListener,KeyListener,FocusListener我想就是这些了。可书上说ActionListener是一个接口...
书上说可以通过建立任何监听器类对象,实现监听器接口,我不知道监听器类是什么,我知道监听器接口是:WindowListener,MouseListener,MouseMotionListener,KeyListener,FocusListener我想就是这些了。可书上说ActionListener是一个接口,我不知道他和什么接口是一组,就上上面我写的一样。
书上还说简单的实现监听器接口不足以将监听器对象链接到事件源上。还要必须将监听器链接到自己想处理的事件源上,“即通过调用源对象中特定的方法注册带有源的监听器对象。”书上的这句话我不明白,也不会做请那位朋友帮我一下吧,谢谢了。
书上还说简单的实现监听器接口不足以将监听器对象链接到事件源上。还要必须将监听器链接到自己想处理的事件源上,“即通过调用源对象中特定的方法注册带有源的监听器对象。”书上的这句话我不明白,也不会做请那位朋友帮我一下吧,谢谢了。
|
例子
//定义接口:其实就是定义类
//我接口中并没有将方法实现
interface myInterface{
int getNum();
String getName();
}
//定义类,声明了接口
public class stu implement myInterface{
.....
//添加你自己的代码.....
//下面实现接口
int getNum(){
.......
return xxx;
}
String getName(){
......
return yyy;
}
}
//定义接口:其实就是定义类
//我接口中并没有将方法实现
interface myInterface{
int getNum();
String getName();
}
//定义类,声明了接口
public class stu implement myInterface{
.....
//添加你自己的代码.....
//下面实现接口
int getNum(){
.......
return xxx;
}
String getName(){
......
return yyy;
}
}
|
//Item2.java
//你可以看以下下面的代码,我能正常编译运行的
import java.util.*;
public class Item2 implements Comparable {
private String id;
private String name;
private double retail;
private int quantity;
private double price;
private boolean noDiscount;
Item2(String idIn, String nameIn, String retailIn, String quanIn, String discountIn) {
id = idIn;
name = nameIn;
retail = Double.parseDouble(retailIn);
quantity = Integer.parseInt(quanIn);
if (discountIn.equals("TRUE"))
noDiscount = true;
else
noDiscount = false;
if (quantity > 400)
price = retail * .5D;
else if (quantity > 200)
price = retail * .6D;
else
price = retail * .7D;
price = Math.floor( price * 100 + .5 ) / 100;
if (noDiscount)
price = retail;
}
public int compareTo(Object obj) {
Item2 temp = (Item2)obj;
if (this.price temp.price)
return -1;
return 0;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getRetail() {
return retail;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
}
//你可以看以下下面的代码,我能正常编译运行的
import java.util.*;
public class Item2 implements Comparable {
private String id;
private String name;
private double retail;
private int quantity;
private double price;
private boolean noDiscount;
Item2(String idIn, String nameIn, String retailIn, String quanIn, String discountIn) {
id = idIn;
name = nameIn;
retail = Double.parseDouble(retailIn);
quantity = Integer.parseInt(quanIn);
if (discountIn.equals("TRUE"))
noDiscount = true;
else
noDiscount = false;
if (quantity > 400)
price = retail * .5D;
else if (quantity > 200)
price = retail * .6D;
else
price = retail * .7D;
price = Math.floor( price * 100 + .5 ) / 100;
if (noDiscount)
price = retail;
}
public int compareTo(Object obj) {
Item2 temp = (Item2)obj;
if (this.price temp.price)
return -1;
return 0;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getRetail() {
return retail;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
}
|
//define a event object,the object will tell the consumer which window throws the event
//a event object is a object that will take the data from source to dest,
//or a protocal between two objects(here are Event source object and Event listener)
class WndEvent{
Wnd m_oWnd;
WndEvent(Wnd src){m_oWnd=src;}
String getWnd(){return m_oWnd.toString();};
}
//define a listener that process the window event
interface WndListener{
void wndOpen(WndEvent e);
void wndClose(WndEvent e);
}
//a listener implemention
class AWndListener implements WndListener{
public void wndOpen(WndEvent e){
System.out.println("window ["+ e.getWnd() + "] opened.");
}
public void wndClose(WndEvent e){
System.out.println("window ["+ e.getWnd() + "] closed.");
}
}
//a event source object
class Wnd{
String m_strWndName;
Wnd(String strWndName){m_strWndName=strWndName;}
void initShow(){
//show the window first time
//after accomplish this,call wndOpen()
for(int i=0;i