时间组件

单时间组件

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Date;
import java.util.Locale;

import javax.swing.JPanel;

import com.eltima.components.ui.DatePicker;


/**
 * 时间选择框输入框
 * 
 * @author luojialin
 * @since 2014-3-14
 */
public class SFDatePicker extends JPanel {

	private static final long serialVersionUID = 3385428174172494736L;
	
	
    /*日期显示格式*/
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	/*日期时间格式*/
	public static final String YYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss";
	
    private static Font font=new Font("Times New Roman", Font.PLAIN, 14);
	private static Dimension dimension=new Dimension(100,24);
	
	private DatePicker startDatepick;
	
	public SFDatePicker() {
        this(YYY_MM_DD_HH_MM_SS);
	}
	
	public SFDatePicker(String formate) {
        super();
        
        this.setOpaque(false);
		startDatepick = new DatePicker(new Date(),formate,font,dimension);
		startDatepick.setLocale(Locale.CHINA);//设置国家
		startDatepick.setTimePanleVisible(true);//设置时钟面板可见
		startDatepick.setBorder(null);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(startDatepick);
	}
	

	
	/**
	 * 设置是否可以使用
	 * @param flag
	 */
	public void setEnable(boolean flag){
		this.setEnabled(flag);
	}
	

	
	/**
	 * 获取控件时间
	 * @return
	 */
	public Date getDate(){
		return (Date)startDatepick.getValue();
	}
	
	/**
	 * 设置控件时间
	 * @param date
	 */
	public void setDate(Date date){
		startDatepick.a(date);
	}

	
	/**
	 * 设置 日期时间 显示样式
	 * @param formate
	 * 格式:YYYY_MM_DD</P>
	 * YYY_MM_DD_HH_MM_SS</P>
	 */
	public void setFormat(String format){
		
		Date date = new Date();
    	if(SFDatePicker.YYYY_MM_DD.equals(format)){
    		format = YYYY_MM_DD;
    		startDatepick = new DatePicker(date,format,font,dimension);
    		startDatepick = new DatePicker(date,format,font,dimension);
    		startDatepick.setTimePanleVisible(false);
    	}else if(SFDatePicker.YYY_MM_DD_HH_MM_SS.equals(format)){
    		format = YYY_MM_DD_HH_MM_SS;
    		startDatepick = new DatePicker(date,format,font,dimension);
    		startDatepick = new DatePicker(date,format,font,dimension);
    		startDatepick.setTimePanleVisible(true);
    	}
    }

	
	public void clearDatePicker(){
		startDatepick.getInnerTextField().setText("");
	}

	
	
}

 

 

双时间组件

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.swing.JLabel;

import com.eltima.components.ui.DatePicker;
import com.sf.framework.i18n.Messages;
import com.sf.module.pcss.core.message.MessageManager;


/**
 * 双时间选择框输入框
 * 
 * @author luojialin
 * @since 2014-2-21
 */
public class DoubleDatePicker extends javax.swing.JPanel {

	private static final long serialVersionUID = 3385428174172494736L;
	
	
    /*日期显示格式*/
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	/*日期时间格式*/
	public static final String YYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss";
	
    private static Font font=new Font("Times New Roman", Font.PLAIN, 14);
	private static Dimension dimension=new Dimension(100,24);
	/**
	 *开始时间 
	 */
	private DatePicker startDatepick;
	/**
	 * 结束时间
	 */
	private DatePicker endDatepick;
	
	private JLabel betwenLbl;
	
	private JLabel redStartLbl;
	
	/**红星**/
	public  boolean redStart = false;
	
	public DoubleDatePicker() {
        this(null,false);
	}
	

	/**
	 * 双时间组件</P>
	 * 支持日期格式 :"yyyy-MM-dd"
	 * 支持日期时间格式:"yyyy-MM-dd hh:mm:ss"
	 * @param title   两个时间之间的 文字
	 */
	public DoubleDatePicker(String title) {
		this(title, false);
	}
	
	/**
	 * 双时间组件</P>
	 * 支持日期格式 :"yyyy-MM-dd"
	 * 支持日期时间格式:"yyyy-MM-dd hh:mm:ss"
	 * 
	 * @param title   两个时间之间的 文字
	 * @param redStart 是否显示 * 号
	 */
	public DoubleDatePicker(String title,  boolean redStart) {
		super();
		
		initGUI(title,null, redStart);
		
	}
	
