• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Java学痴
博客园    首页    新随笔    联系   管理    订阅  订阅

ActionListener01

写一个个按钮触发监听事件

package com.lesson01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TextActionEvent
{
    public static void main(String[] args)
    {
        //按一下按钮触发事件
        Frame frame = new Frame();
        //添加一个按钮
        Button button = new Button();
        MyActionListener myActionListener = new MyActionListener();
        //这里需要一个监听,所以我们这里添加一个监听
        button.addActionListener(myActionListener);
        //添加按钮,并且添加布局(东南西北中)
        frame.add(button,BorderLayout.CENTER);
        //自适应调整布局
        frame.pack();
        //关闭窗口事件
        windowsClose(frame);
        //显示窗 口
        frame.setVisible(true);

    }

    //关闭窗口事件
    private static void windowsClose(Frame frame)
    {
        frame.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

    }
}

//写一个自己的监听方法
class MyActionListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("您成功点击了监听方法!!!");
    }
}

  

  

posted on 2021-11-18 10:54  Java学痴  阅读(45)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3