JAVA-事件监听机制

image

package com.itheima;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JFrame11 {
    public static void main(String[] args) {
        JFrame jf=new JFrame();
        jf.setTitle("事件监听机制");
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);        //取消窗体的默认布局

        //创建按钮
        JButton jButton=new JButton("你点我啊");
        jButton.setBounds(0,0,100,100);
        jf.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("你过来啊");
            }
        });


        //添加按钮到窗体中
        jf.setVisible(true);


    }
}

image

posted @ 2022-10-29 23:38  NiceTwocu  阅读(23)  评论(0编辑  收藏  举报