码迷,mamicode.com
首页 > 编程语言 > 详细

java鼠标与键盘事件监听

时间:2015-09-06 17:53:44      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

package cn.stat.p3.windowdemo;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class shijiandemo {
    
    private Frame f;
    private TextField tf;
    private Button but;
    
    

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new shijiandemo() ;
    }



    public shijiandemo() {
        super();
        init();
    }



    private void init() {
        f=new Frame("鼠标与键盘事件监听");
        
        f.setBounds(200,200,400,400);
        
        f.setLayout(new FlowLayout());
        
        tf=new TextField(15);
        but=new Button("这是一个按钮");
        f.add(tf);
        f.add(but);
        
        f.setVisible(true);
        myevent();
        
    }



    private void myevent() {
        
        //添加键盘事件
        tf.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                // 设定只能输入数字
                int code=e.getKeyCode();
                if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))
                {
                    System.out.println("必须是数字");
                    e.consume();
                }
                    
                
            }
        });
        
        //添加窗体事件
        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
        });
        
        //添加鼠标事件
        but.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount()==1)
                {
                    tf.setText("单击了");
                }
                if(e.getClickCount()==2)
                {
                    tf.setText("双击了");
                }
            
            }

            private int count=0;

            @Override
            public void mouseEntered(MouseEvent e) {
                
                but.setLabel("鼠标触发了"+count++);
            }
        });
        
    }

}

 

java鼠标与键盘事件监听

标签:

原文地址:http://www.cnblogs.com/zywf/p/4786286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!