本帖最后由 文密 于 2013-2-28 21:31 编辑
import java.awt.*;
import java.awt.event.*;
public class GUItest {
public static void main(String[] args) {
new MouseEvent();
}
}
class MouseEvent
{
private Frame f2;
private Button but2;
public MouseEvent()
{
init();
}
public void init()
{
f2=new Frame("my MouseEvent");
f2.setBounds(200,300,300,200); //设置窗体大小位置
f2.setLayout(new FlowLayout());//设置窗体的模式
but2=new Button("my button");
//将组件添加到窗体
f2.add(but2);
myEvent();//调用事件
//显示窗体
f2.setVisible(true);
}
private void myEvent()
{
//窗体事件
f2.addWindowListener(new WindowAdapter(){
//关闭窗体
public void windowClosing(WindowEvent e)
{
System.out.println("退出,OK");
System.exit(0);
}
//激活窗体时
public void windowActivated(WindowEvent e)
{
System.out.println("OK,I'm alive");
}
//最小化时
public void windowIconified(WindowEvent e)
{
System.out.println("最小化");
}
});
//button事件
but2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.out.println("action ok");
//System.exit(0);
}
});
//鼠标事件
but2.addMouseListener(new MouseAdapter(){
private int count1=1;
private int count2=1;
public void mouseEntered(MouseEvent e)
{
System.out.println("鼠标进入组件"+count1++);
}
public void mouseCliked(MouseEvent e)
{
System.out.println("鼠标点击"+count2++);
}
});
//添加键盘监听
but2.addKeyListener(new KeyAdapter(){
public void Pressed(KeyEvent e)
{
System.out.println(KeyEvent.getKeyText(e.getKeyChar())+"..."+e.getKeyCode());
}
});
}
}
上面程序只有 窗体事件和 button事件 起作用,鼠标事件和键盘监听都没有用?请问这是什么问题? |
|