Java-GUI(鼠标事件)-键盘事件

来源:互联网 发布:斗战三国志进阶数据 编辑:程序博客网 时间:2024/06/12 01:35
import java.awt.*;import java.awt.event.*; class MouseAndKeyEvent{    private Frame f;    private Button but;    private TextField tf;         MouseAndKeyEvent()    {        init();    }         public void init()    {        f = new Frame("my frame");                 //对frame进行基本设置。        f.setBounds(300,100,600,500);        f.setLayout(new FlowLayout());                 tf = new TextField(20);                 but = new Button("my button");                 //将组件添加到frame中        f.add(tf);        f.add(but);                 //加载一个窗体上事件。        myEvent();                 //显示窗体        f.setVisible(true);    }              private void myEvent()    {        f.addWindowListener(new WindowAdapter()        {            public void windowClosing(WindowEvent e)            {                System.exit(0);            }        });                 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+"...是非法的");                    e.consume();                }            }        });                 //给But添加一个键盘监听。        but.addKeyListener(new KeyAdapter()        {            public void keyPressed(KeyEvent e )            {                if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)                    System.out.println("ctrl+enter is run");                                     // System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());            }        });                 /* but.addActionListener(new ActionListener()        {            private int action = 1;            public void actionPerformed(ActionEvent e)            {                System.out.println("action ok---"+action++);            }        });                 but.addMouseListener(new MouseAdapter()        {            private int count = 1;            private int clickCount = 1;            public void mouseEntered(MouseEvent e)            {                System.out.println("鼠标进入到该组件"+count++);            }            public void mouseClicked(MouseEvent e)            {                System.out.println("点击动作"+clickCount++);            }        }); */    }       public static void main(String[] args)    {        new MouseAndKeyEvent();    }}

0 0
原创粉丝点击