📄 jdaychooser.java
字号:
/**
* Sets a specific calendar. This is needed for correct graphical
* representation of the days.
*
* @param calendar
* the new calendar
*/
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
drawDays();
}
/**
* Sets the font property.
*
* @param font
* the new font
*/
public void setFont(Font font) {
if (days != null) {
for (int i = 0; i < 49; i++) {
days[i].setFont(font);
}
}
if (weeks != null) {
for (int i = 0; i < 7; i++) {
weeks[i].setFont(font);
}
}
}
/**
* Sets the foregroundColor color.
*
* @param foreground
* the new foregroundColor
*/
public void setForeground(Color foreground) {
super.setForeground(foreground);
if (days != null) {
for (int i = 7; i < 49; i++) {
days[i].setForeground(foreground);
}
drawDays();
}
}
/**
* JDayChooser is the ActionListener for all day buttons.
*
* @param e
* the ActionEvent
*/
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
int day = new Integer(buttonText).intValue();
setDay(day);
}
/**
* JDayChooser is the FocusListener for all day buttons. (Added by Thomas
* Schaefer)
*
* @param e
* the FocusEvent
*/
/*
* Code below commented out by Mark Brown on 24 Aug 2004. This code breaks
* the JDateChooser code by triggering the actionPerformed method on the
* next day button. This causes the date chosen to always be incremented by
* one day.
*/
public void focusGained(FocusEvent e) {
// JButton button = (JButton) e.getSource();
// String buttonText = button.getText();
//
// if ((buttonText != null) && !buttonText.equals("") &&
// !e.isTemporary()) {
// actionPerformed(new ActionEvent(e.getSource(), 0, null));
// }
}
/**
* Does nothing.
*
* @param e
* the FocusEvent
*/
public void focusLost(FocusEvent e) {
}
/**
* JDayChooser is the KeyListener for all day buttons. (Added by Thomas
* Schaefer and modified by Austin Moore)
*
* @param e
* the KeyEvent
*/
public void keyPressed(KeyEvent e) {
int offset = (e.getKeyCode() == KeyEvent.VK_UP) ? (-7)
: ((e.getKeyCode() == KeyEvent.VK_DOWN) ? (+7)
: ((e.getKeyCode() == KeyEvent.VK_LEFT) ? (-1)
: ((e.getKeyCode() == KeyEvent.VK_RIGHT) ? (+1) : 0)));
int newDay = getDay() + offset;
if ((newDay >= 1) && (newDay <= calendar.getMaximum(Calendar.DAY_OF_MONTH))) {
setDay(newDay);
}
}
/**
* Does nothing.
*
* @param e
* the KeyEvent
*/
public void keyTyped(KeyEvent e) {
}
/**
* Does nothing.
*
* @param e
* the KeyEvent
*/
public void keyReleased(KeyEvent e) {
}
/**
* Enable or disable the JDayChooser.
*
* @param enabled
* The new enabled value
*/
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
for (short i = 0; i < days.length; i++) {
if (days[i] != null) {
days[i].setEnabled(enabled);
}
}
for (short i = 0; i < weeks.length; i++) {
if (weeks[i] != null) {
weeks[i].setEnabled(enabled);
}
}
}
/**
* In some Countries it is often usefull to know in which week of the year a
* date is.
*
* @return boolean true, if the weeks of the year is shown
*/
public boolean isWeekOfYearVisible() {
return weekOfYearVisible;
}
/**
* In some Countries it is often usefull to know in which week of the year a
* date is.
*
* @param weekOfYearVisible
* true, if the weeks of the year shall be shown
*/
public void setWeekOfYearVisible(boolean weekOfYearVisible) {
if (weekOfYearVisible == this.weekOfYearVisible) {
return;
} else if (weekOfYearVisible) {
add(weekPanel, BorderLayout.WEST);
} else {
remove(weekPanel);
}
this.weekOfYearVisible = weekOfYearVisible;
validate();
dayPanel.validate();
}
/**
* Returns the day panel.
*
* @return the day panel
*/
public JPanel getDayPanel() {
return dayPanel;
}
/**
* Returns the color of the decoration (day names and weeks).
*
* @return the color of the decoration (day names and weeks).
*/
public Color getDecorationBackgroundColor() {
return decorationBackgroundColor;
}
/**
* Sets the background of days and weeks of year buttons.
*
* @param decorationBackgroundColor
* The background to set
*/
public void setDecorationBackgroundColor(Color decorationBackgroundColor) {
this.decorationBackgroundColor = decorationBackgroundColor;
if (days != null) {
for (int i = 0; i < 7; i++) {
days[i].setBackground(decorationBackgroundColor);
}
}
if (weeks != null) {
for (int i = 0; i < 7; i++) {
weeks[i].setBackground(decorationBackgroundColor);
}
}
}
/**
* Returns the Sunday foreground.
*
* @return Color the Sunday foreground.
*/
public Color getSundayForeground() {
return sundayForeground;
}
/**
* Returns the weekday foreground.
*
* @return Color the weekday foreground.
*/
public Color getWeekdayForeground() {
return weekdayForeground;
}
/**
* Sets the Sunday foreground.
*
* @param sundayForeground
* The sundayForeground to set
*/
public void setSundayForeground(Color sundayForeground) {
this.sundayForeground = sundayForeground;
drawDayNames();
drawDays();
}
/**
* Sets the weekday foreground.
*
* @param weekdayForeground
* The weekdayForeground to set
*/
public void setWeekdayForeground(Color weekdayForeground) {
this.weekdayForeground = weekdayForeground;
drawDayNames();
drawDays();
}
/**
* Requests that the selected day also have the focus.
*/
public void setFocus() {
if (selectedDay != null) {
this.selectedDay.requestFocus();
}
}
/**
* The decoration background is the background color of the day titles and
* the weeks of the year.
*
* @return Returns true, if the decoration background is painted.
*/
public boolean isDecorationBackgroundVisible() {
return decorationBackgroundVisible;
}
/**
* The decoration background is the background color of the day titles and
* the weeks of the year.
*
* @param decorationBackgroundVisible
* true, if the decoration background shall be painted.
*/
public void setDecorationBackgroundVisible(boolean decorationBackgroundVisible) {
this.decorationBackgroundVisible = decorationBackgroundVisible;
initDecorations();
}
/**
* The decoration border is the button border of the day titles and the
* weeks of the year.
*
* @return Returns true, if the decoration border is painted.
*/
public boolean isDecorationBordersVisible() {
return decorationBordersVisible;
}
public boolean isDayBordersVisible() {
return dayBordersVisible;
}
/**
* The decoration border is the button border of the day titles and the
* weeks of the year.
*
* @param decorationBordersVisible
* true, if the decoration border shall be painted.
*/
public void setDecorationBordersVisible(boolean decorationBordersVisible) {
this.decorationBordersVisible = decorationBordersVisible;
initDecorations();
}
public void setDayBordersVisible(boolean dayBordersVisible) {
this.dayBordersVisible = dayBordersVisible;
if (initialized) {
for (int x = 7; x < 49; x++) {
if ("Windows".equals(UIManager.getLookAndFeel().getID())) {
days[x].setContentAreaFilled(dayBordersVisible);
} else {
days[x].setContentAreaFilled(true);
}
days[x].setBorderPainted(dayBordersVisible);
}
}
}
/**
* Updates the UI and sets the day button preferences.
*/
public void updateUI() {
super.updateUI();
setFont(Font.decode("Dialog Plain 11"));
if (weekPanel != null) {
weekPanel.updateUI();
}
if (initialized) {
if ("Windows".equals(UIManager.getLookAndFeel().getID())) {
setDayBordersVisible(false);
setDecorationBackgroundVisible(true);
setDecorationBordersVisible(false);
} else {
setDayBordersVisible(true);
setDecorationBackgroundVisible(decorationBackgroundVisible);
setDecorationBordersVisible(decorationBordersVisible);
}
}
}
/**
* Sets a valid date range for selectable dates. If max is before min, the
* default range with no limitation is set.
*
* @param min
* the minimum selectable date or null (then the minimum date is
* set to 01\01\0001)
* @param max
* the maximum selectable date or null (then the maximum date is
* set to 01\01\9999)
*/
public void setSelectableDateRange(Date min, Date max) {
if (min == null) {
minSelectableDate = defaultMinSelectableDate;
} else {
minSelectableDate = min;
}
if (max == null) {
maxSelectableDate = defaultMaxSelectableDate;
} else {
maxSelectableDate = max;
}
if (maxSelectableDate.before(minSelectableDate)) {
minSelectableDate = defaultMinSelectableDate;
maxSelectableDate = defaultMaxSelectableDate;
}
drawDays();
}
/**
* Sets the maximum selectable date. If null, the date 01\01\9999 will be set instead.
*
* @param max the maximum selectable date
*
* @return the maximum selectable date
*/
public Date setMaxSelectableDate(Date max) {
if (max == null) {
maxSelectableDate = defaultMaxSelectableDate;
} else {
maxSelectableDate = max;
}
drawDays();
return maxSelectableDate;
}
/**
* Sets the minimum selectable date. If null, the date 01\01\0001 will be set instead.
*
* @param min the minimum selectable date
*
* @return the minimum selectable date
*/
public Date setMinSelectableDate(Date min) {
if (min == null) {
minSelectableDate = defaultMinSelectableDate;
} else {
minSelectableDate = min;
}
drawDays();
return minSelectableDate;
}
/**
* Gets the maximum selectable date.
*
* @return the maximum selectable date
*/
public Date getMaxSelectableDate() {
return maxSelectableDate;
}
/**
* Gets the minimum selectable date.
*
* @return the minimum selectable date
*/
public Date getMinSelectableDate() {
return minSelectableDate;
}
/**
* Gets the maximum number of characters of a day name or 0. If 0 is
* returned, dateFormatSymbols.getShortWeekdays() will be used.
*
* @return the maximum number of characters of a day name or 0.
*/
public int getMaxDayCharacters() {
return maxDayCharacters;
}
/**
* Sets the maximum number of characters per day in the day bar. Valid
* values are 0-4. If set to 0, dateFormatSymbols.getShortWeekdays() will be
* used, otherwise theses strings will be reduced to the maximum number of
* characters.
*
* @param maxDayCharacters
* the maximum number of characters of a day name.
*/
public void setMaxDayCharacters(int maxDayCharacters) {
if (maxDayCharacters == this.maxDayCharacters) {
return;
}
if (maxDayCharacters < 0 || maxDayCharacters > 4) {
this.maxDayCharacters = 0;
} else {
this.maxDayCharacters = maxDayCharacters;
}
drawDayNames();
drawDays();
invalidate();
}
/**
* Creates a JFrame with a JDayChooser inside and can be used for testing.
*
* @param s
* The command line arguments
*/
public static void main(String[] s) {
JFrame frame = new JFrame("JDayChooser");
frame.getContentPane().add(new JDayChooser());
frame.pack();
frame.setVisible(true);
}
class DecoratorButton extends JButton {
private static final long serialVersionUID = -5306477668406547496L;
public DecoratorButton() {
setBackground(decorationBackgroundColor);
setContentAreaFilled(decorationBackgroundVisible);
setBorderPainted(decorationBordersVisible);
}
public void addMouseListener(MouseListener l) {
}
public boolean isFocusable() {
return false;
}
public void paint(Graphics g) {
if ("Windows".equals(UIManager.getLookAndFeel().getID())) {
// this is a hack to get the background painted
// when using Windows Look & Feel
if (decorationBackgroundVisible) {
g.setColor(decorationBackgroundColor);
} else {
g.setColor(days[7].getBackground());
}
g.fillRect(0, 0, getWidth(), getHeight());
if (isBorderPainted()) {
setContentAreaFilled(true);
} else {
setContentAreaFilled(false);
}
}
super.paint(g);
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -