JAVA的事件处理(上)

时间:2022-12-19 23:19:19

一.动作事件(ActionEvent)
接口:

ActionListener

接口中只包含一个方法:

public void actionPerformed(ActionEvent e)

动作事件中用到的一些方法

getActionCommand() //用来获取触动动作事件的事件源命令符
getSource() //获取发生Action事件的事件源对象的引用

动作事件的两种使用方法:
1.直接给事件添加监听:

public class Jpan2 extends JFrame {

    public Jpan2() {
        super("as");
        setSize(200,200);
        setLayout(new FlowLayout());

        JButton jb=new JButton("as1");
        jb.addActionListener(new ActionListener() {
//给按钮添加监听
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println(arg0.getActionCommand());
                System.out.println(arg0.getSource());
            System.exit(0);

                jb.setText("zxc");

                });
            }

        });
        add(jb);
        setVisible(true);
    }

    public static void main(String[] args) {

        new Jpan2();
    }
    }

2.用ActionListener接口

public class Jpan2 extends JFrame implements ActionListener{

    public Jpan2() {
        super("as");
        setSize(200,200);
        setLayout(new FlowLayout());

        JButton jb=new JButton("as1");
        jb.addActionListener(this);
                add(jb);
        setVisible(true);
    }

    public static void main(String[] args) {

        new Jpan2();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        System.exit(0);
    }
}

二.焦点事件监听(FocusEvent)
组件获得焦点或失去焦点时(文本框里的光标)都会产生焦点事件,
接口:

FocusListener

接口中要实现的方法

public void focusGained(FocusEvent e)//获取焦点时会调用
public void focusLost(FocusEvent e)//失去焦点时调用

与动作事件相同焦点事件同样有两种实现方法,这里我只以第一种为例

public class Jpan3 extends JFrame{
public Jpan3() {
    super("dsacv");
    setSize(200,200);
    setLayout(new FlowLayout());
    JTextField jf=new JTextField("asd",5);
    JTextArea as=new JTextArea(5,5);
    as.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent arg0) {
            // TODO Auto-generated method stub
            jf.setText("zxc");

        }

        @Override
        public void focusLost(FocusEvent arg0) {
            // TODO Auto-generated method stub
            jf.setText("qwe");
        }

    });
    add(as);
    add(jf);
    setVisible(true);
}

    public static void main(String[] args) {
        new Jpan3();
    }
}

三.键盘事件(KeyEvent)
按下或释放键盘时,会触发键盘事件
接口:

KeyListener

接口中要实现的方法:
“`
public void keyPressed(KeyEvent arg0)//按下键时调用
public void keyReleased(KeyEvent arg0)//松开键时调用
public void keyTyped(KeyEvent arg0)//敲击键时调用

键盘事件常用的方法

getKeyCode()//获取与此事件键相关联的整数keycode
getKeyText(int keycode )//获取描述此keycode的标签
getKeyChar()//获取与此事件键相关的字符
isControlDown()//是否按下了CTRL键
isAltDown()是否按下alt键
isShiftDown()是否按下shift键


与动作事件相同焦点事件同样有两种实现方法,这里我只以第一种为例

public class Jpan3 extends JFrame{
public Jpan3() {
super(“dsacv”);
setSize(200,200);
setLayout(new FlowLayout());
JTextField jf=new JTextField(“asd”,5);
JTextArea as=new JTextArea(5,5);
jf.addKeyListener(new KeyListener() {

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyChar()=='a') {
         as.append("q");

        System.out.println(arg0.getKeyCode());
        System.out.println(arg0.getKeyText(65));}
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

// as.append(“a”);
}

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

// as.append(“z”);
}

});
add(as);
add(jf);
setVisible(true);

}

public static void main(String[] args) {
    new Jpan3();
}

}

“`