computer 版 (精华区)
发信人: arbo (arbo), 信区: program
标 题: java 笔记4
发信站: 听涛站 (2001年09月17日10:44:09 星期一), 站内信件
发信人: airforce1 (五湖废人), 信区: Java
标 题: java 笔记4
发信站: BBS 水木清华站 (Sat Apr 1 16:24:38 2000)
又讲了点事件响应的东西。
jdk1.0 和jdk1.1后有角大的区别
Hierarchical model(Jdk1.0)
Delegation model(Jdk1.1)
Hierarchical Model is quite simple and well situed toan object
oriented programming environment
but just can handled by the componet from which it originated
or by one of the containers of the containers of the originating component.
and you must either subclass or base container will need to create a
handleEvent()
Delegation Model ,
events are sent to the component from which
the event originated, but it si up to each component to
register one or more classes called listeners, which contain event handlers
that can receive object s=eparate from the component.
Listeners are classses which implement the Listener interface.
a example: see last
event divide into many categories.
another two concept is Event Adapters and Anonymous Classes
because the listerner are the interface, you need to create the function
body for every method in the interface, very boring to use, then
the adapter let you can just overwirte the method you need.
anonymous classes is just : include an entire class defintion within the
scpe of an expression.
event of jdk1.1 sample.
see next
import java.awt.*;
import java.awt.event.*;
public class TwoListen implements
MouseMotionListener, MouseListener {
private Frame f;
private TextField tf;
public static void main(String args[]) {
TwoListen that = new TwoListen();
that.go();
}
public void go() {
f = new Frame("Two listeners example");
f.add ("North", new Label ("Click and drag the mouse"));
tf = new TextField (30);
f.add ("South", tf);
tf.addMouseMotionListener(new Aa());
f.addMouseMotionListener(this);
f.addMouseListener (this);
f.setSize(300, 200);
f.setVisible(true);
}
// These are MouseMotionListener events
public void mouseDragged (MouseEvent e) {
String s = "Mouse dragging: X = " + e.getX() + " Y = " + e.
getYY();
System.out.println(s);
tf.setText (s);
}
public void mouseMoved (MouseEvent e) {
String s = "Mouse moving: X = " + e.getX() + " Y = " + e.ge
tY()
f.requestFocus();
System.out.println(s);
tf.setText (s);
}
// These are MouseListener events
public void mouseClicked (MouseEvent e) {
}
public void mouseEntered (MouseEvent e) {
String s = "The mouse entered";
tf.setText (s);
s=e.paramString();
tf.setText(s);
//f = new Frame("mouseEntered ");
//f.add ("West",tf );
//f.setSize(100,200);
//f.setVisible(true);
}
public void mouseExited (MouseEvent e) {
String s = "The mouse has left the building";
tf.setText (s);
}
public void mousePressed (MouseEvent e) {
}
public void mouseReleased (MouseEvent e) {
}
}
class Aa implements MouseMotionListener{
public void mouseDragged(MouseEvent e){
Component c;
c=e.getComponent();
String s=c.toString();
if (c instanceof TextField)
{
// String s=e.paramString();
System.out.println("MouseDrageed happened"+s);
}
}
public void mouseMoved (MouseEvent e) {
String s=e.paramString();
System.out.println("MouseMoved happened"+s);
}
}
--
--
※ 来源:·听涛站 tingtao.dhs.org·[FROM: 匿名天使的家]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1.818毫秒