选择显示字体大小

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

基本的selector接口
  
  日期选择类的核心是date_selector_panel.仔细分析后,首先做了一些如下变量声明,不要被"常量初始化"语法掷出。(以为括号中的代码是静态的,和其他没有任何联系.)实例初始化声名使用this的结构避免了在构造器中连接另一个构造器。他们提供了保证在每个构造器都能有效调用的初始化方法。同时在一个地方给出所有的初始化代码也是非常方便的。散布在各个地方的没有初始化的变量很容易引起运行错误。当移动代码到构造器中也是非常困难的,因为移动过程中你必须考虑你已经改变的2个地方。比如说,在下面日历声明中代码就是实例初始化:
   public class date_selector_panel extends jpanel implements date_selector
{
//这些字符串应该应该在资源包中,因此它们是国际化的。    
        private string[] months =
    {   "jan","feb", "mar","apr", "may","june",
        "july","aug","sept","oct","nov","dec"
    };

    private static final int days_in_week = 7,  // 一个 星期的天数
   max_weeks    = 6;  // 在一月中最大的//星期数。
    // 用户选择的日期
private date selected = null;

    private calendar calendar = calendar.getinstance();
    {   calendar.set( calendar.hour,    0 );
        calendar.set( calendar.minute,  0 );
        calendar.set( calendar.second,  0 );
    }

    //显示在屏幕上的当前日期
    private final calendar today = calendar.getinstance();

下面便开始处理点击不同天的事件。我采用比较容易实现的外观即将每天都以一个标签是日期的按纽显示在屏幕上,我定义了一个简单的监听对象并加载到每个按纽中。由监听对象获取按纽标签值,由标签值可以得到日期与日历的位置,然后它将从日历中获取日期对象并且将日期字符串发送给监听器(通过调用fire_actionevent()的方式)。

//一个监听器适合所有calendar事件
private final button_handler day_listener  = new button_handler();

private class button_handler implements actionlistener
{   public void actionperformed(actionevent e)
    {
        if (e.getactioncommand().equals("d"))
        {   string text = ((jbutton) e.getsource()).gettext();

            if(text.length() > 0)  //  <=0 means click on blank square. ignore.
            {   calendar.set
                (   calendar.get(calendar.year),    // reset the calendar
                    calendar.get(calendar.month),   // to be the chosen
                    integer.parseint(text)          // date.
                );
                selected = calendar.gettime();
                fire_actionevent( select_action, selected.tostring() );
            }
        }
    }
}

     java的awteventmulticaster 类实现了动作监听。这种实现在我提供的文档(一篇关于multicasters的文章列在 resources <http://www.javaworld.com/javaworld/jw-07-2003/>.)中叙述的比较清楚。
private actionlistener subscribers = null;
public synchronized void addactionlistener(actionlistener l)
{   subscribers = awteventmulticaster.add(subscribers, l);
}
public synchronized void removeactionlistener(actionlistener l)
{   subscribers = awteventmulticaster.remove(subscribers, l);
}
private void fire_actionevent( int id, string command )
{   if (subscribers != null)
         subscribers.actionperformed(new actionevent(this, id, command) );
}


//在面板显示前调用addnotify()。它实际上创建了基本的图象对象,因此你可以调用super.addnotify()去调用面板。
//这里使用它是一个入口去发送初始化actionevent到支持标题的任何实体。
public void addnotify()
{
    super.addnotify();
    int month = calendar.get(calendar.month);
    int year  = calendar.get(calendar.year);
    fire_actionevent( change_action, months&#91;month&#93; + &quot; &quot; + year );
}

    然后我创建并初始化了代表天的按纽数组。有趣的是,日历并不能用二维数组表示,于是,我把按纽放在gridlayout布局中,让布局管理器来获取他的状态。在线形数组中在grid中的第一个按钮表示第一周的第一天;第八个按纽是第二周的第一天;等等。下面是代码 :

--&quot;);
        days&#91;i&#93; = day;
        day.setborder (borderfactory.createemptyborder(1,2,1,2));
         day.setfocuspainted (false);   // cannot get focus
        day.setactioncommand    (&quot;d&quot;);
        day.addactionlistener   (day_listener);// our single listener
        day.setopaque (false);                  
  // transparent background
    }
}

