import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
private Frame f;
private Button but;
MouseAndKeyEvent()
{
init();
}
public void init()
{
f=new Frame("wode chuangkou");
but=new Button("anyixia");
f.setBounds(300,200,600,500);
f.setLayout(new FlowLayout());
f.add(but);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addKeyListener(new KeyAdapter()
{
public void KeyPressed(KeyEvent e)
{
System.out.println(e.getKeyChar()+"..."+e.getKeyCode());
}
});
/*
but.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
System.out.println("andaoshangmle");
}
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount()==2)
{
add();
}
}
});
*/
}
public void add()
{
int a=5,b=5;
int sum=a+b;
System.out.println("a加b的结果为"+sum);
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
}
}
这个键盘事件,该有的都有了啊,为什么按键之后,dos里没反应呢?
|
|