博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swing应用开发实战系列之二:设计日期选择面板窗口
阅读量:4597 次
发布时间:2019-06-09

本文共 2116 字,大约阅读时间需要 7 分钟。

 

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

代码如下:

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:日期选择面板窗口
* Copyright: Copyright (c) 2015
* Company: 河南电力科学研究院智能电网所
* @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

主页:

空间:

转载于:https://www.cnblogs.com/shangbingbing/p/5036248.html

你可能感兴趣的文章
w3school HTML基础教程
查看>>
在centos下解决 “致命错误:curses.h:没有那个文件或目录”
查看>>
程序员必学的职场人际关系22原则
查看>>
css3的transition效果和transfor效果
查看>>
[置顶] 小白学习KM算法详细总结--附上模板题hdu2255
查看>>
dp 40题 转载
查看>>
组合数取模
查看>>
23种设计模式
查看>>
.Net学习笔记——细节问题
查看>>
mongodb副本集群搭建
查看>>
HDU 4505 小Q系列故事——电梯里的爱情
查看>>
synchronized同一把锁锁不同代码
查看>>
LA5135图论+ 割点性质运用
查看>>
329. Longest Increasing Path in a Matrix
查看>>
3.7 有符号二进制数
查看>>
Linux组件封装之一:MUtexLock
查看>>
android软件开发之webView.addJavascriptInterface循环渐进【一】
查看>>
第八次作业
查看>>
jQuery知识盲点
查看>>
shell-逐行读取文件
查看>>