然后便是构造器。主要工作都在这没有参数的构造器中。它创建了包含日历的面板,建立了gridlayout布局,并将按纽加入到布局中:

public date_selector_panel()
{
    jpanel calendar_display = new jpanel();
    calendar_display.setopaque(false);
    calendar_display.setborder( borderfactory.createemptyborder(5,3,0,1) );
    calendar_display.setlayout(new gridlayout(max_weeks /*rows*/, days_in_week /*columns*/ ));

    for( int i = 0; i <  days.length; ++i )
        calendar_display.add(days[i]);

    setopaque( false );
    setlayout( new borderlayout() );
    add(calendar_display, borderlayout.center);
    update_calendar_display();
}

   全部代码清单还含有少量的的方便组件,使用他们比用当前日期能更好的初始化日历。在这里我没有谈到。
在date_selector_panel中最值得说的是update_calendar_display()方法,当改变日历的时候该方法更新显示的日历。我使用java.util.calendar()方法来判断星期日与月初的偏移量并在这些按钮上设置空字符串的标签。最后我将用空字符串来填写表示月末的按钮标签。
通过这种方式,你看到的每个按都在改变,即使它代表的是当前月中的无效天。这里并不需要用代码实现 ,因为当你 将按钮放入gridlayout中,布局便会自动列出你放入的按钮。
private void update_calendar_display()
{
    setvisible(false);  // improves paint speed and reduces flicker.

    clear_highlight();

    // the buttons that comprise the calendar are in a single
    // dimensioned array that was added to a 6x7 grid layout in
    // order. because of the linear structure, it's easy to
    // lay out the calendar just by changing the labels on
    // the buttons. here's the algorithm used below:
    //
    //  1) find out the offset to the first day of the month.
    //  2) clear everything up to that offset.
    //  3) add the days of the month.
    //  4) clear everything else.

    int month = calendar.get(calendar.month);
    int year  = calendar.get(calendar.year);

    fire_actionevent( change_action, months&#91;month&#93; + &quot; &quot; + year );

    calendar.set( year, month, 1 ); // first day of the current month.

    int first_day_offset = calendar.get(calendar.day_of_week);      /* 1 */

    assert calendar.sunday == 0;
    assert first_day_offset < days.length;

    int i = 0;
    while( i < first_day_offset-1 )                              /* 2 */
        days&#91;i++&#93;.settext(&quot;&quot;);

    int day_of_month = 1;
    for(; i < days.length; ++i )                                 /* 3 */
    {
        if( calendar.get(calendar.month)==today.get(calendar.month)
        &&  calendar.get(calendar.year )==today.get(calendar.year )
        &&  calendar.get(calendar.date )==today.get(calendar.date ) )
        {   highlight( days&#91;i&#93; );
        }

        days&#91;i&#93;.settext( string.valueof(day_of_month) );

        calendar.roll( calendar.date, /*up=*/ true );   // forward one day.

        day_of_month = calendar.get(calendar.date);
        if( day_of_month == 1 )
            break;
    }

    // note that we break out of the previous loop with i positioned
    // at the last day we added, thus the following ++ *must* be a
    // preincrement because we want to start clearing at the cell
    // after that.

    while( ++i < days.length )                                 /* 4 */
        days&#91;i&#93;.settext(&quot;&quot;);

    setvisible(true);
}

private jbutton highlighted = null;
private void clear_highlight()
{
    if( highlighted != null )
    {   highlighted.setbackground( color.white );
        highlighted.setforeground( color.black );
        highlighted.setopaque(false);
        highlighted = null;
    }
}

private void highlight( jbutton cell )
{
    highlighted = cell;
    cell.setbackground( com.holub.ui.colors.dark_red );
    cell.setforeground( color.white );
    cell.setopaque( true );
}

    类的其他部分定义了获取calendar值的几种方法。都是些非常简单的,在这里就没有列出来。

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