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

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

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

2019-11-18 16:25:30
字體:
來源:轉載
供稿:網友
彈出對話框

   對話框包裝器有2個類組成:第一個是Popup_dialog,繼承與JDialog實現小的如前所示的框架。
生成常規對話框的主要難點是所有的裝飾(框架與標題欄)的消失問題,因此如果你需要生成自己的標題欄,下面提供了具體實現的大部分代碼:
標題欄是包含一個顯示標題的標簽和一個按鈕(關閉按鈕,如導航箭頭一樣加載圖象資源)。
代碼如下:
public class Popup_dialog extends JDialog
{
    PRivate Color TITLE_BAR_COLOR = com.holub.ui.Colors.LIGHT_YELLOW;
    private Color CLOSE_BOX_COLOR = com.holub.ui.Colors.DARK_RED;

    private JLabel title = new JLabel("xxxxxxxxxxxxxx");
    {   title.setHorizontalAlignment(SwingConstants.CENTER);
        title.setOpaque( false );
        title.setFont( title.getFont().deriveFont(Font.BOLD) );
    }

    private JPanel header = new JPanel();
    {   header.setBackground( TITLE_BAR_COLOR );
        header.setLayout( new BorderLayout() );
        header.setBorder( BorderFactory.createEmptyBorder(2,2,2,2) );
        header.add( title                   , BorderLayout.CENTER );
        header.add( create_close_button()   , BorderLayout.EAST   );
    }

    private JPanel content_pane = new JPanel();
    {   content_pane.setLayout( new BorderLayout() );
    }

    public Popup_dialog( Frame owner ){ super(owner); setModal(true); }
    public Popup_dialog( Dialog owner){ super(owner); setModal(true); }

    /* Code common to all constrUCtors. */
    {
        init_dragable();

        setUndecorated( true );
        JPanel contents = new JPanel();
        contents.setBorder( BorderFactory.createLineBorder(Color.BLACK,1) );
        contents.setLayout(new BorderLayout());
        contents.add(header,       BorderLayout.NORTH);
        contents.add(content_pane, BorderLayout.CENTER);
        contents.setBackground( Color.WHITE );

        setContentPane( contents ); // , BorderLayout.CENTER );
        setLocation(100,100);
    }

    private JButton create_close_button()
    {
        URL image = getClass().getClassLoader().getResource(
                                                "images/8px.red.X.gif");

        JButton b = (image!=null) ? new JButton( new ImageIcon(image) )
                                  : new JButton( "  X  " )
                                  ;

        Border outer = BorderFactory.createLineBorder(CLOSE_BOX_COLOR,1);
        Border inner = BorderFactory.createEmptyBorder(2,2,2,2);

        b.setBorder( BorderFactory.createCompoundBorder(outer,inner) );

        b.setOpaque( false );
        b.addActionListener
        (   new ActionListener()
            {   public void actionPerformed(ActionEvent e)
                {   Popup_dialog.this.setVisible(false);
                    Popup_dialog.this.dispose();
                }
            }
        );

        b.setFocusable( false );
        return b;
    }

    /** Set the dialog title to the indicated text. */
    public void setTitle( String text ){ title.setText( text ); }

    /** Add your widgets to the window returned by this method, in
     *  a manner similar to a JFrame. Do not modify the Popup_dialog
     *  itself. The returned container is a {@link JPanel JPanel}
     *  with a preinstalled {@link BorderLayout}.
     *  By default, it's colored dialog-box gray.
     *  @return the content pane.
     */

    public Container getContentPane(){ return content_pane; }
有點意思實現拖拉功能的代碼。主要的思路是設置2個監聽器。撲捉按紐的鼠標監聽器與使用變量reference_position存儲鼠標點擊在標題標簽中的上層窗口位置。
  當鼠標移動時,鼠標移動監聽器將被激活。問題是和拖動聯系一起的事件一般和屏幕聯系在一起而不是和上層窗口聯系一起。拉句柄在當前鼠標位置移動對話框到原始的reference_position位置。就象從原始位置拖動窗體到現在鼠標位置一樣。
下面是代碼:
private Point reference_position = new Point(0,0);
private MouseMotionListener movement_handler;
private MouseListener click_handler;

private void init_dragable()
{
    movement_handler =
        new MouseMotionAdapter()
        {   public void mouseDragged( MouseEvent e )
            {   // The reference position is the (window-relative)
                // cursor position when the click occurred. The
                // current_mouse_position is mouse position
                // now, and the deltas represent the distance
                // moved.

                Point current_mouse_position  = e.getPoint();
                Point current_window_location = getLocation();

                int delta_x=current_mouse_position.x - reference_position.x;
                int delta_y=current_mouse_position.y - reference_position.y;

                // Move the window over by the computed delta. This move
                // effectively shifts the window-relative, current mouse
                // position back to the original reference position.

                current_window_location.translate(delta_x, delta_y);
                setLocation(current_window_location);
            }
        };

    click_handler =
        new MouseAdapter()
        {   public void mousePressed( MouseEvent e )
            {   reference_position = e.getPoint();  // Start of the drag
            }
        };

    setDragable(true);
}

/** Turn dragability on or off.
*/
public void setDragable( boolean on )
{   if( on )
    {   title.addMouseMotionListener ( movement_handler );
        title.addMouseListener       ( click_handler    );
    }
    else
    {   title.removeMouseMotionListener ( movement_handler );
        title.removeMouseListener       ( click_handler );
    }
}


日期對話框

