本题的功能是定义自已的组件类。窗口中排布着12个按钮,鼠标移动按钮时.按钮背景颜色改变,用鼠标单击按钮时,后台将显示该按钮对应的字符。 importjflva.awt.*; importjava.awt.event.*; importjava.util.*

admin2013-12-02  27

问题 本题的功能是定义自已的组件类。窗口中排布着12个按钮,鼠标移动按钮时.按钮背景颜色改变,用鼠标单击按钮时,后台将显示该按钮对应的字符。
importjflva.awt.*;
importjava.awt.event.*;
importjava.util.*;
classjava3extendsFrame{
Stringkeys=”123456789*0#”;
java3(    ){
super(”java3”);
addWindowI.istener(newWindowAdapter(    ){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
));
setLayout(newGridI.ayout(4,3,6,6));
for(inti=0;iKeyButtonkb=newKeyButton(keys.charAt
(i));
kb.addkeyl。istener(this);
kb.setBackground(C010r.pink);
kb.setF0reground(C010r.black);
add(kb);
}
setSize(200.200);
show(    );
}
classKeyEventHandlerextendsKeyAdapter{
publicvoidkeyTyped(KeyEventevt){

Syatem.out.println(evt.getChat(    ));
}
}
publicstaticvoidmain(String[]args){
newjava3(    )I
}
}
ClassKeyButtonextendsComponent{
Keyl,istenerkeyListener;
booleanhighlighted;
charkey;
KeyButton(chark){
this.key=k;
addMouseListener(newMouseEventHandler(    ));
}
publicvoidpaint(Graphicsg){
intW=getSize(    ).width;
inth=getSize(    ).height;
Strings=””+key!
FontMetricsfm=g.getFontMetrics(    );
if(highlighted){
g.setColor(getBackground(    ));
g.fillR0undRect(0,0,w一1,h一1,10,10);
}
g.setColor(getF0reground(    ));
g.drawRoundRect(0.0,w一1。h一1,10.10);
g.drawString(s,(w—fm.stringWidth(s))12.
(h—fm.getHeight(    ))/2十fm.getAscent(    ));
}
classMouseEventHandlerextendsMouseAdapter{
publicvoidmouserPressed(MouseEventevt);
if(keyListener!=null){
keyListener.keyTyped(
FlewKeyEfent(KeyButton.this,KeyEvent.KEY—
TYPE@D@
System.currentTimeMillis(    )。
O,KeyEvent.VK—UNDEFINED,key));
}
}
publicvoidmouseEntered(MouseEventevl){
highlighted=true;
repaint(    );
}
public:voidmouseExited(MouseEventevt){
highlighted=false;
repaint(    );
}
}
publicsynchronizedvoidaddKeyListener(KeyLis—
tener1){
keylListener=AWTEventMuhicaster.add(key—
Listener,1);
}
publicsynchronizedvoidremoveKeyListener(Key—
Listenetl){
keyListener=AWTEventMulticaster.remove
(keyListener,1);
}
}

选项

答案第1处:inti=0;i
解析 第1处是作为遍历keys中元素的条件.而keys中的字符元素就是各个按钮对应的字符;第2处注册键盘事件监听功能.KeyButton实现了KeyLlstener接口,而kb是KeyButton类的对象,所以它是合格的监听者;第3处将从键盘读到的字符输出。
转载请注明原文地址:https://jikaoti.com/ti/MgkiFFFM
0

相关试题推荐
随机试题
最新回复(0)