zoukankan      html  css  js  c++  java
  • GUI 监听事件

    编写代码 TestActionEvent测试类

    package com.xiang.lesson02;
    
    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 TestActionEvent {
        public static void main(String[] args) {
            Frame frame = new Frame();
            Button button = new Button("btn1");
    
    //        因为addActionListener 需要一个  ActionListener ,所以就构造了一个ActionListener
            MyActionListener listener = new MyActionListener();
            button.addActionListener(listener);
    
            frame.add(button, BorderLayout.CENTER);
    
            frame.setVisible(true);
            frame.setBounds(400, 400, 400, 400);
            frame.setBackground(Color.yellow);
            WindowClose(frame);
        }
    
        //关闭窗体事件
        private static void WindowClose(Frame frame) {
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    
    // ActionListener 事件监听
    class MyActionListener implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("MyActionListener");
        }
    };
    

    运行结果

  • 相关阅读:
    sqlalchemy 基本操作
    Codeforces 716A Crazy Computer
    Codeforces 719B Anatoly and Cockroaches
    POJ 1067 取石子游戏
    P1028 数的计算
    P1914 一串字母
    P1308 统计单词数
    P1200 你的飞碟在这儿
    P1055 书号
    P1567 气温统计
  • 原文地址:https://www.cnblogs.com/d534/p/15104789.html
Copyright © 2011-2022 走看看