	/**
	 * 双时间组件</P>
	 * 支持日期格式 :"yyyy-MM-dd"
	 * 支持日期时间格式:"yyyy-MM-dd hh:mm:ss"
	 * 
	 * @param title   两个时间之间的 文字
	 * @param formate 显示 格式
	 */
	public DoubleDatePicker(String title,  String formate) {
		super();
		initGUI(title,formate, false);
		
	}
	
	
	private void initGUI(String title, String formate, boolean redStart){
		this.setOpaque(false);
		betwenLbl = new JLabel(title);
		startDatepick = new DatePicker(new Date(),formate,font,dimension);
		endDatepick = new DatePicker(new Date(),formate,font,dimension);
		if(redStart)
			redStartLbl = new JLabel("<html><font color = red>* </font></html>");
		else
			redStartLbl = new JLabel();
		
		startDatepick.setLocale(Locale.CHINA);//设置国家
		startDatepick.setTimePanleVisible(true);//设置时钟面板可见
		startDatepick.setBorder(null);
		
		endDatepick.setLocale(Locale.CHINA);//设置国家
		endDatepick.setTimePanleVisible(true);//设置时钟面板可见
		endDatepick.setBorder(null);
		startDatepick.setPreferredSize(dimension);
		endDatepick.setPreferredSize(dimension);
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(startDatepick);
		this.add(betwenLbl);
		this.add(endDatepick);
		this.add(redStartLbl);
		
		this.startDatepick.getInnerTextField().addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {
			}
			@Override
			public void focusLost(FocusEvent e) {
				if("".equals(startDatepick.getText().trim())){
					startDatepick.getInnerTextField().setValue(null);
				}else{
					 verify();
				}
			}
		});
		this.endDatepick.getInnerTextField().addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {
			}
			@Override
			public void focusLost(FocusEvent e) {
				if("".equals(endDatepick.getText().trim())){
					endDatepick.getInnerTextField().setValue(null);
				}else{
					verify();
				}
			}
		});
		//this.setPreferredSize(new Dimension(600, 30));
	}

	private void verify(){
		Date start = (Date)startDatepick.getValue();
		Date end = (Date)endDatepick.getValue();
		if(start != null && end != null){
			if(start.after(end)){
				MessageManager.getInstance().error("",Messages.getString("time.setting.error"));
				endDatepick.getInnerTextField().setValue(null);
			}
		}
	}
	

	
	/**
	 * 设置是否可以使用
	 * @param flag
	 */
	public void setEnable(boolean flag){
		this.startDatepick.setEnabled(flag);
		this.endDatepick.setEnabled(flag);
	}
	

	/**
	 * 获取时间</P>
	 * 
	 * 返回List 格式的 Date 数组
	 * @return
	 */
	public List<Date> getDate(){
		Date start = (Date)this.startDatepick.getValue();
		Date end = (Date)this.endDatepick.getValue();
		List<Date> list = new ArrayList<Date>();
		list.add(start);
		list.add(end);
		return list;
	}
	
	/**
	 * 获取第一个控件时间
	 * @return
	 */
	public Date getFirstDate(){
		return (Date)this.startDatepick.getValue();
	}
	
	/**
	 * 获取第二个控件时间
	 * @return
	 */
	public Date getSecondDate(){
		return (Date)this.endDatepick.getValue();
	}
	
	/**
	 * 设置第一个控件时间
	 * @param date
	 */
	public void setFirstDate(Date date){
		startDatepick.a(date);
	}
	
	/**
	 * 设置第二个控件时间
	 * @param date
	 */
	public void setSecondDate(Date date){
		endDatepick.a(date);
	}
	
	public void setDate(List<Date> list){
		if(list.get(0) != null && list != null)
			startDatepick.a(list.get(0));
		if(list.get(1) != null && list != null)
			endDatepick.a(list.get(1));
	}

		
	public boolean isRedStart() {
		return redStart;
	}

	/**
	 * 设置红星
	 * @param redStart
	 */
	public void setRedStart(boolean redStart) {
		if(redStart){
			redStartLbl.setText("<html><font color = red>* </font></html>");
			redStart = true;
		}else{
			redStartLbl.setText("");
			redStart = false;
		}
			 
	}
	
	/**
	 * 设置 日期时间 显示样式
	 * @param formate
	 * 格式:YYYY_MM_DD</P>
	 * YYY_MM_DD_HH_MM_SS</P>
	 */
	public void setFormat(String format){
		Date date = new Date();
    	if(DoubleDatePicker.YYYY_MM_DD.equals(format)){
    		format = YYYY_MM_DD;
    		startDatepick = new DatePicker(date,format,font,dimension);
    		endDatepick = new DatePicker(date,format,font,dimension);
    		startDatepick.setTimePanleVisible(false);
    	}else if(DoubleDatePicker.YYY_MM_DD_HH_MM_SS.equals(format)){
    		format = YYY_MM_DD_HH_MM_SS;
    		startDatepick = new DatePicker(date,format,font,dimension);
    		endDatepick = new DatePicker(date,format,font,dimension);
    		startDatepick.setTimePanleVisible(true);
    	}
    }

	public void setTitleFont(Font font){
		this.redStartLbl.setFont(font);
		this.betwenLbl.setFont(font);
	}
	
	public void setDatePickerFont(Font font){
		this.startDatepick.setFont(font);
		this.endDatepick.setFont(font);
	}
	
	public void setDatePickerSize(Dimension dimsion){
		this.startDatepick.setPreferredSize(dimsion);
		this.endDatepick.setPreferredSize(dimsion);
	}
	
	
	public DatePicker getStartDatepick() {
		return startDatepick;
	}

	public void setStartDatepick(DatePicker startDatepick) {
		this.startDatepick = startDatepick;
	}

	public DatePicker getEndDatepick() {
		return endDatepick;
	}

	public void setEndDatepick(DatePicker endDatepick) {
		this.endDatepick = endDatepick;
	}
	
	public void clearDatePicker(){
		startDatepick.getInnerTextField().setText("");
		endDatepick.getInnerTextField().setText("");
	}

	
	
}

 

 

你可能感兴趣的:(时间)