📄 datechooserpanel.java
字号:
}
this.buttons = new JButton[42];
for (int i = 0; i < 42; i++) {
final JButton b = new JButton("");
b.setMargin(new Insets(1, 1, 1, 1));
b.setName(Integer.toString(i));
b.setFont(this.dateFont);
b.setFocusPainted(false);
b.setActionCommand("dateButtonClicked");
b.addActionListener(this);
this.buttons[i] = b;
p.add(b);
}
return p;
}
/**
* Returns the button color according to the specified date.
*
* @param theDate the date.
* @return the color.
*/
private Color getButtonColor(final Calendar theDate) {
if (equalDates(theDate, this.chosenDate)) {
return this.chosenDateButtonColor;
}
else if (theDate.get(Calendar.MONTH) == this.chosenDate.get(
Calendar.MONTH)) {
return this.chosenMonthButtonColor;
}
else {
return this.chosenOtherButtonColor;
}
}
/**
* Returns true if the two dates are equal (time of day is ignored).
*
* @param c1 the first date.
* @param c2 the second date.
* @return boolean.
*/
private boolean equalDates(final Calendar c1, final Calendar c2) {
if ((c1.get(Calendar.DATE) == c2.get(Calendar.DATE))
&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
&& (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))) {
return true;
}
else {
return false;
}
}
/**
* Returns the first date that is visible in the grid. This should always
* be in the month preceding the month of the selected date.
*
* @return the date.
*/
private Calendar getFirstVisibleDate() {
final Calendar c = Calendar.getInstance();
c.set(this.chosenDate.get(Calendar.YEAR), this.chosenDate.get(
Calendar.MONTH), 1);
c.add(Calendar.DATE, -1);
while (c.get(Calendar.DAY_OF_WEEK) != getFirstDayOfWeek()) {
c.add(Calendar.DATE, -1);
}
return c;
}
/**
* Returns the first day of the week (controls the labels in the date
* panel).
*
* @return the first day of the week.
*/
private int getFirstDayOfWeek() {
return this.firstDayOfWeek;
}
/**
* Update the button labels and colors to reflect date selection.
*/
private void refreshButtons() {
final Calendar c = getFirstVisibleDate();
for (int i = 0; i < 42; i++) {
final JButton b = this.buttons[i];
b.setText(Integer.toString(c.get(Calendar.DATE)));
b.setBackground(getButtonColor(c));
c.add(Calendar.DATE, 1);
}
}
/**
* Changes the contents of the year selection JComboBox to reflect the
* chosen date and the year range.
*/
private void refreshYearSelector() {
if (!this.refreshing) {
this.refreshing = true;
this.yearSelector.removeAllItems();
final Integer[] years = getYears(this.chosenDate.get(
Calendar.YEAR));
for (int i = 0; i < years.length; i++) {
this.yearSelector.addItem(years[i]);
}
this.yearSelector.setSelectedItem(new Integer(this.chosenDate.get(
Calendar.YEAR)));
this.refreshing = false;
}
}
/**
* Returns a vector of years preceding and following the specified year.
* The number of years preceding and following is determined by the
* yearSelectionRange attribute.
*
* @param chosenYear the selected year.
* @return a vector of years.
*/
private Integer[] getYears(final int chosenYear) {
final int size = this.yearSelectionRange * 2 + 1;
final int start = chosenYear - this.yearSelectionRange;
final Integer[] years = new Integer[size];
for (int i = 0; i < size; i++) {
years[i] = new Integer(i + start);
}
return years;
}
/**
* Constructs a panel containing two JComboBoxes (for the month and year)
* and a button (to reset the date to TODAY).
*
* @return the panel.
*/
private JPanel constructSelectionPanel() {
final JPanel p = new JPanel();
final int minMonth = this.chosenDate.getMinimum(Calendar.MONTH);
final int maxMonth = this.chosenDate.getMaximum(Calendar.MONTH);
final String[] months = new String[maxMonth - minMonth + 1];
System.arraycopy(SerialDate.getMonths(), minMonth, months, 0,
months.length);
this.monthSelector = new JComboBox(months);
this.monthSelector.addActionListener(this);
this.monthSelector.setActionCommand("monthSelectionChanged");
p.add(this.monthSelector);
this.yearSelector = new JComboBox(getYears(0));
this.yearSelector.addActionListener(this);
this.yearSelector.setActionCommand("yearSelectionChanged");
p.add(this.yearSelector);
return p;
}
/**
* Returns a panel that appears at the bottom of the calendar panel -
* contains a button for selecting today's date.
*
* @return the panel.
*/
private JPanel constructControlPanel() {
final JPanel p = new JPanel();
p.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
this.todayButton = new JButton("Today");
this.todayButton.addActionListener(this);
this.todayButton.setActionCommand("todayButtonClicked");
p.add(this.todayButton);
return p;
}
/**
* Returns the color for the currently selected date.
*
* @return a color.
*/
public Color getChosenDateButtonColor() {
return this.chosenDateButtonColor;
}
/**
* Redefines the color for the currently selected date.
*
* @param chosenDateButtonColor the new color
*/
public void setChosenDateButtonColor(final Color chosenDateButtonColor) {
if (chosenDateButtonColor == null) {
throw new NullPointerException("UIColor must not be null.");
}
final Color oldValue = this.chosenDateButtonColor;
this.chosenDateButtonColor = chosenDateButtonColor;
refreshButtons();
firePropertyChange("chosenDateButtonColor", oldValue,
chosenDateButtonColor);
}
/**
* Returns the color for the buttons representing the current month.
*
* @return the color for the current month.
*/
public Color getChosenMonthButtonColor() {
return this.chosenMonthButtonColor;
}
/**
* Defines the color for the buttons representing the current month.
*
* @param chosenMonthButtonColor the color for the current month.
*/
public void setChosenMonthButtonColor(final Color chosenMonthButtonColor) {
if (chosenMonthButtonColor == null) {
throw new NullPointerException("UIColor must not be null.");
}
final Color oldValue = this.chosenMonthButtonColor;
this.chosenMonthButtonColor = chosenMonthButtonColor;
refreshButtons();
firePropertyChange("chosenMonthButtonColor", oldValue,
chosenMonthButtonColor);
}
/**
* Returns the color for the buttons representing the other months.
*
* @return a color.
*/
public Color getChosenOtherButtonColor() {
return this.chosenOtherButtonColor;
}
/**
* Redefines the color for the buttons representing the other months.
*
* @param chosenOtherButtonColor a color.
*/
public void setChosenOtherButtonColor(final Color chosenOtherButtonColor) {
if (chosenOtherButtonColor == null) {
throw new NullPointerException("UIColor must not be null.");
}
final Color oldValue = this.chosenOtherButtonColor;
this.chosenOtherButtonColor = chosenOtherButtonColor;
refreshButtons();
firePropertyChange("chosenOtherButtonColor", oldValue,
chosenOtherButtonColor);
}
/**
* Returns the range of years available for selection (defaults to 20).
*
* @return The range.
*/
public int getYearSelectionRange() {
return this.yearSelectionRange;
}
/**
* Sets the range of years available for selection.
*
* @param yearSelectionRange the range.
*/
public void setYearSelectionRange(final int yearSelectionRange) {
final int oldYearSelectionRange = this.yearSelectionRange;
this.yearSelectionRange = yearSelectionRange;
refreshYearSelector();
firePropertyChange("yearSelectionRange", oldYearSelectionRange,
yearSelectionRange);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -