当前页面: 开发资料首页 → J2EE 专题 → 解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(四)
摘要: 解决日期选择问题,一劳永逸(使用Decorator模式实现日期选择组件)(四)
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); }
}
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查看她的个人信息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 标题