复制代码
- //:DynamicEvents.java
- //ThenewJava1.1eventmodelallowsyouto
- //changeeventbehaviordynamically.Also
- //demonstratesmultipleactionsforanevent.
- importjava.awt.*;
- importjava.awt.event.*;
- importjava.util.*;
- publicclassDynamicEventsextendsFrame{
- Vectorv=newVector();
- inti=0;
- Button
- b1=newButton("Button1"),
- b2=newButton("Button2");
- publicDynamicEvents(){
- setLayout(newFlowLayout());
- b1.addActionListener(newB());
- b1.addActionListener(newB1());
- b2.addActionListener(newB());
- b2.addActionListener(newB2());
- add(b1);
- add(b2);
- }
- classBimplementsActionListener{
- publicvoidactionPerformed(ActionEvente){
- System.out.println("Abuttonwaspressed");
- }
- }
- classCountListenerimplementsActionListener{
- intindex;
- publicCountListener(inti){index=i;}
- publicvoidactionPerformed(ActionEvente){
- System.out.println(
- "CountedListener"+index);
- }
- }
- classB1implementsActionListener{
- publicvoidactionPerformed(ActionEvente){
- System.out.println("Button1pressed");
- ActionListenera=newCountListener(i++);
- v.addElement(a);
- b2.addActionListener(a);
- }
- }
- classB2implementsActionListener{
- publicvoidactionPerformed(ActionEvente){
- System.out.println("Button2pressed");
- intend=v.size()-1;
- if(end>=0){
- b2.removeActionListener(
- (ActionListener)v.elementAt(end));
- v.removeElementAt(end);
- }
- }
- }
- publicstaticvoidmain(String[]args){
- Framef=newDynamicEvents();
- f.addWindowListener(
- newWindowAdapter(){
- publicvoidwindowClosing(WindowEvente){
- System.exit(0);
- }
- });
- f.setSize(300,200);
- f.show();
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |