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

📄 datepopup.java

📁 OpenSwing开源项目
💻 JAVA
字号:
package com.sunking.swing.refer;

import java.text.*;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.basic.*;

import com.sunking.swing.*;

/**
 * <p>Title: JDatePicker</p>
 * <p>Description: DatePopup 选择框弹出的日期选择面板</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
 * @version 1.0
 */
public class DatePopup extends BasicComboPopup {
    public static final SimpleDateFormat dateFormat_CN
            = new SimpleDateFormat("yyyy/MM/dd");
    protected Calendar calendar;
    protected JLabel monthLabel;
    protected JPanel days = null;

    protected Color selectedBackground;
    protected Color selectedForeground;
    protected Color background;
    protected Color foreground;
    ImageIcon todayIcon;
    /**
     * 年月格式
     */
    final SimpleDateFormat monthFormat
            = new SimpleDateFormat("yyyy-MM");

    /**
     * 日格式
     */
    final SimpleDateFormat dayFormat
            = new SimpleDateFormat("d");


    public DatePopup(JComboBox box) {
        super(box);
        todayIcon = OpenSwingUtil.getOpenSwingImage("today.gif",
                new ImageIcon());
        calendar = Calendar.getInstance();
        background = UIManager.getColor("ComboBox.background");
        foreground = UIManager.getColor("ComboBox.foreground");
        selectedBackground = UIManager.getColor(
                "ComboBox.selectionBackground");
        selectedForeground = UIManager.getColor(
                "ComboBox.selectionForeground");
        initializePopup();
    }

    /**
     * 初始化弹出面板
     */
    protected void initializePopup() {
        //   << <   9999/99/99  > >>
        JPanel header = new JPanel();
        header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
        header.setBackground(new Color(0, 0, 128));
        header.setForeground(Color.white);
        header.setPreferredSize(new Dimension(1, 25));

        JLabel label;
        label = createUpdateButton(Calendar.YEAR, -1);
        label.setText("<<");
        header.add(Box.createHorizontalStrut(12));
        header.add(label);
        header.add(Box.createHorizontalStrut(12));

        label = createUpdateButton(Calendar.MONTH, -1);
        label.setText("< ");
        header.add(label);

        monthLabel = new JLabel("", JLabel.CENTER);
        monthLabel.setBackground(new Color(0, 0, 128));
        monthLabel.setForeground(Color.white);
        header.add(Box.createHorizontalGlue());
        header.add(monthLabel);
        header.add(Box.createHorizontalGlue());

        label = createUpdateButton(Calendar.MONTH, 1);
        label.setText(" >");
        header.add(label);

        label = createUpdateButton(Calendar.YEAR, 1);
        label.setText(">>");

        header.add(Box.createHorizontalStrut(12));
        header.add(label);
        header.add(Box.createHorizontalStrut(12));

        //星期日 星期一 星期二 星期三 星期四 星期五 星期六
        JPanel pWeeks = new JPanel(new GridLayout(0, 7));
        pWeeks.setBackground(background);
        pWeeks.setOpaque(true);
        DateFormatSymbols sy = new DateFormatSymbols(Locale.getDefault());
        String strWeeks[] = sy.getShortWeekdays();
        for (int i = 1; i <= 7; i++) {
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setForeground(foreground);
            label.setText(strWeeks[i]);
            pWeeks.add(label);
        }
        //中间放日期的面板
        days = new JPanel(new GridLayout(0, 7));
        days.setBorder(new TopBottomLineBorder(Color.black));
        days.setBackground(background);
        days.setOpaque(true);
        JPanel pCenter = new JPanel(new BorderLayout());
        pCenter.setBackground(background);
        pCenter.setOpaque(true);
        pCenter.add(pWeeks, BorderLayout.NORTH);
        pCenter.add(days, BorderLayout.CENTER);
        //显示今天的日期,直接单击图标跳到今天
        JLabel lbToday = new JDayLable(new Date(), "Today:"
                                       + dateFormat_CN.format(new Date()));
        lbToday.setIcon(todayIcon);
        lbToday.setHorizontalAlignment(JLabel.LEFT);
        JPanel pSouth = new JPanel(new BorderLayout());
        pSouth.setBackground(background);
        pSouth.setForeground(foreground);
        pSouth.add(lbToday, BorderLayout.CENTER);

        setForeground(foreground);
        setBackground(background);
        setBorder(BorderFactory.createLineBorder(Color.black));
        setLayout(new BorderLayout());
        removeAll();
        add(BorderLayout.NORTH, header);
        add(BorderLayout.CENTER, pCenter);
        add(BorderLayout.SOUTH, pSouth);
    }

