AWT重点理解事件监听器

import java.awt.*;
import java.awt.event.*;

class AwtDemo2
{

	private Frame f;
	private Button but;

	AwtDemo2()
	{
		init();
		myEvent();

		f.setVisible(true);
	}

	public void init()
	{
		f = new Frame("awtdemo2");

		f.setBounds(300,200,600,500);

		f.setLayout(new FlowLayout());

		but = new Button("my button");
		f.add(but);
	}
	private void myEvent()
	{

		but.addActionListener(new ActionListener()
		{
			private int count = 1;
			public void actionPerformed(ActionEvent e)
			{
//				System.out.println(e.getSource());

				Button b = (Button)e.getSource();

//				System.out.println(b.getParent());
				Frame ff = (Frame)b.getParent();

				ff.add(new Button("new button-"+count++));
				ff.validate();
				/*
				f.add(new Button("new button-"+count++));
				f.validate();
				*/
			}
		});
		f.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args) 
	{
		new AwtDemo2();
	}
}

 

你可能感兴趣的:(监听器)