本帖最后由 Sword 于 2013-5-9 22:19 编辑
package day22;
import java.awt.*;
import java.awt.event.*;
public class MouseAndKeyEent {
private Frame f;
private Button but1,but2;
private TextField tf;
MouseAndKeyEent()
{
init();
}
public void init()
{
f=new Frame("my frame");
f.setBounds(500,400,500, 400);
f.setLayout(new FlowLayout());
but1=new Button("my button");
f.add(but1);
but2=new Button("key button");
f.add(but2);
f.add(tf);
myEvent();
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but1.addMouseListener(new MouseAdapter(){
private int count=1;
private int click=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入该区域"+count++);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("click"+click++);
}
});
but2.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
System.out.println(e.getKeyChar()+"key event");
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int code=e.getKeyCode();
if(!(code>=KeyEvent.VK_0 &&code<=KeyEvent.VK_9))
System.out.println(code+"....非法");
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MouseAndKeyEent();
}
}
毕老师视频中有以上一个代码,但是我的老是提示出错。请高手指点。
|