import java.awt.*;
import java.awt.event.*;
//*****************多监听器范例代码*************
public class MyMulticast
{
public static void main(String [] args)
{
MulticastFrame frame = new MulticastFrame();
frame.show();
}
}
class MulticastFrame extends Frame
{
public MulticastFrame()
{
this.setTitle("MulticastTexting");
this.setSize(300,200);
MulticastPanel panel = new MulticastPanel();
this.add(panel);
this.addWindowListener(new MyWindowAdapter());
}
private class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
}
}
}
class MulticastPanel extends Panel
{
private Button closeAllButton;
public MulticastPanel()
{
Button newButton = new Button("新建");
this.add(newButton);
ActionListener newListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
makeNewFrame();
}
};
newButton.addActionListener(newListener);
closeAllButton = new Button("关闭全部");
this.add(closeAllButton);
}
private void makeNewFrame()
{
//*********************重点部分********************************
final BlankFrame frame = new BlankFrame();// 静态引用变量
frame.show();
ActionListener closeAllListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.dispose();//为什么可以关闭掉所有的窗口呢?实在有点不太明白呀 帮我解释下
}
};
closeAllButton.addActionListener(closeAllListener);//注册监听器
}
//*********************************************************************************
}
class BlankFrame extends Frame
{
private static int counter=0;
public BlankFrame()
{
counter++;
this.setTitle("窗体"+counter);
this.setSize(200,150);
this.setLocation(30*counter,30*counter);
}
}