Swing应用开发实战系列之二:设计日期选择面板窗口

Swing本身没有提供什么华丽丽的日期时间选择控件,所以笔者就在网上搜了个第三方的jar包jdatepicker-1.3.2.jar,基于此设计了个很轻量的日期选择面板,很简单的。效果图如下所示:

Swing应用开发实战系列之二:设计日期选择面板窗口

代码如下:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.JDatePanel;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
/**
* Description:日期选择面板窗口<br>
* Copyright: Copyright (c) 2015<br>
* Company: 河南电力科学研究院智能电网所<br>
* @author shangbingbing 2015-01-01编写
* @version 1.0
*/
public class DialogDatePicker extends JDialog {
private static final long serialVersionUID = 1L;
/**
* 弹出日期选择窗口
* @param modal 是否是模态窗口
* @param txtSelectedDate 日期内容接收文本框
* @param screenX 显示X点坐标
* @param screenY 显示Y点坐标
*/
public DialogDatePicker(boolean modal, final JTextField txtSelectedDate,int screenX,int screenY) {
final JDatePanel jp = JDateComponentFactory.createJDatePanel(new UtilDateModel(new Date()));
jp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(txtSelectedDate != null) {
try{
txtSelectedDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(jp.getModel().getValue()));
} catch (Exception ex) {
txtSelectedDate.setText("");
}
}
}
});
JPanel pnl = (JPanel)jp;
this.add(pnl);
this.setTitle("选择日期");
this.setResizable(false);
this.setModal(modal);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setBounds(screenX, screenY, 300, 300);
this.setVisible(true);
} public static void main(String[] args) {
JTextField txtDate = new JTextField();
new DialogDatePicker(true,txtDate,300,400);
System.out.println(txtDate.getText());
}
}

【完】

作者:商兵兵

单位:河南省电力科学研究院智能电网所

QQ:52190634

主页:http://www.cnblogs.com/shangbingbing

空间:http://shangbingbing.qzone.qq.com

上一篇:vscode git连接github


下一篇:20145307陈俊达《网络对抗》Exp5 MSF基础应用