GUI-Event-鼠标等

鼠标键盘、几乎所有的事件都具备,所以要直接去找compent  、 然后就找到了addKeyListener   AddMouseListener 

常见的事件:  Action事件、 鼠标键盘 

 

下面方法:包括、 键盘监听、鼠标监听 、  以及关于双击等的操作,

import java.awt.*;
import java.awt.event.*;
public class first {

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

}

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,200,500,300);
        f.setLayout(new FlowLayout());
        
        tf = new TextField(20);
        
        but = new Button("my botton");
        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.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        System.out.println("action ok");
                    }
                });
        but.addKeyListener(new KeyAdapter()
                {
                    public void keyPressed(KeyEvent e)
                    {
                        //如果想按下  ctrl+Enter关闭文件      即组合键         VK_ENTER 为回车的常量
                        if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
                            System.out.println("ctrl+enter is run");
                        
//                        System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"....."+e.getKeyCode());
                        //getKeyCode(),是找到对应键值的编码、getKeyText(),通过编码返回键盘输入的键是什么。
                    }
                });
        
        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)
                    {
                        if(e.getClickCount()==2)   //单机可以改到双击  、MouseEvent当中的getClickCount 是判断双击的方法
                            System.out.println("双击动作"+clickCount++);
                    }
                });
    } 
}

 

posted @ 2019-11-11 15:23  蚂蚁雅黑1010  阅读(148)  评论(0编辑  收藏  举报