一次项目研发中需要日期时间选择控件,网上提供的不多,且质量一般,所以只好自己做,参考了网上某位同学的作品Jave日期选择控件DateChooser 。
目前的代码将日期时间选择封装在一个按钮组件内,应该很好用,支持Application 、Applet
CODE:import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException ;
import java.awt.Color ;
import java.awt.Font;
import java.awt.Point ;
import java.awt.Dimension ;
import java.awt.BorderLayout ;
import java.awt.FlowLayout ;
import java.awt.GridLayout ;
import java.awt.Component ;
import java.awt.Cursor ;
import java.awt.Frame ;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSpinner ;
import javax.swing.JSpinner.NumberEditor ;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants ;
import javax.swing.event.ChangeListener ;
import javax.swing.event.ChangeEvent ;
import javax.swing.border.LineBorder ;
public class DateChooserJButton extends JButton {
private DateChooser dateChooser =null;
private String preLabel ="" ;
public DateChooserJButton() {
this(getNowDate()) ;
}
public DateChooserJButton(SimpleDateFormat df , String dateString) {
this() ;
setText(df,dateString) ;
}
public DateChooserJButton(Date date) {
this("",date);
}
public DateChooserJButton(String preLabel , Date date) {
if (preLabel!=null) this.preLabel = preLabel ;
setDate(date) ;
setBorder(null) ;
setCursor(new Cursor(Cursor.HAND_CURSOR)) ;
super.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dateChooser==null) dateChooser = new DateChooser();
Point p = getLocationOnScreen() ;
p.y = p.y+30 ;
dateChooser.showDateChooser(p) ;
}
}) ;
}
private static Date getNowDate() {
return Calendar.getInstance().getTime() ;
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy年MM月dd日HH时") ;
}
[1]
【责编:Peng】
--------------------next---------------------