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

📄 jmdatepicker.java.svn-base

📁 梦界家园程序开发基底框架
💻 SVN-BASE
字号:
package jm.framework.gui.module;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
 * <p>Title: JM 整合Swing控件,使用配置信息</p>
 *
 * <p>Copyright: Copyright (c) 2004-2006</p>
 *
 * <p>Company: 1SHome</p>
 *
 * <p>@author Spook</p>
 *
 * @since 1.3
 * @see JDK 1.5.0.6
 */
public class JMDatePicker extends JPanel
{
    private class EventHandler
        implements ActionListener, HierarchyBoundsListener, ChangeListener
    {

        public void ancestorMoved(HierarchyEvent e)
        {
            hidePopup();
        }

        public void ancestorResized(HierarchyEvent e)
        {
            hidePopup();
        }

        public void actionPerformed(ActionEvent arg0)
        {
            if(arg0.getSource().equals(bShowDatePanel))
            {
                if(popup == null)
                    showPopup();
                else
                    hidePopup();
            } else
            if(arg0.getSource().equals(datePanel))
                hidePopup();
        }

        public void stateChanged(ChangeEvent arg)
        {
            if(arg.getSource().equals(datePanel) && tfDate != null)
                tfDate.setText(datePanel.toString());
        }

        private EventHandler()
        {
        }

    }


    public static void main(String args[])
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(InstantiationException e)
        {
            e.printStackTrace();
        }
        catch(IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch(UnsupportedLookAndFeelException e)
        {
            e.printStackTrace();
        }
        JFrame testFrame = new JFrame();
        testFrame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent arg0)
            {
                System.exit(0);
            }

        });
        testFrame.setSize(600, 300);
        JPanel jPanel = new JPanel();
        JMDatePicker test = new JMDatePicker();
        test.setBounds(new Rectangle(30,30,500,50));
        jPanel.add(test);
        jPanel.setLayout(null);
        testFrame.getContentPane().add(jPanel);
        testFrame.setVisible(true);
    }

    public JMDatePicker()
    {
        this(new Date(), 3);
    }

    public JMDatePicker(Date date, int dateFormat)
    {
        setLayout(new GridBagLayout());
        EventHandler eventHandler = new EventHandler();
        addHierarchyBoundsListener(eventHandler);
        datePanel = new JMDatePanel();
        datePanel.addChangeListener(eventHandler);
        datePanel.addActionListener(eventHandler);
        datePanel.setStartDate(date);
        datePanel.setDateFormat(dateFormat);
        tfDate = new JTextField(datePanel.toString());
        tfDate.setEditable(false);
        tfDate.setBackground(UIManager.getColor("TextField.background"));
        tfDate.setHorizontalAlignment(0);
        bShowDatePanel = new JButton("...");
        bShowDatePanel.addActionListener(eventHandler);
        bShowDatePanel.setMargin(new Insets(0, 0, 0, 0));
        int height = tfDate.getPreferredSize().height;
        int width = height;
        bShowDatePanel.setPreferredSize(new Dimension(width, height + 1));
        add(tfDate, new GridBagConstraints(0, 0, 1, 1, 1.0D, 0.0D, 17, 2, new Insets(0, 0, 0, 0), 0, 0));
        add(bShowDatePanel, new GridBagConstraints(1, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 0, 0, 0), 0, 0));
        add(Box.createGlue(), new GridBagConstraints(0, 1, 2, 1, 1.0D, 1.0D, 17, 1, new Insets(0, 0, 0, 0), 0, 0));
    }

    public String toString()
    {
        return datePanel.toString();
    }

    public void setDate(Date date)
    {
        datePanel.setStartDate(date);
    }

    public Date getDate()
    {
        return datePanel.getDate();
    }

    public void setDateFormat(int format)
    {
        datePanel.setDateFormat(format);
    }

    private void showPopup()
    {
        if(popup == null)
        {
            PopupFactory fac = new PopupFactory();
            Point xy = getLocationOnScreen();
            datePanel.setVisible(true);
            popup = fac.getPopup(this, datePanel, (int)xy.getX(), (int)((xy.getY() + (double)getHeight()) - 1.0D));
            popup.show();
        }
    }

    private void hidePopup()
    {
        if(popup != null)
        {
            tfDate.setText(datePanel.toString());
            popup.hide();
            popup = null;
        }
    }

    public void setEnabled(boolean enabled)
    {
        super.setEnabled(enabled);
        if(enabled)
        {
            tfDate.setEnabled(true);
            tfDate.setBackground(UIManager.getColor("TextField.background"));
        } else
        {
            tfDate.setEnabled(false);
        }
        bShowDatePanel.setEnabled(enabled);
    }

    public void setToolTipText(String toolTip)
    {
        tfDate.setToolTipText(toolTip);
        bShowDatePanel.setToolTipText(toolTip);
    }

    private Popup popup;
    private JTextField tfDate;
    private JButton bShowDatePanel;
    private JMDatePanel datePanel;

}

⌨️ 快捷键说明

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