当前页面: 开发资料首页 → J2EE 专题 → 解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(三)
摘要: 解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(三)
//一个监听器适合所有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() );
}
}
}
}
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) );
}
//这里使用它是一个入口去发送初始化ActionEvent到支持标题的任何实体。
public void addNotify()
{
super.addNotify();
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
fire_ActionEvent( CHANGE_ACTION, months[month] + " " + year );
}
--");
days[i] = day;
day.setBorder (BorderFactory.createEmptyBorder(1,2,1,2));
day.setFocusPainted (false); // Cannot get focus
day.setActionCommand ("D");
day.addActionListener (day_listener);// Our single listener
day.setOpaque (false);
// Transparent background
}
}
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[month] + " " + 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[i++].setText("");
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[i] );
}
days[i].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[i].setText("");
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 );
}
ealy ,java 爱好者,Matrix jsp翻译小组成员,可以点击http://www.matrix.org.cn/user_view.asp?username=ealy查看她的个人信息Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd, scwcd 基本