GUI常用监听事件

概念

对鼠标、键盘等一系列事件做出相应的反馈

  • 事件监听
//创建监听事件
public class Demo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        Button button = new Button("start");

        //添加监听器
        //根据参数我们可以知道需要添加一个监听器类,所以需要什么,我们就new什么
        MyListener myListener = new MyListener();
        button.addActionListener(myListener);

        frame.add(button);
        windowClose(frame);
        frame.pack();//自动控制大小
        frame.setVisible(true);

    }

    //实现关闭窗口监听
    private static void windowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
    }


    static class MyListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("产生事件响应...");
        }
    }
}
  • 多事件监听
public class Demo01 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");

        //设置监听
        MyListener myListener = new MyListener();
        btn1.setActionCommand("btn1");
        btn2.setActionCommand("btn2");
        btn1.addActionListener(myListener);
        btn2.addActionListener(myListener);

        frame.setLayout(new GridLayout(1,2));
        frame.add(btn1);
        frame.add(btn2);
        frame.pack();
        frame.setVisible(true);
    }


    //实现共同监听事件
    static class MyListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(actionEvent.getActionCommand()+"发出响应。");
            //通过监听事件返回值,判断
            if (actionEvent.getActionCommand().equals("btn1")){
                System.out.println("start");
            }
        }
    }
}

常用监听:

一、文本监听 textField()

public class TextListener {
    public static void main(String[] args) {
        TextFrame textFrame = new TextFrame("文本测试窗口");

        //新建文本框
        TextField textField = new TextField();

        textFrame.add(textField,BorderLayout.CENTER);
        textFrame.setVisible(true);
    }

}

//将窗口类封装
class TextFrame extends Frame{
    public TextFrame(String title){
        super(title);
        this.setBounds(200,200,500,400);
    }
}

二、鼠标监听

public class MouseListener {
    public static void main(String[] args) {
        mouseFrame frame = new mouseFrame("鼠标监听测试窗口");

        //添加鼠标监听
        frame.addMouseListener(new myMouse());

        frame.setVisible(true);
    }
}

//适配器模式
class myMouse extends MouseAdapter{
    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
        System.out.println("点击");
        //获取鼠标位置
        System.out.println("鼠标位置为("+mouseEvent.getX()+","+mouseEvent.getY()+")");
    }
}

class mouseFrame extends Frame{
    public mouseFrame(String title){
        super(title);
        this.setBounds(200,200,500,400);
    }
}

三、窗口监听

//实现窗口监听
class windowClose extends WindowAdapter{
    @Override
    public void windowClosing(WindowEvent windowEvent) {
        System.exit(0);
    }
}

补充:适配器模式的作用,个人理解为避免实现不必要的接口方法,即只想实现接口的其中几个方法,采用一个实现所有接口的类(适配器),通过继承这个适配器来实现(重载)某个方法。
在这里插入图片描述

原文地址:https://www.cnblogs.com/shimmernight/p/13441720.html