首先引用com.qt.datapicker.DatePicker;包实现如下:
package Date;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import com.qt.datapicker.DatePicker;
public class TestDate extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public void launch() {
this.setBounds(190,190,300,300);
this.setLayout(null);
final ObservingTextField jtf = new ObservingTextField();
jtf.setEditable(false);
JButton btn = new JButton("日历");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
DatePicker dp = new DatePicker(jtf,Locale.CHINESE);
Date selectedDate = dp.parseDate(jtf.getText());
dp.setSelectedDate(selectedDate);
dp.start(jtf);
}
});
jtf.setBounds(50,130,110,23);
btn.setBounds(160,130,60,23);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.add(jtf);
this.add(btn);
this.setVisible(true);
}
public static void main(String[] args) {
TestDate td = new TestDate();
td.launch();
}
}
class ObservingTextField extends JTextField implements Observer {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void update(Observable o, java.lang.Object arg) {
// TODO Auto-generated method stub
Calendar calendar = (Calendar) arg;
DatePicker dp = (DatePicker) o;
setText(dp.formatDate(calendar));
}
}
效果图如下:
然后介绍用com.eltima.components.ui.DatePicker包的实现方法:
package Date;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import com.eltima.components.ui.DatePicker;
public class TestDate extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public void launch() {
this.setBounds(200,200,200,200);
this.setLayout(null);
DatePicker dp = new DatePicker(this,new Date());
dp.setEditorable(false);
dp.setLocale(Locale.CHINA);
dp.setTextAlign(DatePicker.CENTER);
dp.setBounds(50,50,100,40);
this.add(dp);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new TestDate().launch();
}
}
效果图如下: