按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
this。color = color;
}
public void paint(Graphics g) {
g。setColor(color);
int rnd = 30;
g。fillRoundRect(0; 0; size()。width;
size()。height; rnd; rnd);
g。setColor(Color。black);
397
…………………………………………………………Page 399……………………………………………………………
g。drawRoundRect(0; 0; size()。width;
size()。height; rnd; rnd);
FontMetrics fm = g。getFontMetrics();
int width = fm。stringWidth(label);
int height = fm。getHeight();
int ascent = fm。getAscent();
int leading = fm。getLeading();
int horizMargin = (size()。width width)/2;
int verMargin = (size()。height height)/2;
g。setColor(Color。white);
g。drawString(label; horizMargin;
verMargin + ascent + leading);
}
public boolean keyDown(Event evt; int key) {
TextField t =
(TextField)parent。h。get(〃keyDown〃);
t。setText(evt。toString());
return true;
}
public boolean keyUp(Event evt; int key) {
TextField t =
(TextField)parent。h。get(〃keyUp〃);
t。setText(evt。toString());
return true;
}
public boolean lostFocus(Event evt; Object w) {
TextField t =
(TextField)parent。h。get(〃lostFocus〃);
t。setText(evt。toString());
return true;
}
public boolean gotFocus(Event evt; Object w) {
TextField t =
(TextField)parent。h。get(〃gotFocus〃);
t。setText(evt。toString());
return true;
}
public boolean
mouseDown(Event evt;int x;int y) {
TextField t =
(TextField)parent。h。get(〃mouseDown〃);
t。setText(evt。toString());
return true;
}
public boolean
mouseDrag(Event evt;int x;int y) {
TextField t =
(TextField)parent。h。get(〃mouseDrag〃);
t。setText(evt。toString());
return true;
}
public boolean
398
…………………………………………………………Page 400……………………………………………………………
mouseEnter(Event evt;int x;int y) {
TextField t =
(TextField)parent。h。get(〃mouseEnter〃);
t。setText(evt。toString());
return true;
}
public boolean
mouseExit(Event evt;int x;int y) {
TextField t =
(TextField)parent。h。get(〃mouseExit〃);
t。setText(evt。toString());
return true;
}
public boolean
mouseMove(Event evt;int x;int y) {
TextField t =
(TextField)parent。h。get(〃mouseMove〃);
t。setText(evt。toString());
return true;
}
public boolean mouseUp(Event evt;int x;int y) {
TextField t =
(TextField)parent。h。get(〃mouseUp〃);
t。setText(evt。toString());
return true;
}
}
public class AutoEvent extends Applet {
Hashtable h = new Hashtable();
String'' event = {
〃keyDown〃; 〃keyUp〃; 〃lostFocus〃;
〃gotFocus〃; 〃mouseDown〃; 〃mouseUp〃;
〃mouseMove〃; 〃mouseDrag〃; 〃mouseEnter〃;
〃mouseExit〃
};
MyButton
b1 = new MyButton(this; Color。blue; 〃test1〃);
b2 = new MyButton(this; Color。red; 〃test2〃);
public void init() {
setLayout(new GridLayout(event。length+1;2));
for(int i = 0; i 《 event。length; i++) {
TextField t = new TextField();
t。setEditable(false);
add(new Label(event'i'; Label。CENTER));
add(t);
h。put(event'i'; t);
}
add(b1);
add(b2);
}
399
…………………………………………………………Page 401……………………………………………………………
} ///:~
我们可以看到构建器使用利用自变量同名的方法,所以自变量被赋值,并且使用 this 来区分:
this。label = label;
paint()方法由简单的开始:它用按钮的颜色填充了一个“圆角矩形”,然后画了一个黑线围绕它。请注意
size()的使用决定了组件的宽度和长度(当然,是像素)。这之后,paint()看起来非常的复杂,因为有大量
的预测去计算出怎样利用“font metrics”集中按钮的标签到按钮里。我们能得到一个相当好的关于继续关
注方法调用的主意,它将程序中那些相当平凡的代码挑出,当我们想集中一个标签到一些组件里时,我们正
好可以对它进行剪切和粘贴。
您直到注意到AutoEvent 类才能正确地理解 keyDown();keyUp()及其它方法的运行。这包含一个Hashtable
(译者注:散列表)去控制字符串来描述关于事件处理的事件和TextField 类型。当然,这些能被静态的创
建而不是放入Hashtable 但我认为您会同意它是更容易使用和改变的。特别是,如果我们需要在AutoEvent
中增加或删除一个新的事件类型,我们只需要简单地在事件列队中增加或删除一个字符串——所有的工作都
自动地完成了。
我们查出在 keyDown(),keyup()及其它方法中的字符串的位置回到 myButton 中。这些方法中的任何一个都
用父句柄试图回到父窗口。父类是一个AutoEvent,它包含Hashtable h 和get()方法,当拥有特定的字符串
时,将对一个我们知道的TextField 对象产生一个句柄(因此它被选派到那)。然后事件对象修改显示在
TextField 中的字符串陈述。从我们可以真正注意到举出的例子在我们的程序中运行事件时以来,可以发现
这个例子运行起来颇为有趣的。
13。14 程序片的局限
出于安全缘故,程序片十分受到限制,并且有很多的事我们都不能做。您一般会问:程序片看起来能做什
么,传闻它又能做什么:扩展浏览器中WEB 页的功能。自从作为一个网上冲浪者,我们从未真正想了解是否
一个WEB 页来自友好的或者不友好的站点,我们想要一些可以安全地行动的代码。所以我们可能会注意到大
量的限制:
(1) 一个程序片不能接触到本地的磁盘。这意味着不能在本地磁盘上写和读,我们不想一个程序片通过WEB
页面阅读和传送重要的信息。写是被禁止的,当然,因为那将会引起病毒的侵入。当数字签名生效时,这些
限制会被解除。
(2) 程序片不能拥有菜单。(注意:这是规定在Swing 中的)这可能会减少关于安全和关于程序简化的麻
烦。我们可能会接到有关程序片协调利益以作为WEB 页面的一部分的通知;而我们通常不去注意程序片的范
围。这儿没有帧和标题条从菜单处弹