📄 dateselectorpanel.java
字号:
public synchronized void addActionListener(ActionListener l)
{ subscribers = AWTEventMulticaster.add(subscribers, l);
}
/** Remove a listener.
* @see DateSelector
*/
public synchronized void removeActionListener(ActionListener l)
{ subscribers = AWTEventMulticaster.remove(subscribers, l);
}
/** Notify the listeners of a scroll or select
*/
private void fire_ActionEvent( int id, String command )
{ if (subscribers != null)
subscribers.actionPerformed(new ActionEvent(this, id, command) );
}
/***********************************************************************
* Handle clicks from the buttons that represent calendar days.
*/
private class ButtonHandler 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 choosen
Integer.parseInt(text) // date.
);
fire_ActionEvent( SELECT_ACTION,
calendar.getTime().toString() );
}
}
}
}
//----------------------------------------------------------------------
private JButton highlighted = null;
private void clearHighlight()
{
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 );
}
//----------------------------------------------------------------------
/** Redraw the buttons that comprise the calandar to display the current
* month
*/
private void updateCalendarDisplay()
{
setVisible(false); // improves paint speed & reduces flicker
clearHighlight();
// The buttons that comprise the calendar are in a single
// dimentioned 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 firstDayOffset = calendar.get(Calendar.DAY_OF_WEEK); /* 1 */
// assert firstDayOffset < days.length;
int i = 0;
while( i < firstDayOffset-1 ) /* 2 */
days[i++].setText("");
int dayOfMonth = 1;
for(; i < days.length; ++i ) /* 3 */
{
// Can't get calendar.equals(today) to work, so do it manually
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(dayOfMonth) );
calendar.roll( Calendar.DATE, /*up=*/ true ); // forward one day
dayOfMonth = calendar.get(Calendar.DATE);
if( dayOfMonth == 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 becasue we want to start clearing at the cell
// after that.
while( ++i < days.length ) /* 4 */
days[i].setText("");
setVisible(true);
}
/** Create a naviagion button with an image appropriate to the caption.
* The <code>caption</code> argument is used as the button's "action
* command." This method is public only because it has to be.
* (It overrides a public method.) Pretend it's not here.
*/
public void addNotify()
{
super.addNotify();
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
fire_ActionEvent( CHANGE_ACTION, months[month] + " " + year );
}
/** Returns the {@link Date Date} selected by the user or null if
* the window was closed without selecting a date. The returned
* Date has hours, minutes, and seconds values of 0. Modifying
* the returned Date has no effect on the displayed date.
*/
public Date getDateRepresentation()
{ return calendar.getTime();
}
/** Returns a Calendar that represents the currently selected
* date. This object is not hooked up to the widget in any
* way. Modifying the returned Calendar
* will not affect the displayed date, for example.
* This calender represents the most recently selected date.
*/
public Calendar getCalendarRepresentation()
{ Calendar clone = Calendar.getInstance();
clone.setTime( calendar.getTime() );
return clone;
}
/** Display the specified date.
* @param src display the date represented by this Calendar.
* Modifying the Calendar after this call will
* not affect the displayed date in any way.
*/
public void displayDate( Calendar src )
{ calendar.setTime( src.getTime() );
updateCalendarDisplay();
}
/** Display the specified date.
* @param src display the date represented by this Date object.
* Modifying the Calendar after this call will
* not affect the displayed date in any way.
*/
public void displayDate( Date src )
{ calendar.setTime( src );
updateCalendarDisplay();
}
/** Works just like {@link Calendar#roll(int,boolean)}. */
public void roll(int field, boolean up)
{ calendar.roll(field,up);
updateCalendarDisplay();
}
/** Works just like {@link Calendar#roll(int,int)}. */
public void roll(int field, int amount)
{ calendar.roll(field,amount);
updateCalendarDisplay();
}
/** Works just like {@link Calendar#set(int,int,int)}
* Sets "today" (which is higlighted) to the indicated day.
*/
public void set( int year, int month, int date )
{ calendar.set(year,month,date);
today.set(year,month,date);
updateCalendarDisplay();
}
/** Works just like {@link Calendar#get(int)} */
public int get( int field )
{ return calendar.get(field);
}
/** Works just like {@link Calendar#setTime(Date)},
* Sets "today" (which is higlighted) to the indicated day.
*/
public void setTime( Date d )
{ calendar.setTime(d);
today.setTime(d);
updateCalendarDisplay();
}
/** Works just like {@link Calendar#getTime} */
public Date getTime( )
{ return calendar.getTime();
}
/** Return a Calendar object that represents the currently-displayed
* month and year. Modifying this object will not affect the
* current panel.
* @return a Calendar representing the panel's state.
*/
public Calendar getCalendar()
{ Calendar c = Calendar.getInstance();
c.setTime( calendar.getTime() );
return c;
}
/** Change the display to match the indicated calendar. This Calendar
* argument is used only to provide the new date/time information.
* Modifying it after a call to the current method will not affect
* the DateSelectorPanel at all.
* Sets "today" (which is higlighted) to the indicated day.
* @param calendar A calendar positioned t the date to display.
*/
public void setFromCalendar(Calendar calendar)
{ this.calendar.setTime( calendar.getTime() );
today.setTime( calendar.getTime() );
updateCalendarDisplay();
}
//----------------------------------------------------------------------
private static class Test
{ public static void main( String[] args )
{ JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().setLayout( new FlowLayout() );
DateSelector left = new TitledDateSelector(new NavigableDateSelector());
DateSelector center = new NavigableDateSelector();
DateSelector right = new DateSelectorPanel(1900,1,2);
((NavigableDateSelector)center).changeNavigationBarColor(
Colors.TRANSPARENT );
ActionListener l =
new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ System.out.println( e.getActionCommand() );
}
};
left.addActionListener (l);
center.addActionListener(l);
right.addActionListener (l);
JPanel white = new JPanel(); // proove that it's transparent.
white.setBackground(Color.WHITE);
white.add( (JPanel)center );
// I hate these casts, but they're
// mandated by the fact that
// Component is not an interface.
//
frame.getContentPane().add( (JPanel)left );
frame.getContentPane().add( white );
frame.getContentPane().add( (JPanel)right );
frame.pack();
frame.show();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -