国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

解決日期選擇問題,一勞永逸(使用Decorator模式實現日期選擇組件)(四)

2019-11-18 16:25:30
字體:
來源:轉載
供稿:網友
標題

   這Date_selector_panel是重要部分?,F在我們來看看他的裝飾。Titled_date_selector類只做一件事情:給未裝飾的日歷增加個標題。這是對實現了Date_selector的JPanel面板的包裝。它只顯示現有的Date_selector和顯示日期的標簽。下面的實現是微不足道。和用戶界面無相關值得說的只有那其中10行動作監聽代碼。監聽器獲取日期改變通知(通過用戶導航或者方法調用)便做相應的標簽日期更新。其他代碼只是簡單地創建面板和將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 )
    {   this.selector = selector;

        title.setHorizontalAlignment(SwingConstants.CENTER);
        title.setOpaque     ( true                                      );
        title.setBackground ( com.holub.ui.Colors.LIGHT_YELLOW          );
        title.setFont       ( title.getFont().deriveFont( Font.BOLD )   );

        selector.addActionListener
        (   new ActionListener()
            {   public void actionPerformed( ActionEvent e )
                {   if( e.getID() == Date_selector_panel.CHANGE_ACTION )
                        title.setText( e.getActionCommand() );
                    else
                        my_subscribers.actionPerformed(e);
                }
            }
        );

        setOpaque(false);
        setLayout( new BorderLayout() );
        add( title,  BorderLayout.NORTH );
        add( (JPanel)selector, BorderLayout.CENTER );
    }

    /** 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 )
            title.setOpaque( false );
        else
            title.setBackground( label_background_color );
    }

    private ActionListener my_subscribers = null;
    public synchronized void addActionListener(ActionListener l)
    {   my_subscribers = AWTEventMulticaster.add(my_subscribers, l);
    }
    public synchronized void removeActionListener(ActionListener l)
    {   my_subscribers = AWTEventMulticaster.remove(my_subscribers, l);
    }

    public Date get_selected_date()     { return selector.get_selected_date();  }
    public Date get_current_date()      { return selector.get_current_date();   }
    public void roll(int f, boolean up) {        selector.roll(f,up);           }
    public int  get(int f)              { return selector.get(f);               }
}


navigation/導航
  下面展示的就是導航欄的實現代碼,雖然有點長,但同樣非常地簡單。代碼由定義了4個圖象文件的代碼開始。(我計劃以后放棄箭頭采用代碼實現,但是現在仍然在用圖象文件。)用下面的代碼,把圖象作為資源獲取過來。
ClassLoader loader = getClass().getClassLoader();
loader.getResource( "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/10px.red.arrow.right.double.gif",
        NEXT_MONTH      = "images/10px.red.arrow.right.gif",
        PREVIOUS_YEAR   = "images/10px.red.arrow.left.double.gif",
        PREVIOUS_MONTH  = "images/10px.red.arrow.left.gif";

    // These constants are used to identify the button and
    // as the button caption in the event that the appropriate
    // image file can't 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 )
    {   this.selector = selector;
        setBorder( null  );
        setOpaque( false );
        setLayout( new BorderLayout() );
        add( (JPanel)selector, BorderLayout.CENTER );

        navigation.setLayout(new FlowLayout());
        navigation.setBorder( null );
        navigation.setBackground( com.holub.ui.Colors.LIGHT_YELLOW );
        navigation.add( make_navigation_button(BACK_YEAR    ) );
        navigation.add( make_navigation_button(BACK_MONTH   ) );
        navigation.add( make_navigation_button(FORWARD_MONTH) );
        navigation.add( make_navigation_button(FORWARD_YEAR ) );

        add(navigation, BorderLayout.SOUTH);
    }

    // ...
    // 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 = e.getActionCommand();

            if     (direction==FORWARD_YEAR )selector.roll(Calendar.YEAR,true);
            else if(direction==BACK_YEAR    )selector.roll(Calendar.YEAR,false);
            else if(direction==FORWARD_MONTH)
            {
                selector.roll(Calendar.MONTH,true);
                if( selector.get(Calendar.MONTH) == Calendar.JANUARY )
                    selector.roll(Calendar.YEAR,true);
            }
            else if (direction==BACK_MONTH  )
            {
                selector.roll(Calendar.MONTH,false);
                if( selector.get(Calendar.MONTH) == Calendar.DECEMBER )
                    selector.roll(Calendar.YEAR,false);
            }
            else
            {   assert false:  "UneXPected direction";
            }
        }
    }

    private JButton make_navigation_button(String caption)
    {
        ClassLoader loader = getClass().getClassLoader();
        URL image =
            (caption==FORWARD_YEAR  )? loader.getResource(NEXT_YEAR):
            (caption==BACK_YEAR     )? loader.getResource(PREVIOUS_YEAR):
            (caption==FORWARD_MONTH )? loader.getResource(NEXT_MONTH):
                                       loader.getResource(PREVIOUS_MONTH) ;

        JButton b = (image!=null) ? new JButton( new ImageIcon(image) )
                                  : new JButton(caption)
                                  ;
        b.setBorder(new EmptyBorder(0,4,0,4));
        b.setFocusPainted(false);
        b.setActionCommand(caption);
        b.addActionListener( navigation_listener );
        b.setOpaque( false );
        return b;
    }

    public synchronized void addActionListener(ActionListener l)
    {   selector.addActionListener(l);
    }
    public synchronized void removeActionListener(ActionListener l)
    {   selector.removeActionListener(l);
    }
    public Date get_selected_date()     { return selector.get_selected_date();  }
    public Date get_current_date()      { return selector.get_current_date();   }
    public void roll(int f, boolean up) {        selector.roll(f,up);           }
    public int  get(int f)              { return selector.get(f);               }
}


ealy ,java 愛好者,Matrix jsp翻譯小組成員,可以點擊http://www.matrix.org.cn/user_view.asp?username=ealy查看她的個人信息 
進入討論組討論。

(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凌源市| 蛟河市| 班玛县| 通许县| 滨州市| 河南省| 防城港市| 云南省| 孙吴县| 南平市| 呼图壁县| 南召县| 营山县| 出国| 漠河县| 靖西县| 龙泉市| 巢湖市| 金坛市| 和田市| 攀枝花市| 馆陶县| 宣武区| 汤阴县| 深泽县| 吴忠市| 丽水市| 广元市| 贡山| 安康市| 鄂托克前旗| 福建省| 静安区| 新沂市| 密山市| 三穗县| 黎平县| 嘉兴市| 大方县| 土默特右旗| 高陵县|