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

📄 datepopup.java

📁 Java写的日期选择控件
💻 JAVA
字号:
/*
 * DatePopup.java
 *
 */

package dyno.swing.beans;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;

/**
 *
 * @author  rehte
 */
public class DatePopup extends JPopupMenu{
    static{
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    }
    
    static int getMonth(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.MONTH)+1;
    }
    static int getYear(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.YEAR);
    }
    static int getDate(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.DATE);
    }
    static class Month {
        private int month;
        public Month(Date d){
            month=getMonth(d);
        }
        public Month(int m){
            month=m;
        }
        public int month(){
            return month;
        }
        public String toString(){
            return names[month-1];
        }
        public boolean equals(Object o){
            if(o==null)
                return false;
            if(!(o instanceof Month))
                return false;
            Month m=(Month)o;
            return m.month==month;
        }
        private static final String[]names={
            "January",
            "Februray",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December",
        };
    }
    
    private static final Month[] MONTHS={
        new Month(1),
        new Month(2),
        new Month(3),
        new Month(4),
        new Month(5),
        new Month(6),
        new Month(7),
        new Month(8),
        new Month(9),
        new Month(10),
        new Month(11),
        new Month(12),
    };
    /** Creates new form DatePopup */
    public DatePopup() {
        initComponents();
        DefaultComboBoxModel model=new DefaultComboBoxModel(MONTHS);
        cbMonth.setModel(model);
        BasicComboPopup bpopup=(BasicComboPopup)((BasicComboBoxUI)cbMonth.getUI()).getAccessibleChild(cbMonth, 0);
        Date today=new Date();
        datePanel.setCalendarDate(today);
        cbMonth.setSelectedItem(new Month(today));
        spYear.setValue(new Integer(getYear(today)));
    }
    
    private ArrayList<ActionListener> listeners=new ArrayList<ActionListener>();
    
    public void addActionListener(ActionListener l){
        if(!listeners.contains(l))
            listeners.add(l);
    }
    public void removeActionListener(ActionListener l){
        if(listeners.contains(l))
            listeners.remove(l);
    }
    protected void fireActionPerformed(ActionEvent e){
        for(ActionListener l:listeners)
            l.actionPerformed(e);
    }
    public void setAnimationEffect(int effect){
        datePanel.setEffect(effect);
    }
    private Date date;
    public Date getDate(){
        return date;
    }
    public void setDate(Date d){
        date=d;
        DateModel m=new DateModel(d);
        datePanel.setModel(m);
        cbMonth.setSelectedItem(new Month(d));
        spYear.setValue(new Integer(m.getYear()));
    }
    private void initComponents() {
        jPanel1 = new JPanel();
        cbMonth = new MonthCombo();
        spYear = new JSpinner();
        btnLeft = new JButton();
        btnRight = new JButton();
        datePanel = new DatePanel();
        jPanel2 = new JPanel();
        btnClear = new JButton();
        btnToday = new JButton();
        setLayout(new BorderLayout());
        
        setBorder(BorderFactory.createLineBorder(new Color(122, 150, 223)));
        jPanel1.setBackground(new Color(122, 150, 223));
        jPanel1.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
        cbMonth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                cbMonthActionPerformed(evt);
            }
        });
        
        spYear.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent evt) {
                spYearStateChanged(evt);
            }
        });
        
        btnLeft.setIcon(new ImageIcon(getClass().getResource("/dyno/swing/beans/left_arrow.png")));
        btnLeft.setBorder(null);
        btnLeft.setFocusPainted(false);
        btnLeft.setMaximumSize(new Dimension(18, 18));
        btnLeft.setMinimumSize(new Dimension(18, 18));
        btnLeft.setPreferredSize(new Dimension(18, 18));
        btnLeft.setPressedIcon(new ImageIcon(getClass().getResource("/dyno/swing/beans/pressed_left_arrow.png")));
        btnLeft.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnLeftActionPerformed(evt);
            }
        });
        
        btnRight.setBackground(new Color(122, 150, 223));
        btnRight.setIcon(new ImageIcon(getClass().getResource("/dyno/swing/beans/right_arrow.png")));
        btnRight.setBorder(null);
        btnRight.setFocusPainted(false);
        btnRight.setMargin(new Insets(0, 0, 0, 0));
        btnRight.setMaximumSize(new Dimension(18, 18));
        btnRight.setMinimumSize(new Dimension(18, 18));
        btnRight.setPreferredSize(new Dimension(18, 18));
        btnRight.setPressedIcon(new ImageIcon(getClass().getResource("/dyno/swing/beans/pressed_right_arrow.png")));
        btnRight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnRightActionPerformed(evt);
            }
        });
        
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(cbMonth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spYear, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(btnRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
                );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(btnLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(btnRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(cbMonth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(spYear, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                );
        add(jPanel1, BorderLayout.NORTH);
        
        datePanel.setBackground(Color.white);
        datePanel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                datePanelActionPerformed(evt);
            }
        });
        
        GroupLayout datePanelLayout = new GroupLayout(datePanel);
        datePanel.setLayout(datePanelLayout);
        datePanelLayout.setHorizontalGroup(
                datePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 261, Short.MAX_VALUE)
                );
        datePanelLayout.setVerticalGroup(
                datePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 131, Short.MAX_VALUE)
                );
        add(datePanel, BorderLayout.CENTER);
        
        jPanel2.setBackground(new Color(122, 150, 223));
        jPanel2.setPreferredSize(new Dimension(100, 40));
        btnClear.setText("Clear");
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });
        
        btnToday.setText("Today");
        btnToday.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnTodayActionPerformed(evt);
            }
        });
        
        GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(60, Short.MAX_VALUE)
                .addComponent(btnToday)
                .addGap(44, 44, 44)
                .addComponent(btnClear)
                .addGap(67, 67, 67))
                );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(btnClear)
                .addComponent(btnToday))
                .addContainerGap())
                );
        add(jPanel2, BorderLayout.SOUTH);
        
    }// </editor-fold>
    
    private void datePanelActionPerformed(ActionEvent evt) {
        DateModel m=datePanel.getModel();
        date=new Date(m.getYear()-1900, m.getMonth()-1, m.getDate());
        fireActionPerformed(new ActionEvent(this, 0, "choose"));
    }
    
    private void btnClearActionPerformed(ActionEvent evt) {
        date=null;
        fireActionPerformed(new ActionEvent(this, 0, "choose"));
    }
    
    private void btnTodayActionPerformed(ActionEvent evt) {
        date=new Date();
        DateModel m=new DateModel(date);
        datePanel.setModel(m);
        cbMonth.setSelectedItem(new Month(m.getMonth()));
        spYear.setValue(new Integer(m.getYear()));
        fireActionPerformed(new ActionEvent(this, 0, "choose"));
    }
    
    private void btnLeftActionPerformed(ActionEvent evt) {
        DateModel model=datePanel.getModel();
        int m=model.getMonth();
        int y=model.getYear();
        if(m==1){
            m=12;
            y--;
        }else
            m--;
        DateModel newModel=new DateModel(y, m, model.getDate());
        datePanel.setModel(newModel);
        spYear.setValue(new Integer(y));
        cbMonth.setSelectedItem(new Month(m));
    }
    
    private void btnRightActionPerformed(ActionEvent evt) {
        DateModel model=datePanel.getModel();
        int m=model.getMonth();
        int y=model.getYear();
        if(m==12){
            m=1;
            y++;
        }else
            m++;
        DateModel newModel=new DateModel(y, m, model.getDate());
        datePanel.setModel(newModel);
        spYear.setValue(new Integer(y));
        cbMonth.setSelectedItem(new Month(m));
    }
    
    private void spYearStateChanged(ChangeEvent evt) {
        DateModel model=datePanel.getModel();
        DateModel newModel=new DateModel(((Integer)spYear.getValue()).intValue(), model.getMonth(), model.getDate());
        datePanel.setModel(newModel);
    }
    
    private void cbMonthActionPerformed(ActionEvent evt) {
        Object o=cbMonth.getSelectedItem();
        if(o==null)
            return;
        Month m=(Month)o;
        DateModel model=datePanel.getModel();
        DateModel newModel=new DateModel(model.getYear(), m.month(), model.getDate());
        datePanel.setModel(newModel);
    }
    
    
    
    // Variables declaration - do not modify
    private JButton btnClear;
    private JButton btnLeft;
    private JButton btnRight;
    private JButton btnToday;
    private JComboBox cbMonth;
    private dyno.swing.beans.DatePanel datePanel;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JSpinner spYear;
    // End of variables declaration
    
}

⌨️ 快捷键说明

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