熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

使用Decorator模式實現日期選擇組件(4)

2022-06-13   來源: Java高級技術 

  標題
  這Date_selector_panel是重要部分現在我們來看看他的裝飾Titled_date_selector類只做一件事情給未裝飾的日歷增加個標題這是對實現了Date_selector的JPanel面板的包裝它只顯示現有的Date_selector和顯示日期的標簽下面的實現是微不足道和用戶界面無相關值得說的只有那其中行動作監聽代碼監聽器獲取日期改變通知(通過用戶導航或者方法調用)便做相應的標簽日期更新其他代碼只是簡單地創建面板和將Date_selector與JLable標題包裝進面板中
  這表明這部分代碼易寫易管理比較簡單如果它被混合在Date_selector_panel中將會在 沒有明顯的優點的情況下增加了代碼的復雜度(代碼有組織地放在某處比全混合在一個地方更加清晰)如果我想要標題更加美觀只需要修改這一個類即可以實現根本就不需要動其他部分
  
  public class Titled_date_selector extends JPanel implements Date_selector
  {  private    Date_selector selector;
    private final JLabel title = new JLabel(XXXX);
  
    /** Wrap an existing Date_selector to add a title bar showing
     * the displayed month and year The title changes as the
     * user navigates
     */
  
    public Titled_date_selector( Date_selector selector )
    {  thisselector = selector;
  
      titlesetHorizontalAlignment(SwingConstantsCENTER);
      titlesetOpaque   ( true                   );
      titlesetBackground ( comholubuiColorsLIGHT_YELLOW     );
      titlesetFont    ( titlegetFont()deriveFont( FontBOLD )  );
  
      selectoraddActionListener
      (  new ActionListener()
        {  public void actionPerformed( ActionEvent e )
          {  if( egetID() == Date_selector_panelCHANGE_ACTION )
              titlesetText( egetActionCommand() );
            else
              my_subscribersactionPerformed(e);
          }
        }
      );
  
      setOpaque(false);
      setLayout( new BorderLayout() );
      add( title BorderLayoutNORTH );
      add( (JPanel)selector BorderLayoutCENTER );
    }
  
    /** This constructor lets you specify the background color of the
     * title strip that holds the month name and year (the default
     * is light yellow)
     *
     * @param label_background_color the color of the title bar or
     *   null to make it transparent
     */
    public Titled_date_selector( Date_selector selector Color label_background_color )
    {  this(selector);
      if( label_background_color == null )
        titlesetOpaque( false );
      else
        titlesetBackground( label_background_color );
    }
  
    private ActionListener my_subscribers = null;
    public synchronized void addActionListener(ActionListener l)
    {  my_subscribers = AWTEventMulticasteradd(my_subscribers l);
    }
    public synchronized void removeActionListener(ActionListener l)
    {  my_subscribers = AWTEventMulticasterremove(my_subscribers l);
    }
  
    public Date get_selected_date()   { return selectorget_selected_date(); }
    public Date get_current_date()   { return selectorget_current_date();  }
    public void roll(int f boolean up) {    selectorroll(fup);      }
    public int get(int f)       { return selectorget(f);        }
  }
  
  NAVIGATION/導航
  下面展示的就是導航欄的實現代碼雖然有點長但同樣非常地簡單代碼由定義了個圖象文件的代碼開始(我計劃以後放棄箭頭采用代碼實現但是現在仍然在用圖象文件)用下面的代碼把圖象作為資源獲取過來
  ClassLoader loader = getClass()getClassLoader();
  loadergetResource( IMAGE_FILE_NAME );
  classloader在找類的地方找圖象資源比如程序在文件系統中運行它將要在classpath中查找文件路徑因為沒有用到絕對路徑代碼是更加容易的打包成jar文件並且文件也不再需要建立在文件系統中導航欄是一個四個用圖象做標簽的按紐按紐的動作監聽通過Date_selector的roll()來包裝日歷對象並且月份的改變也激發標題欄的改變有一點非常重要就是導航條不知道也不影響標題標題包裝器是一個監聽所以它能自動的更新標題導航條根本就不知道標題包裝器的存在
  
  public class Navigable_date_selector extends JPanel implements Date_selector
  {  private    Date_selector selector;
  
    // Names of image files used for the navigator bar
    private static final String
      NEXT_YEAR    = images/pxredarrowrightdoublegif
      NEXT_MONTH   = images/pxredarrowrightgif
      PREVIOUS_YEAR  = images/pxredarrowleftdoublegif
      PREVIOUS_MONTH = images/pxredarrowleftgif;
  
    // These constants are used to identify the button and
    // as the button caption in the event that the appropriate
    // image file cant be located
  
    private static final String FORWARD_MONTH  = >  
                  FORWARD_YEAR  = >>  
                  BACK_MONTH   = <  
                  BACK_YEAR    = <<  
  
    private JPanel navigation = new JPanel();
  
    public Navigable_date_selector( Date_selector selector )
    {  thisselector = selector;
      setBorder( null );
      setOpaque( false );
      setLayout( new BorderLayout() );
      add( (JPanel)selector BorderLayoutCENTER );
  
      navigationsetLayout(new FlowLayout());
      navigationsetBorder( null );
      navigationsetBackground( comholubuiColorsLIGHT_YELLOW );
      navigationadd( make_navigation_button(BACK_YEAR  ) );
      navigationadd( make_navigation_button(BACK_MONTH  ) );
      navigationadd( make_navigation_button(FORWARD_MONTH) );
      navigationadd( make_navigation_button(FORWARD_YEAR ) );
  
      add(navigation BorderLayoutSOUTH);
    }
  
    //
    // I left out a few constructors and utility methods that go here
    //
  
    private final Navigation_handler navigation_listener
                      = new Navigation_handler();
  
    /** Handle clicks from the navigation bar buttons */
  
    private class Navigation_handler implements ActionListener
    {  public void actionPerformed(ActionEvent e)
      {  String direction = egetActionCommand();
  
        if   (direction==FORWARD_YEAR )selectorroll(CalendarYEARtrue);
        else if(direction==BACK_YEAR  )selectorroll(CalendarYEARfalse);
        else if(direction==FORWARD_MONTH)
        {
          selectorroll(CalendarMONTHtrue);
          if( selectorget(CalendarMONTH) == CalendarJANUARY )
            selectorroll(CalendarYEARtrue);
        }
        else if (direction==BACK_MONTH )
        {
          selectorroll(CalendarMONTHfalse);
          if( selectorget(CalendarMONTH) == CalendarDECEMBER )
            selectorroll(CalendarYEARfalse);
        }
        else
        {  assert false: Unexpected direction;
        }
      }
    }
  
    private JButton make_navigation_button(String caption)
    {
      ClassLoader loader = getClass()getClassLoader();
      URL image =
        (cap
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27437.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.