选择显示字体大小

解决日期选择问题,一劳永逸(使用decorator模式实现日期选择组件)(五)

弹出对话框

   对话框包装器有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查看她的个人信息 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons