zoukankan      html  css  js  c++  java
  • java例程练习(事件监听机制)

    //事件监听机制
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class TestEvent {
        public static void main(String[] args) {
        	Frame f = new Frame("Test");
        	Button b = new Button("Press Me!");
    
        	Monitor bh = new Monitor(); //实现了某种监听器接口的类的对象
    
        	b.addActionListener(bh);    //注册
    
        	f.add(b, BorderLayout.CENTER);
        	f.pack();
        	f.setVisible(true);
        }
    }
    
    //实现了某种监听器接口的类
    class Monitor implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("A button has been pressed");
    	}
    }
    import java.awt.*;
    import java.awt.event.*;
    
    public class TestActionEvent {
    	public static void main(String[] args) {
    		Frame f = new Frame("Test");
    		Button b1 = new Button("Start");
    		Button b2 = new Button("Stop");
    
    		//一个监听器类被两个button监听
    		Monitor2 bh = new Monitor2();
    		b1.addActionListener(bh);
    		b2.addActionListener(bh);
    
    		b2.setActionCommand("Game over");
    
    		f.add(b1,"North");
    		f.add(b2,"Center");
    		f.pack();
    
    		f.setVisible(true);
    
    	}
    
    }
    
    class Monitor2 implements ActionListener {
    	public void actionPerformed (ActionEvent e) {
    		System.out.println("A button has been pressed," +
    			"the relative info is:\n" + e.getActionCommand());
    	}
    }


  • 相关阅读:
    java 网络编程入门
    正确、安全地停止SpringBoot应用服务
    spring boot application.properties文件外部配置
    logback
    基本排序算法的总结
    jqury属性操作,特殊效果
    click事件和jquery选项卡
    jquery加载方式,选择器,样式操作
    css之定位
    css中设置background属性
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671618.html
Copyright © 2011-2022 走看看