   剩下的都是些微不足道的包裝。Date_selector_dialog 修飾者把包裝的Date_selector放在彈出框中。它從Titled_date_selector中復制了一些代碼用來顯示對話框標題欄(當前日期和月份)這里根本就沒有考慮導航條問題。因為Date_selector已經包含了導航條等等。。
public class Date_selector_dialog extends Popup_dialog implements Date_selector
{
    private Date_selector selector = new Date_selector_panel();

    /** Creates a dialog box with the indicated parent that holds
     *  a standard {@link Date_selector_panel Date_selector_panel}
     *  (as created using the no-arg constructor).
     */
    public Date_selector_dialog( Frame parent )
    {   super(parent);
        selector = new Navigable_date_selector( new Date_selector_panel() );
        init();
    }

    /* Like {@link #Date_selector_dialog(Frame),
     * but for a {@link Dialog} parent.
     */
    public Date_selector_dialog( Dialog parent )
    {   super(parent);
        selector = new Navigable_date_selector( new Date_selector_panel() );
        init();
    }

    /** Creates a dialog box with the indicated parent that holds
     *  the indicated Date_selector.
     *  Note that the current month and year display in the
     *  dialog-box title bar, so there's no need to display them in
     *  the selector too.
     */
    public Date_selector_dialog( Frame parent, Date_selector to_wrap )
    {   super(parent);
        selector = to_wrap;
        init();
    }

    /* Like {@link #Date_selector_dialog(Frame,Date_selector),
     * but for a {@link Dialog} parent.
     */

    public Date_selector_dialog( Dialog parent, Date_selector to_wrap )
    {   super(parent);
        selector = to_wrap;
        init();
    }

    /** Code common to all constructors.
     */
    private void init()
    {   getContentPane().add( (Container)selector, BorderLayout.CENTER );
        selector.addActionListener
        (   new ActionListener()
            {   public void actionPerformed( ActionEvent event )
                {   if( event.getID() == Date_selector.CHANGE_ACTION )
                    {   setTitle( event.getActionCommand() );
                    }
                    else
                    {   setVisible(false);
                        dispose();
                    }
                }
            }
        );
        ((Container)selector).setVisible(true);
        pack();
    }

    /** For use when you pop up a dialog using
     * setVisible(true) rather than {@link #select}.
     * @return the selected date or null if the dialog was closed
     *          without selecting anything.
     */
    public Date get_selected_date()
    {   return selector.get_selected_date();
    }

    /** Get the current date. The dialog stays in existence
     *  until the user closes it or selects a date, so this
     *  method can be used to see what month the user has
     *  scrolled to.
     *  @return the date currently displayed on the calendar.
     */
    public Date get_current_date()
    {   return selector.get_current_date();
    }

    /** Add an action listener for both.
     *  {@link Date_selector#CHANGE_ACTION} and
     *  {@link Date_selector#SELECT_ACTION} action events.
     */
    public void addActionListener(ActionListener l)
    {   selector.addActionListener(l);
    }

    /** Remove a previously added listener. */
    public void removeActionListener(ActionListener l)
    {   selector.removeActionListener(l);
    }

    /** Pops up the chooser and blocks until the user selects
     *  a date.
     * @return the selected date or null if the dialog was closed
     *          without selecting anything.
     */
    public Date select()
    {
        setVisible(true);
        return selector.get_selected_date();
    }

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


decorator 模式:
總的來說,盡管日歷選擇組件比實用,但是這篇文章的重點還是使用細化實現的Decorator 模式的使用。Decorator 模式適合代碼獨立化,易于管理,實現和調試。每個Decorator 在邏輯上都很重要且規范化。你要改變某物,只要知道要改變的地方;你要添加一個裝飾,只要繼承一個的簡單接口。
這種細化也減少了類中耦合度(不受歡迎的相互之間的依賴性)。在單一接口中改變或增加某一特性時使用Decorator 模式更能顯示它的靈活性。so ,使用Decorator 模式吧

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

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



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 老河口市| 雷州市| 上思县| 亚东县| 逊克县| 龙泉市| 九龙城区| 长治市| 武汉市| 罗江县| 乌拉特中旗| 阜宁县| 新丰县| 攀枝花市| 讷河市| 灵台县| 汕尾市| 习水县| 永新县| 会宁县| 长子县| 井陉县| 宜兰市| 昔阳县| 闽清县| 蒙城县| 常山县| 上犹县| 化州市| 武乡县| 桐梓县| 五常市| 疏附县| 南城县| 陆良县| 东丽区| 长岭县| 嵊泗县| 台江县| 五指山市| 资阳市|