    /**
     * 显示弹出面板
     */
    protected void firePropertyChange(String propertyName,
                                      Object oldValue,
                                      Object newValue) {
        if (propertyName.equals("visible")) {
            if (oldValue.equals(Boolean.FALSE)
                && newValue.equals(Boolean.TRUE)) { //SHOW
                try {
                    String strDate = comboBox.getSelectedItem().toString();
                    strDate = strDate.substring(0,10);
                    Date currentSelectionDate = dateFormat_CN.parse(strDate);
                    calendar.setTime(currentSelectionDate);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                updatePopup();
            } else if (oldValue.equals(Boolean.TRUE)
                       && newValue.equals(Boolean.FALSE)) { //HIDE
            }
        }
        super.firePropertyChange(propertyName, oldValue, newValue);
    }

    /**
     * 创建上一月,下一月,上一年,下一年"按钮"
     * @param field int
     * @param amount int
     * @return JLabel
     */
    protected JLabel createUpdateButton(final int field, final int amount) {
        final JLabel label = new JLabel();
        final Border selectedBorder = new LineBorder(Color.black);
        final Border unselectedBorder = new EmptyBorder(selectedBorder.
                getBorderInsets(new JLabel()));
        label.setBorder(unselectedBorder);
        label.setBackground(new Color(0, 0, 128));
        label.setForeground(Color.white);
        label.setRequestFocusEnabled(false);
        label.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                calendar.add(field, amount);
                updatePopup();
            }

            public void mouseEntered(MouseEvent e) {
                label.setBorder(selectedBorder);
            }

            public void mouseExited(MouseEvent e) {
                label.setBorder(unselectedBorder);
            }
        });
        return label;
    }

    /**
     * 更新日期
     */
    protected void updatePopup() {
        monthLabel.setText(monthFormat.format(calendar.getTime()));
        days.removeAll();

        Calendar setupCalendar = (Calendar) calendar.clone();
        setupCalendar.set(Calendar.DAY_OF_MONTH, 1);
        int first = setupCalendar.get(Calendar.DAY_OF_WEEK);
        /**
                        1  2  3  4  5  6  7
         2  3  4  5  6  7  8  9  10 11 12 13
         8  9  10 11 12 13 14 15 16 17 18 19
         14 15 16 17 18 19 20 21 22 23 24 25
         20 21 22 23 24 25 26 27 28 29 30 31
         26 27 28 29 30 31
         */
        setupCalendar.add(Calendar.DATE, -first);
        int flag = 0;
        for (int i = 0; i < 42; i++) {
            setupCalendar.add(Calendar.DATE, 1);
            String txt = dayFormat.format(setupCalendar.getTime());
            JLabel label = new JDayLable(setupCalendar.getTime(), txt);
            days.add(label);
            if (txt.equals("1")) {
                flag++;
            }
            if (flag != 1) {
                label.setEnabled(false);
            }
        }
        days.repaint();
        pack();
    }

    /**
     * <p>Title: OpenSwing</p>
     * <p>Description: TopBottomLineBorder只带上下两条线的边界Border</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    class TopBottomLineBorder extends AbstractBorder {
        private Color lineColor;
        public TopBottomLineBorder(Color color) {
            lineColor = color;
        }

        public void paintBorder(Component c, Graphics g, int x, int y,
                                int width, int height) {
            g.setColor(lineColor);
            g.drawLine(0, 0, c.getWidth(), 0);
            g.drawLine(0, c.getHeight() - 1, c.getWidth(),
                       c.getHeight() - 1);
        }
    }


    /**
     * <p>Title: JDatePicker</p>
     * <p>Description:JDayLable 带选取日期功能的JLabel </p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
     * @version 1.0
     */
    class JDayLable extends JLabel implements MouseListener {
        Date date;
        Image img;
        boolean isToday = false;
        public JDayLable(Date date, String text) {
            setHorizontalAlignment(JLabel.CENTER);
            setForeground(foreground);
            setPreferredSize(new Dimension(40, 20));
            setToolTipText(dateFormat_CN.format(date));
            addMouseListener(this);
            this.date = date;
            setText(text);
            Date d = new Date();
            isToday = (text.length() < 3) &&
                      dateFormat_CN.format(date).equals(dateFormat_CN.format(d));
            if (calendar.getTime().equals(date)) {
                setBorder(new LineBorder(selectedBackground, 1));
            }
        }

        public void mousePressed(MouseEvent e) {}

        public void mouseClicked(MouseEvent e) {}

        public void mouseReleased(MouseEvent e) {
            if (isEnabled()) {
                setOpaque(false);
                setBackground(background);
                setForeground(foreground);
            }
            calendar.setTime(date);

            if (comboBox.isEditable() && comboBox.getEditor() != null) {
                comboBox.configureEditor(comboBox.getEditor(),
                                         dateFormat_CN.format(calendar.getTime()));
            }
            comboBox.setSelectedItem(dateFormat_CN.format(calendar.getTime()));
            comboBox.setPopupVisible(false);
        }

        public void mouseEntered(MouseEvent e) {
            if (isEnabled()) {
                setOpaque(true);
                setBackground(selectedBackground);
                setForeground(selectedForeground);
            }
        }

        public void mouseExited(MouseEvent e) {
            if (isEnabled()) {
                setOpaque(false);
                setBackground(background);
                setForeground(foreground);
            }
        }

        public void paint(Graphics g) {
            super.paint(g);
            if (isToday && todayIcon != null && isEnabled()) {
                int x = (this.getWidth() - todayIcon.getIconWidth()) / 2;
                int y = (this.getHeight() - todayIcon.getIconHeight()) / 2;
                todayIcon.paintIcon(this, g, x, y);
            }
        }
    }
}

⌨️ 快捷键说明

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