AWT直接使用两种事件管理器来实现对窗口和按钮监听

package guiawt;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;

public class Two
{
	public static void main(String[] args)
	{
		Frame frame = new Frame("MY FRAME");
		Button button = new Button("HELLO");
		button.addActionListener(new MyListener());     //实现button按钮的监听
		
		frame.add(button);
		frame.addWindowListener(new MyWindowListener());  //实现窗口推出时的监听,并输出对应的信息
		
		frame.setSize(500,500);
		frame.setVisible(true);							//只有设置为可显示时才可以看到,所有的frame框架
		
		
	}
	
}

class MyListener implements ActionListener
{
	
	
	public void actionPerformed(ActionEvent e)
	{
		long milliSeconds = e.getWhen();
		Date date = new Date(milliSeconds);
		System.out.println(date.toLocaleString());  //java 1.5 up support (SuppressWarnings("deprecate"))
		
	}
}
class MyWindowListener implements WindowListener		//直接使用source中的Ovrride /implements直接调用方法
{

	public void windowOpened(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e)			//重写windowClosing方法中内容
	{
		
		System.out.println("windsclose");
		System.exit(0); // 直接退出jvm
		
	}

	public void windowClosed(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowActivated(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void windowDeactivated(WindowEvent e)
	{
		// TODO Auto-generated method stub
		
	}
	
}
 
 
 
 


java中所有的添加事件管理都是 addXXXListener

你可能感兴趣的:(jvm,Date,框架,String,Class,button)