java GUI 日期时间选择工具
这是一个用于java图形化界面的日期时间选择工具,仅适用于学习使用,不适合实际开发。
里面有些内容没用,但是没有删除,可以根据自己需求拓展
1.可以只展示日期,可以只展示时间,可以同时显示
2.TimeChangeUtils类是一个JLabel标签,直接展示时间
3.MyDate类是日期相关类,MyTime是时间相关类
效果图
TimeChangeUtils
点击点击设置时间
点击确认
只有日期模式
只有时间模式
直接上代码
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeChangeUtils extends JLabel{
//年月日时分秒的下拉列表
private JComboBox<Integer> yearBox,monthBox,dayBox,hourBox,minuteBox,secondBox;
//年月日时分秒标签
private JLabel yearJLabel,monthJLabel,dayJLabel,hourJLabel,minuteJLabel,secondJLabel;
//操作按钮
JButton querenButton,quxiaoButton;
//时间字符串
private String returnString;
//显示容器
private JDialog jDialog;
//获取当前时间
private MyDate nowDate;
private Date returnDate;
//默认时间构造方式
private String string="yyyy-MM-dd HH:mm:ss";
private MyDate date;
private MyTime time;
public static final int JUST_DATE=0,JUST_TIME=1,ALL=2;
public TimeChangeUtils(int type) {
super();
news(type);
}
private void news(int type) {
jDialog=new JDialog();
jDialog.setBounds(this.getX(), this.getY(), 250, 150);
switch (type) {
case JUST_DATE:{
jDialog.setLayout(new GridLayout(2,1));
date=new MyDate();
jDialog.add(date);
break;
}
case JUST_TIME:{
jDialog.setLayout(new GridLayout(2,1));
time=new MyTime();
jDialog.add(time);
break;
}
case ALL:{
jDialog.setLayout(new GridLayout(3,1));
date=new MyDate();
time=new MyTime();
jDialog.add(date);
jDialog.add(time);
break;
}
default:{
System.out.println("类型错误!");
break;
}
}
JPanel j=new JPanel();
j.setLayout(new GridLayout(1,2));
querenButton=new JButton("确认");
quxiaoButton=new JButton("取消");
j.add(quxiaoButton);
j.add(querenButton);
jDialog.add(j);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
jDialog.setVisible(true);
}
});
querenButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setText(getDateTime());
jDialog.setVisible(false);
}
});
quxiaoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jDialog.setVisible(false