本帖最后由 Sword 于 2013-5-9 22:25 编辑
package day22;
import java.awt.*;
import java.awt.event.*;
public class MouseAndKeyEent {
private Frame f;
private Button but1,but2;
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);
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");
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MouseAndKeyEent();
}
}
这段代码运行了,但是为什么运行后,我敲键盘的字母却并没有显示呢? |