为窗体添加事件

package frank;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 为窗体添加事件
 * */
public class App
{
	private Frame f = new Frame("测试");
	private TextArea ta = new TextArea(6,40);
	public static void main(String[] args)
	{
		new App().init();			
	}
	void init()
	{
		f.addWindowListener(new MyListener());
		f.add(ta);
		f.pack();
		f.setVisible(true);
	}
	class MyListener implements WindowListener
	{
		public void windowActivated(WindowEvent e)
		{
			ta.append("窗口被激活!\n");
		}
		public void windowClosed(WindowEvent e)
		{
			ta.append("窗口被成功关闭!\n");
		}
		public void windowClosing(WindowEvent e)
		{
			ta.append("用户关闭窗口!\n");
			Dialog d = new Dialog(f,"模式对话框!",true);
			d.setVisible(true);
			System.exit(0);
		}
		public void windowDeactivated(WindowEvent e)
		{
			ta.append("窗口失去焦点!\n");	
		}
		public void windowDeiconified(WindowEvent e)
		{
			ta.append("窗口被恢复!\n");	
		}
		public void windowIconified(WindowEvent e)
		{
			ta.append("窗口被最小化!\n");	
		}
		public void windowOpened(WindowEvent e)
		{
			ta.append("窗口初次被打开!\n");	
		}
	}
}

  

posted on 2013-10-26 14:50  wp456  阅读(244)  评论(0)    收藏  举报

导航