⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendarpanel.java

📁 一个demo是关于swing
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  NachoCalendar * * Project Info:  http://nachocalendar.sf.net * * 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.] * * Changes * ------- *  *  2005-01-02   Added property change support. *  2004-12-24   Reimplemented layout functions. *               Added keyboard navigation. *               Reimplemented selection model, now supports multiple selection    *  2004-12-12   Reimplemented the constructor. *  2004-10-22   setEnabled(boolean b) overriden, now works *  2004-10-17   Added keylistener support. *  2004-10-01   Checked with checkstyle * * ------- * * CalendarPanel.java * * Created on August 12, 2004, 4:05 PM */package net.sf.nachocalendar.components;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.event.AdjustmentEvent;import java.awt.event.AdjustmentListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.text.ParseException;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import net.sf.nachocalendar.event.DateSelectionEvent;import net.sf.nachocalendar.event.DateSelectionListener;import net.sf.nachocalendar.event.MonthChangeEvent;import net.sf.nachocalendar.event.MonthChangeListener;import net.sf.nachocalendar.model.DataModel;import net.sf.nachocalendar.model.DateSelectionModel;import net.sf.nachocalendar.model.DefaultDateSelectionModel;/** * Panel used to show many months at once. * @author Ignacio Merani */public class CalendarPanel extends JPanel implements ChangeListener {    private boolean antiAliased, enabled;    private KeyListener klistener;    private MouseListener mlistener;    private MonthChangeListener monthlistener;    private DateSelectionListener listlistener;    private DateSelectionModel dateSelectionModel;    private DataModel datamodel;    private HeaderRenderer headerrenderer;    private DayRenderer dayrenderer;    private Calendar navigation, calendar;    private Date minDate, maxDate;        /** Array with the panels. */    private MonthPanel[] months;        /** Orientation. */    private int orientation;        /** Scroll Position. */    private int scrollPosition;        /**     * Horizontal orientation.     */    public static final int HORIZONTAL = 0;        /** Vertical orientation. */    public static final int VERTICAL = 1;        /** Left Position.*/    public static final int LEFT = 0;        /** Right Position. */    public static final int RIGHT = 1;        /** Up Position. */    public static final int UP = 0;        /** Down Position. */    public static final int DOWN = 1;        /** Scroll Bar. */    private JScrollBar scroll;        /** Year Scroller. */    private YearScroller ys;        /** Array representing the working days. */    private boolean[] workingdays = {false, true, true, true, true, true, true };        /** Calendar used to calc dates. */    private Calendar cal;        private int showingmonth, showingyear, quantity;    private Date date;    private JPanel abajo;    private boolean showWeekNumber;        /** Utility field holding list of ChangeListeners. */    private transient java.util.ArrayList changeListenerList;        /** Holds value of property yearPosition.  */    private int yearPosition;        /** Default constructor, it constructs a vertical panel with 3 months. */    public CalendarPanel() {        this(3, VERTICAL);    }        /**     * It constructs a  panel with 3 months and the provided orientation.     * @param quantity quantity of months to show at once     */    public CalendarPanel(int quantity) {        this(quantity, VERTICAL);    }        /**     * It constructs a panel with the provided quantity and orientation.     * @param quantity months to show at once     * @param orientation orientation     */    public CalendarPanel(int quantity, int orientation) {        this(quantity, orientation, true);    }        /**     * Creates a new instance of CalendarPanel.     * @param showWeekNumber true to show the week numbers     * @param quantity months to show at once     * @param orientation the orientation     */    public CalendarPanel(int quantity, int orientation, boolean showWeekNumber) {        if (quantity < 1) {            quantity = 1;        }        if (quantity > 12) {            quantity = 12;        }        this.quantity = quantity;        this.showWeekNumber = showWeekNumber;        this.orientation = orientation;        navigation = new GregorianCalendar();        calendar = new GregorianCalendar();        dateSelectionModel = new DefaultDateSelectionModel();        ys = new YearScroller();        int or = (orientation == HORIZONTAL ? JScrollBar.HORIZONTAL : JScrollBar.VERTICAL);        scroll = new JScrollBar(or, 0, 5, 0, (17 - quantity));        scroll.setUnitIncrement(1);        scroll.setBlockIncrement(3);        cal = new GregorianCalendar();        setLayout(new BorderLayout());                createListeners();        setQuantity(quantity);                if (orientation == VERTICAL) {            layoutVertical();        } else {            layoutHorizontal();        }                setShowingYear(cal.get(Calendar.YEAR));        setShowingMonth(0);        setValue(new Date());        //showMonth(cal.getTime());    }        /** Utility method used during initialization. */    private void createListeners() {        scroll.addAdjustmentListener(new AdjustmentListener() {            public void adjustmentValueChanged(AdjustmentEvent e) {                setShowingMonth(e.getValue());            }        });                ys.addChangeListener(new ChangeListener() {            public void stateChanged(ChangeEvent e) {                setShowingYear(ys.getYear());            }        });                addMouseWheelListener(new MouseWheelListener() {            public void mouseWheelMoved(MouseWheelEvent e) {                if (!isEnabled()) return;                int q = e.getWheelRotation();                int value = scroll.getValue();                value += q;                if (value < 0) {                    value = 0;                }                if (value > 11) {                    value = 11;                }                scroll.setValue(value);            }        });                // listener to pass events        klistener = new KeyListener() {            public void keyPressed(KeyEvent e) {                boolean changed = false;                int keycode = e.getKeyCode();                navigation.setTime(calendar.getTime());                if ((keycode == KeyEvent.VK_LEFT) || (keycode == 226)) {                    int month = navigation.get(Calendar.MONTH);                    navigation.add(Calendar.DAY_OF_YEAR, -1);                    changed = true;                }                if ((keycode == KeyEvent.VK_RIGHT) || (keycode == 227)) {                    int month = navigation.get(Calendar.MONTH);                    navigation.add(Calendar.DAY_OF_YEAR, 1);                    changed = true;                }                if ((keycode == KeyEvent.VK_UP) || (keycode == 224)) {                    int month = navigation.get(Calendar.MONTH);                    navigation.add(Calendar.DAY_OF_YEAR, -7);                    changed = true;                }                if ((keycode == KeyEvent.VK_DOWN) || (keycode == 225)) {                    int month = navigation.get(Calendar.MONTH);                    navigation.add(Calendar.DAY_OF_YEAR, 7);                    changed = true;                }                if ((keycode == KeyEvent.VK_PAGE_UP)) {                    int month = navigation.get(Calendar.MONTH);                    navigation.add(Calendar.MONTH, -1);                    scroll.setValue(navigation.get(Calendar.MONTH));                    changed = true;                }                if ((keycode == KeyEvent.VK_PAGE_DOWN)) {                    int month = navigation.get(Calendar.MONTH);                    navigation.add(Calendar.MONTH, 1);                    scroll.setValue(navigation.get(Calendar.MONTH));                    changed = true;                }                if (changed) {                    if (!isShowing(navigation.getTime())) {                        showMonth(navigation.getTime());                    }                    if ((!e.isControlDown()) && (!e.isShiftDown())) {                        dateSelectionModel.clearSelection();                        if (e.isShiftDown()) {                            dateSelectionModel.addSelectionInterval(                                    dateSelectionModel.getLeadSelectionDate(),                                    navigation.getTime());                        } else                            dateSelectionModel.addSelectionInterval(navigation                                    .getTime(), navigation.getTime());                    } else {                        if (e.isShiftDown()) {                            dateSelectionModel.addSelectionInterval(                                    dateSelectionModel.getLeadSelectionDate(),                                    navigation.getTime());                        } else {                            if (dateSelectionModel.isSelectedDate(navigation                                    .getTime())) {                                dateSelectionModel.removeSelectionInterval(                                        navigation.getTime(), navigation                                                .getTime());                            } else                                dateSelectionModel.addSelectionInterval(                                        navigation.getTime(), navigation                                                .getTime());                        }                    }                    dateSelectionModel.setLeadSelectionDate(navigation                            .getTime());                    calendar.setTime(navigation.getTime());                    refreshSelection();                    repaint();                    //monthpanel.repaint();                }                fireKeyListenerKeyPressed(e);            }            public void keyReleased(KeyEvent e) {                fireKeyListenerKeyReleased(e);            }            public void keyTyped(KeyEvent e) {                fireKeyListenerKeyTyped(e);            }        };                monthlistener = new MonthChangeListener() {            public void monthIncreased(MonthChangeEvent e) {                setDate(e.getDate());            }                        public void monthDecreased(MonthChangeEvent e) {                setDate(e.getDate());            }        };                        mlistener = new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                DayPanel dp = (DayPanel) e.getSource();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -