datechooserpanel.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 511 行 · 第 1/2 页
JAVA
511 行
/* ======================================================================== * JCommon : a free general purpose class library for the Java(tm) platform * ======================================================================== * * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jcommon/index.html * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * --------------------- * DateChooserPanel.java * --------------------- * (C) Copyright 2000-2004, by Object Refinery Limited. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: DateChooserPanel.java,v 1.6 2005/06/01 14:12:29 taqua Exp $ * * Changes (from 26-Oct-2001) * -------------------------- * 26-Oct-2001 : Changed package to com.jrefinery.ui.* (DG); * 08-Dec-2001 : Dropped the getMonths() method (DG); * 13-Oct-2002 : Fixed errors reported by Checkstyle (DG); * */package org.jfree.ui;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Font;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DateFormatSymbols;import java.util.Calendar;import java.util.Date;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingConstants;import javax.swing.UIManager;import org.jfree.date.SerialDate;/** * A panel that allows the user to select a date. * * @author David Gilbert */public class DateChooserPanel extends JPanel implements ActionListener { /** * The date selected in the panel. */ private Calendar chosenDate; /** * The color for the selected date. */ private Color chosenDateButtonColor; /** * The color for dates in the current month. */ private Color chosenMonthButtonColor; /** * The color for dates that are visible, but not in the current month. */ private Color chosenOtherButtonColor; /** * The first day-of-the-week. */ private int firstDayOfWeek; /** * The range used for selecting years. */ private int yearSelectionRange = 20; /** * The font used to display the date. */ private Font dateFont = new Font("SansSerif", Font.PLAIN, 10); /** * A combo for selecting the month. */ private JComboBox monthSelector; /** * A combo for selecting the year. */ private JComboBox yearSelector; /** * A button for selecting today's date. */ private JButton todayButton; /** * An array of buttons used to display the days-of-the-month. */ private JButton[] buttons; /** * A flag that indicates whether or not we are currently refreshing the buttons. */ private boolean refreshing = false; /** * The ordered set of all seven days of a week, * beginning with the 'firstDayOfWeek'. */ private int[] WEEK_DAYS; /** * Constructs a new date chooser panel, using today's date as the initial selection. */ public DateChooserPanel() { this(Calendar.getInstance(), false); } /** * Constructs a new date chooser panel. * * @param calendar the calendar controlling the date. * @param controlPanel a flag that indicates whether or not the 'today' button should * appear on the panel. */ public DateChooserPanel(final Calendar calendar, final boolean controlPanel) { super(new BorderLayout()); this.chosenDateButtonColor = UIManager.getColor("textHighlight"); this.chosenMonthButtonColor = UIManager.getColor("control"); this.chosenOtherButtonColor = UIManager.getColor("controlShadow"); // the default date is today... this.chosenDate = calendar; this.firstDayOfWeek = calendar.getFirstDayOfWeek(); this.WEEK_DAYS = new int[7]; for (int i = 0; i < 7; i++) { this.WEEK_DAYS[i] = ((this.firstDayOfWeek + i - 1) % 7) + 1; } add(constructSelectionPanel(), BorderLayout.NORTH); add(getCalendarPanel(), BorderLayout.CENTER); if (controlPanel) { add(constructControlPanel(), BorderLayout.SOUTH); } setDate(calendar.getTime()); } /** * Sets the date chosen in the panel. * * @param theDate the new date. */ public void setDate(final Date theDate) { this.chosenDate.setTime(theDate); this.monthSelector.setSelectedIndex(this.chosenDate.get(Calendar.MONTH)); refreshYearSelector(); refreshButtons(); } /** * Returns the date selected in the panel. * * @return the selected date. */ public Date getDate() { return this.chosenDate.getTime(); } /** * Handles action-events from the date panel. * * @param e information about the event that occurred. */ public void actionPerformed(final ActionEvent e) { if (e.getActionCommand().equals("monthSelectionChanged")) { final JComboBox c = (JComboBox) e.getSource(); this.chosenDate.set(Calendar.MONTH, c.getSelectedIndex()); refreshButtons(); } else if (e.getActionCommand().equals("yearSelectionChanged")) { if (!this.refreshing) { final JComboBox c = (JComboBox) e.getSource(); final Integer y = (Integer) c.getSelectedItem(); this.chosenDate.set(Calendar.YEAR, y.intValue()); refreshYearSelector(); refreshButtons(); } } else if (e.getActionCommand().equals("todayButtonClicked")) { setDate(new Date()); } else if (e.getActionCommand().equals("dateButtonClicked")) { final JButton b = (JButton) e.getSource(); final int i = Integer.parseInt(b.getName()); final Calendar cal = getFirstVisibleDate(); cal.add(Calendar.DATE, i); setDate(cal.getTime()); } } /** * Returns a panel of buttons, each button representing a day in the month. This is a * sub-component of the DatePanel. * * @return the panel. */ private JPanel getCalendarPanel() { final JPanel p = new JPanel(new GridLayout(7, 7)); final DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(); final String[] weekDays = dateFormatSymbols.getShortWeekdays(); for (int i = 0; i < this.WEEK_DAYS.length; i++) { p.add(new JLabel(weekDays[this.WEEK_DAYS[i]], SwingConstants.CENTER)); } 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(new Integer(i).toString()); b.setFont(this.dateFont); b.setFocusPainted(false); b.setActionCommand("dateButtonClicked"); b.addActionListener(this);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?