zoukankan      html  css  js  c++  java
  • 【Java】-NO.16.EBook.4.Java.1.011-【疯狂Java讲义第3版 李刚】- AWT

     1.0.0 Summary

    Tittle:【Java】-NO.16.EBook.4.Java.1.011-【疯狂Java讲义第3版 李刚】-  AWT

    Style:EBook

    Series:Java

    Since:2017-09-30

    End:....

    Total Hours:...

    Degree Of Diffculty:2

    Degree Of Mastery:2

    Practical Level:2

    Desired Goal:2

    Archieve Goal:....

    Gerneral Evaluation:...

    Writer:kingdelee

    Related Links:

    http://www.cnblogs.com/kingdelee/

    1.

     2.事件适配器

    事件适配器是监听器接口的空实现。用于需要创建监听器时,通过集成的方式来创建,只需要重新自己感兴趣的方法即可。

    // 1.事件适配器是监听器接口的空实现。用于需要创建监听器时,通过集成的方式来创建,只需要重新自己感兴趣的方法即可
    public class WindowAdapterTest
    {
        private Frame f = new Frame("测试");
    	private TextArea ta = new TextArea(6 , 40);
    	public void init()
    	{
    		f.addWindowListener(new MyListener());
    		f.add(ta);
    		f.pack();
    		f.setVisible(true);
    	}
    	class MyListener extends WindowAdapter
    	{
    		public void windowClosing(WindowEvent e)
    		{
    			System.out.println("用户关闭窗口!
    ");
    			System.exit(0);
    		}
    	}
    	public static void main(String[] args)
    	{
    		new WindowAdapterTest().init();
    	}
    }
    

      

    // 1.使用外部类的形式构造适配器,不太推荐这种写法,这里将业务逻辑和显式逻辑耦合不好
    public class MailerListener implements ActionListener
    {
        // 该TextField文本框用于输入发送邮件的地址
    	private TextField mailAddress;
    	public MailerListener(){}
    	public MailerListener(TextField mailAddress)
    	{
    		this.mailAddress = mailAddress;
    	}
    	public void setMailAddress(TextField mailAddress)
    	{
    		this.mailAddress = mailAddress;
    	}
    	// 实现发送邮件
    	public void actionPerformed(ActionEvent e)
    	{
    		System.out.println("程序向“"
    			+ mailAddress.getText() + "”发送邮件...");
    		// 发送邮件的真实实现
    	}
    }
    
    
    
    public class SendMailer
    {
        private Frame f = new Frame("测试");
    	private TextField tf = new TextField(40);
    	private Button send = new Button("发送");
    	public void init()
    	{
    		// 使用MailerListener对象作为事件监听器
    		send.addActionListener(new MailerListener(tf));
    		f.add(tf);
    		f.add(send , BorderLayout.SOUTH);
    		f.pack();
    		f.setVisible(true);
    	}
    	public static void main(String[] args)
    	{
    		new SendMailer().init();
    	}
    }
    

      

    // GUI界面类继承WindowAdapter作为事件监听器类
    public class SimpleEventHandler extends WindowAdapter
    {
        private Frame f = new Frame("测试");
    	private TextArea ta = new TextArea(6 , 40);
    	public void init()
    	{
    		// 将该类的默认对象作为事件监听器对象
    		f.addWindowListener(this);
    		f.add(ta);
    		f.pack();
    		f.setVisible(true);
    	}
    	// GUI界面类直接包含事件处理器方法
    	public void windowClosing(WindowEvent e)
    	{
    		System.out.println("用户关闭窗口!
    ");
    		System.exit(0);
    	}
    	public static void main(String[] args)
    	{
    		new SimpleEventHandler().init();
    	}
    }
    

      

    public class AnonymousEventHandler
    {
        private Frame f = new Frame("测试");
    	private TextArea ta = new TextArea(6 , 40);
    	public void init()
    	{
    		// 以匿名内部类的形式来创建事件监听器对象
    		f.addWindowListener(new WindowAdapter()
    		{
    			// 实现事件处理方法
    			public void windowClosing(WindowEvent e)
    			{
    				System.out.println("用户试图关闭窗口!
    ");
    				System.exit(0);
    			}
    		});
    		f.add(ta);
    		f.pack();
    		f.setVisible(true);
    	}
    	public static void main(String[] args)
    	{
    		new AnonymousEventHandler().init();
    	}
    }
    

      

  • 相关阅读:
    hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数
    2019 上半年 南昌网络赛
    第十章 存储过程和函数
    第九章 Mysql函数
    第八章 Mysql运算符
    第七章 插入、更新与删除数据
    第六章 查询数据
    第五章 触发器
    第四章 视图
    第三章 索引
  • 原文地址:https://www.cnblogs.com/kingdelee/p/7615597.html
Copyright © 2011-2022 走看看