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

📄 jcalendarcombobox.java

📁 日历控件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package de.wannawork.jcalendar;

/*
 * Copyright (c) 2003, Bodo Tasche (http://www.wannawork.de) All rights
 * reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer. * Redistributions in
 * binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JWindow;
import javax.swing.RootPaneContainer;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingConstants;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicArrowButton;

/**
 * This Class creates a ComboBox for selecting the Date. If pressed, it shows a
 * Popup that contains a JCalendarPanel.
 * 
 * You can add a ChangeListener to this ComboBox to receive change events.
 * 
 * It is possible to change the Text on the ComboBox using the
 * DateFormat-Parameter.
 * 
 * @author Bodo Tasche, David Freels
 */
public class JCalendarComboBox extends JPanel implements ActionListener,
        AncestorListener, ChangeListener, SwingConstants {

    /**
     * Creates a Calendar using the current Date and current Local settings.
     */
    public JCalendarComboBox() {
        _calendarPanel = new JCalendarPanel();
        createGUI();
    }

    /**
     * Creates a Calendar using the cal-Date and current Locale Settings. It
     * doesn't use the Locale in the Calendar-Object !
     * 
     * @param cal
     *            Calendar to use
     */
    public JCalendarComboBox(Calendar cal) {
        _calendarPanel = new JCalendarPanel(cal);
        createGUI();
    }

    /**
     * Creates a Calendar using the current Date and the given Locale Settings.
     * 
     * @param locale
     *            Locale to use
     */
    public JCalendarComboBox(Locale locale) {
        _calendarPanel = new JCalendarPanel(locale);
        createGUI();
    }

    /**
     * Creates a Calender using the given Date and Locale
     * 
     * @param cal
     *            Calendar to use
     * @param locale
     *            Locale to use
     */
    public JCalendarComboBox(Calendar cal, Locale locale) {
        _calendarPanel = new JCalendarPanel(cal, locale);
        createGUI();
    }

    /**
     * Creates a Calender using the given Calendar, Locale and DateFormat.
     * 
     * @param cal
     *            Calendar to use
     * @param locale
     *            Locale to use
     * @param dateFormat
     *            DateFormat for the ComboBox
     */
    public JCalendarComboBox(Calendar cal, Locale locale, DateFormat dateFormat) {
        _calendarPanel = new JCalendarPanel(cal, locale, dateFormat);
        createGUI();
    }

    /**
     * Creates a Calender using the given Calendar, Locale and DateFormat.
     * 
     * @param cal
     *            Calendar to use
     * @param locale
     *            Locale to use
     * @param dateFormat
     *            DateFormat for the ComboBox
     * @param location
     *            Location of the Popup (LEFT, CENTER or RIGHT)
     */
    public JCalendarComboBox(Calendar cal, Locale locale,
            DateFormat dateFormat, int location) {
        _calendarPanel = new JCalendarPanel(cal, locale, dateFormat);
        _popupLocation = location;
        createGUI();
    }

    /**
     * Creates a Calender using the given Calendar, Locale and DateFormat.
     * 
     * @param cal
     *            Calendar to use
     * @param locale
     *            Locale to use
     * @param dateFormat
     *            DateFormat for the ComboBox
     * @param location
     *            Location of the Popup (LEFT, CENTER or RIGHT)
     * @param flat
     *            Flat Buttons for next/last Month/Year
     */
    public JCalendarComboBox(Calendar cal, Locale locale,
            DateFormat dateFormat, int location, boolean flat) {
        _calendarPanel = new JCalendarPanel(cal, locale, dateFormat, flat);
        _popupLocation = location;
        createGUI();
    }

    /**
     * Creates the GUI for the JCalendarComboBox
     */
    private void createGUI() {
        _calendarPanel.setListenerModus(JCalendarPanel.FIRE_DAYCHANGES);
        _selected = (Calendar) _calendarPanel.getCalendar().clone();

        _calendarPanel.addChangeListener(this);
        _calendarPanel.setBorder(BorderFactory.createLineBorder(Color.black));

        setLayout(new BorderLayout());

        _spinner.setModel(new SpinnerDateModel());
        _spinner
                .setEditor(new JSpinner.DateEditor(_spinner,
                        ((SimpleDateFormat) _calendarPanel.getDateFormat())
                                .toPattern()));
        _spinner.getModel().setValue(_selected.getTime());
        _spinner.setBorder(null);
        
        ((JSpinner.DefaultEditor )_spinner.getEditor ()).getTextField().addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                System.out.println("Focus lost!!");
                if (!_calendarPanel.getCalendar().getTime().equals(_spinner.getModel().getValue())) {
                    Date date = (Date) _spinner.getModel().getValue();
                    _selected.setTime(date);
                    _calendarPanel.setCalendar(_selected);
                    fireChangeEvent();
                }
            }
        });
        

        _button = new BasicArrowButton(BasicArrowButton.SOUTH);

        Insets insets = new Insets(_button.getMargin().top, 0, _button
                .getMargin().bottom, 0);
        _button.setMargin(insets);
        _button.addActionListener(this);
        _button.setEnabled(true);

        _button.addFocusListener(new FocusAdapter() {

            /**
             * Invoked when a component gains the keyboard focus.
             */
            public void focusGained(FocusEvent e) {
                if (e.getOppositeComponent() != null) {
                    if (e.getOppositeComponent() instanceof JComponent) {
                        JComponent opposite = (JComponent) e
                                .getOppositeComponent();
                        if ((opposite.getTopLevelAncestor() != _calendarWindow)
                                && (!_calendarWindowFocusLost))
                                _calendarWindowFocusLost = false;
                    }
                }
            }
        });

        add(_spinner, BorderLayout.CENTER);
        add(_button, BorderLayout.EAST);
    }

    /**
     * Creates the CalendarWindow-Popup
     */
    private void createCalendarWindow() {
        Window ancestor = (Window) this.getTopLevelAncestor();

        _calendarWindow = new JWindow(ancestor);

        JPanel contentPanel = (JPanel) _calendarWindow.getContentPane();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.add(_calendarPanel);

        ((JComponent) ((RootPaneContainer) ancestor).getContentPane())
                .addAncestorListener(this);

        ((JComponent) ((RootPaneContainer) ancestor).getContentPane())
                .addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {
                        hideCalendar();
                    }
                });

        _calendarWindow.addWindowListener(new WindowAdapter() {

            public void windowDeactivated(WindowEvent e) {
                hideCalendar();
            }
        });

        _calendarWindow.addWindowFocusListener(new WindowAdapter() {

            public void windowLostFocus(WindowEvent e) {
                if (_button.isSelected()) {
                    _calendarWindowFocusLost = true;
                }

                hideCalendar();
            }
        });

        ancestor.addComponentListener(new ComponentAdapter() {

            public void componentResized(ComponentEvent e) {
                hideCalendar();
            }

            public void componentMoved(ComponentEvent e) {
                hideCalendar();
            }

            public void componentShown(ComponentEvent e) {
                hideCalendar();
            }

            public void componentHidden(ComponentEvent e) {
                hideCalendar();
            }
        });
        ComponentListener list;

        _calendarWindow.pack();
    }

    /**
     * Returns the current seleted Date as Calendar
     * 
     * @return current selected Date
     */
    public Calendar getCalendar() {
        return _calendarPanel.getCalendar();
    }

    /**
     * Sets the current selected Date
     * 
     * @param cal
     *            Date to select
     */
    public void setCalendar(Calendar cal) {
        _calendarPanel.setCalendar(cal);
        _spinner.getModel().setValue(_calendarPanel.getCalendar().getTime());
    }

    /**
     * Returns the JCalendarPanel that is shown in the PopUp
     * 
     * @return JCalendarPanel in the PopUp
     */
    public JCalendarPanel getCalendarPanel() {
        return _calendarPanel;
    }

    /**
     * Sets the Popup Location (Left/Right/Center)
     * 
     * @param location
     */
    public void setPopUpLocation(int location) {
        _popupLocation = location;
    }

⌨️ 快捷键说明

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