隨筆-159  評論-114  文章-7  trackbacks-0
          由于本人已經有了很多界面開發經驗,對于這塊不是重點,只列舉一些例子。

          JSF和Web Start(jnlp+jar),將逐步替代Swing和Applet的地位。

          圖形界面開發,步驟一定要記住。

          1.選擇容器
          2.設置布局管理器,Panel默認為Flow,Frame默認為Border
          3.添加組件,可能是容器組件的多層嵌套
          4.添加時間處理,add***Listener,并給出實現。

          事件模型是重點.

          import java.util.*;
          public class NumberSource{
              NumberListener listener
          =null;
              
          public void addNumberListener(NumberListener nl){
                  
          if (listener==null) listener=nl;
              }

              
              
          public void run(){
                  
          for(long i=1l;i<=200000l;i++){
                      
          if (i%10000==0{
                          NumberEvent ne
          =new NumberEvent(this,(int)(i/10000));
                          listener. print(ne);
                      }

                  }

              }

              
              
          public static void main(String[] args){
                  NumberSource ns
          =new NumberSource();
                  MyNumberListener ls
          =new MyNumberListener();
                  ns.addNumberListener(ls);
                  ns.run();
              }

          }


          class NumberEvent extends EventObject{
              
          int time;
              
          public NumberEvent(Object src,int time){
                  
          super(src);
                  
          this.time=time;
              }

              
          public int getTime(){
                  
          return time;
              }

              
          }


          interface NumberListener extends EventListener{
              
          void print(NumberEvent ne);
          }


          class MyNumberListener implements NumberListener{
              
          public void print(NumberEvent ne){
                  System.out.println(ne.getTime()
          +"    10000");
              }

          }


          主要部分有事件源,事件對象,該事件的監聽器接口,接口實現類。

          事件源內部有一個事件監聽器接口類型的對象,因為它只關心在相應的事件處理方法,不關心具體類怎么實現的,也不關心具體類的其他屬性。

          需要建立事件源與監聽器之間的聯系,通過add***Listener方法搭橋,然后在事件源fire的時候調用監聽器方法,得到響應和處理。


          下面為實際的基于觀察者模式的事件驅動模型。Girl為事件源,如果有情緒波動,它會調用監聽她的監聽器方法,而由于Girl可以更換男友,那么監聽器的實現也會隨之變化。


          import java.util.*;
          public class Girl
          //  private ArrayList listeners=new ArrayList(); 
           private EmotionListener listener=null;
            
            
          private String name; 
            
          private int day; 

            
          public Girl (String lover)
              
          this.name = lover; 
            }
           

            
          public String getLover() return name; } 

            
          public void setEmotionListener(EmotionListener l) throws HeartOccupiedException
               
          /*
                if (listeners.size()<10){
                    listeners.add(l);
                }
                
          */

                
                
          if (listener==null){
                    listener
          =l;
                    System.out.println(
          "I have a Boy Friend");
                }

                
                
          else {
                    System.out.println(
          "I already have a bf");
                    
          throw new HeartOccupiedException("Go Away!");
                 }

                    
             }
           

          public void removeEmotionListener(EmotionListener l)
               
          /*
                if(listeners.contains(l)){
                   System.out.println("I can't live without you");
                   listeners.remove(l);
                }
                
          */

                
          if (listener==l) {
                    listener
          =null;
                    System.out.println(
          "I can't live without you");
                }

          }
           
          public void run()
              
          for(day=1;day<=10;day++)
                
          try
                  Thread.sleep(
          1000); 
                  fire(); 
                }
          catch(Exception e){} 
             
            }
           
            
          }
           
          private void fire ()
            EmotionEvent evt 
          = new EmotionEvent(this); 
              
              
          if(listener == null){
                System.out.println(
          "Being a single is lone.");
                 
          return;
               }

               
              
          if(day % 2 == 0)  {
                  
          /*
                  Iterator it=listeners.iterator();
                  while(it.hasNext()){
                      EmotionListener l=(EmotionListener)it.next();
                      l.happy(evt);
                  }
                  
          */

                  listener.happy(evt);
                  
              }

              
          else{
                  
          /*
                  Iterator it=listeners.iterator();
                  while(it.hasNext()){
                      EmotionListener l=(EmotionListener)it.next();
                      l.sad(evt);
                  }
                  
          */

                  
                  listener.sad(evt);
              }

            }
           

          public static void main(String[] args){
              Girl girl
          =new Girl("Anna");
          //    Girl girl2=new Girl("Marry");
              Boy boy1=new Boy("HuXinzhe");
              
          /*
              Boy boy2=new Boy("LiuChunyang");
              Boy boy3=new Boy("LiuXinfu");
              Boy boy4=new Boy("ZhaoDekui");
              Boy boy5=new Boy("ChenZongquan");
              Boy boy6=new Boy("Jerry");
              Boy boy7=new Boy("Larry");
              Boy boy8=new Boy("George");
              Boy boy9=new Boy("WangHaige");
              Boy boy10=new Boy("CuiJixin");
              
          */

              
              
          try{
                  girl.setEmotionListener(boy1);
                  
          /*
                  girl.setEmotionListener(boy2);
                  girl.setEmotionListener(boy3);
                  girl.setEmotionListener(boy4);
                  girl.setEmotionListener(boy5);
                  girl.setEmotionListener(boy6);
                  girl.setEmotionListener(boy7);
                  girl.setEmotionListener(boy8);
                  girl.setEmotionListener(boy9);
                  girl.setEmotionListener(boy10);
                  
          */


              
          //    girl2.setEmotionListener(boy1);
              
          //    girl.setEmotionListener(boy2);
              }

              
          catch(HeartOccupiedException e){
                  System.out.println(e.getMessage());
              }

              
              girl.run();
              
              girl.removeEmotionListener(boy1);
              
          //    girl2.run();
              
          //    girl2.removeEmotionListener(boy1);
          }


          }

                   


          class EmotionEvent extends java.util.EventObject
              
          public EmotionEvent(Object src)
                  
          super(src); 
              }
           
          }
           


          interface EmotionListener extends java.util.EventListener
              
          public void happy(EmotionEvent evt); 
              
          public void sad(EmotionEvent evt); 
          }
           


          class Boy implements EmotionListener{
              
          private String name;
              
          public Boy(String name)this.name = name;} 
              
          public void happy(EmotionEvent evt){
                              String lover 
          = ((Girl)evt.getSource()).getLover();
                              System.out.println(name
          +" said:  "+lover +  ", I am also happy"); 
              }
           
              
          public void sad(EmotionEvent evt){
                              String lover 
          = ((Girl)evt.getSource()).getLover();
                              System.out.println(name
          +" said:  "+lover + ", take care, you make me sad too."); 
              }
           
          }
           

          class HeartOccupiedException extends Exception
              
          public HeartOccupiedException()
                  
          this("No reason"); 
              }
           

              
          public HeartOccupiedException(String mesg)
                  
          super(mesg); 
              }
           
          }
           






          posted on 2005-12-11 21:15 北國狼人的BloG 閱讀(321) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
          主站蜘蛛池模板: 顺昌县| 靖宇县| 宁阳县| 仁布县| 靖江市| 潜江市| 保德县| 青海省| 长岛县| 东源县| 和林格尔县| 白玉县| 德兴市| 康平县| 北流市| 莱芜市| 谢通门县| 文登市| 新和县| 南康市| 沙雅县| 普兰店市| 策勒县| 鞍山市| 湖口县| 德江县| 油尖旺区| 禹城市| 章丘市| 娄底市| 濮阳市| 思茅市| 永善县| 苍溪县| 雅安市| 蒙城县| 利辛县| 丰镇市| 凤翔县| 南阳市| 鸡泽县|