【Java GUI 日期时间选择工具】

这篇博客介绍了如何在Java GUI中创建一个日期时间选择工具,适用于学习用途。该工具可展示日期、时间或两者,并提供了TimeChangeUtils类用于JLabel时间显示,以及MyDate和MyTime类进行日期和时间操作。文章包含了不同展示模式的效果图和相关代码示例。
摘要由CSDN通过智能技术生成

java GUI 日期时间选择工具

这是一个用于java图形化界面的日期时间选择工具,仅适用于学习使用,不适合实际开发。

里面有些内容没用,但是没有删除,可以根据自己需求拓展
1.可以只展示日期,可以只展示时间,可以同时显示
2.TimeChangeUtils类是一个JLabel标签,直接展示时间
3.MyDate类是日期相关类,MyTime是时间相关类

效果图

TimeChangeUtils
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
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值