事件监听

复制代码
public class demo06 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("hahahhaha");

        frame.add(button);
        frame.setVisible(true);
        frame.setSize(100,100);

        button.addActionListener(new MyActionListener());
        windowClose(frame);

    }
    public static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}
 class MyActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        System.out.println("hahahahahahahahah");
    }


}
复制代码

为按钮添加监听事件,构造一个内部类实现ActionListener接口,重写里面的方法

 

 

 

 

关闭窗体的方法,将窗体对象作为参数,这样新建一个船体的时候就不需要重新添加窗体监听器了,只需将其作为参数执行这个方法

 

posted on   大风吹过12138  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示