Java实现的日历功能完整示例
作者:c_jian 发布时间:2021-10-12 18:30:21
标签:Java,日历
本文实例讲述了Java实现的日历功能。分享给大家供大家参考,具体如下:
应用名称:Java日历
用到的知识:Java GUI编程,日期操作
开发环境:win8+eclipse+jdk1.8
功能说明:一个很简单的万年历,可以选择年份和月份,也可以用按钮翻页,日历会实时更新日期,最下方会显示当前操作系统的时间。
效果图:
源代码:
CalendarFrame.java
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
public class CalendarFrame extends JFrame implements ActionListener{
/**
* @author Nut
* 2016.01.13
*/
private static final long serialVersionUID = -7260798316896145633L;
JLabel labelDay[] = new JLabel[42];
JButton titleName[] = new JButton[7];
String name[]={"日","一","二","三","四","五","六"};
JButton nextMonth,previousMonth;
JComboBox choiceYear,choiceMonth;
Calendarbean calendar;
JLabel showYear,showMonth;
JLabel showmessage=new JLabel("",JLabel.CENTER);
int year = 2011,month=2;
//构造方法初始化界面
public CalendarFrame(){
JPanel pCenter = new JPanel();
pCenter.setLayout(new GridLayout(7,7));
//星期栏
for(int i=0;i<7;i++){
titleName[i]=new JButton(name[i]);
titleName[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED));
pCenter.add(titleName[i]);
}
//日期栏
for(int i=0;i<42;i++){
labelDay[i]=new JLabel("",JLabel.CENTER);
labelDay[i].setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
pCenter.add(labelDay[i]);
}
//年月选择栏
choiceYear=new JComboBox();
choiceMonth=new JComboBox();
showYear=new JLabel("年");
showMonth=new JLabel("月 ");
for(int i=1990;i<2050;i++)
choiceYear.addItem(i);
choiceYear.addActionListener(this);
for(int i=1;i<=12;i++)
choiceMonth.addItem(i);
choiceMonth.addActionListener(this);
calendar=new Calendarbean();
nextMonth=new JButton("下月");
previousMonth=new JButton("上月");
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
JPanel pNorth=new JPanel(),
pSouth=new JPanel();
pNorth.add(choiceYear);
pNorth.add(showYear);
pNorth.add(choiceMonth);
pNorth.add(showMonth);
pNorth.add(previousMonth);
pNorth.add (nextMonth);
pSouth.add(showmessage);
add(pCenter,BorderLayout.CENTER);
add(pNorth,BorderLayout.NORTH);
add(pSouth,BorderLayout.SOUTH);
setYearAndMonth(year,month);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void setYearAndMonth(int y,int m){
calendar.setYear(y);
calendar.setMonth(m);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
labelDay[i].setText(day[i]);
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 EEEE");//设置日期格式
showmessage.setText("系统时间:"+df.format(new Date()));
}
//事件动作
public void actionPerformed(ActionEvent e){
if(e.getSource()==nextMonth){
month=month +1;
if(month>12)
month=1;
calendar.setMonth(month);
choiceMonth.setSelectedItem(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++){
labelDay[i].setText(day[i]);
}
}
else if(e.getSource()==previousMonth){
month=month-1;
if(month<1)
month=12;
calendar.setMonth(month);
choiceMonth.setSelectedItem(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++){
labelDay[i].setText(day[i]);
}
}
//选择年份
else if (e.getSource()==choiceYear){
calendar.setYear((Integer) choiceYear.getSelectedItem());
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++){
labelDay[i].setText(day[i]);
}
}
//选择月份
else if (e.getSource()==choiceMonth){
calendar.setMonth((Integer) choiceMonth.getSelectedItem());
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++){
labelDay[i].setText(day[i]);
}
}
// showmessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月");
}
}
Calendarbean.java
import java.util.Calendar;
public class Calendarbean {
String day[];
int year = 2005,month=0;
public void setYear(int year){
this.year=year;
}
public int getYear(){
return year;
}
public void setMonth(int month){
this.month=month;
}
public int getMonth(){
return month;
}
public String[] getCalendar(){
String a[]=new String[42];
Calendar 日历=Calendar.getInstance();
日历.set(year,month-1,1);
int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
if(month==4||month==6||month==9||month==11)
day=30;
if(month==2){
if(((year%4==0)&&(year%100!=0))||(year%400==0))
day=29;
else
day=28;
}
for(int i=星期几,n=1;i<星期几+day;i++){
a[i]=String.valueOf(n);
n++;
}
return a;
}
}
CalendarMainClass.java
public class CalendarMainClass{
public static void main(String args[])
{
CalendarFrame frame = new CalendarFrame();
frame.setBounds(100,100,360,300);
frame.setTitle("Java日历");
frame.setVisible(true);
frame.setYearAndMonth(1990,1);//设置日历初始值为1990年1月
}
}
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/C_jian/article/details/50513386
0
投稿
猜你喜欢
- 如下所示:public static void main(String[] args) throws IOException {  
- 如下所示://定义二维数组写法1 class numthree{public static void main(String[] args)
- 起因最近在写CRUD的时候,发现有个分页的VO写的健壮性比较差,一时手痒改了一下,没想到改了之后好几个功能都出现了问题。原VO关键代码如下:
- 线程池主要解决两个问题:一是当执行大量异步任务时线程池能够提供较好的性能。在不使用线程池时,每当需要执行异步任务时直接new一个线程来运行,
- import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pi
- 一、前言最近写了个项目,前端还没写,需要部署到服务器给女朋友实现前端,可是不熟悉Linux的我,蹑手蹑脚,真的是每一步都是bug,可谓是步步
- eclipse运行tomcat提示端口被占用今天碰到一个问题,在eclipse中运行一个程序,eclipse里面的tomcat一启动就报错说
- Struts2Struts2是在WebWork2基础发展而来的。和struts1一样, Struts2也属于MVC框架。不过有一点
- 本文实例为大家分享了C#实现单词本功能的具体代码,供大家参考,具体内容如下看到网上有类似的教程视频实现单词本,于是自己敲了一个实现单词本功能
- 今天学习到sql中的ResultSet,用到了获取总函数,网上百度说是使用getRow()方法,但是一值返回0.后台调试才发现getRow(
- mybatis 查询返回Map<String,Object> 类型,平时没太注意怎么用,今天又遇到了总结记录一下,方便以后处理此
- 本文实例为大家分享了Java开发实现人机猜拳游戏的具体代码,供大家参考,具体内容如下猜拳游戏游戏规则:人和电分别出剪刀、石头、布,直到人战胜
- 启动Springboot项目时候报错java: 无法访问org.springframework.boot.SpringApplication
- 快速排序过程没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”!光听这个名字是不是就觉得很高端呢。假设我们现在对“52 39 67
- 项目描述: springboot+springcloud+zookeeper+eureka+maven;为多模块多module的分布式架构;
- /// <summary> /// 遍历Co
- 将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出D
- 题外话:学习.NET已经有一年了,从C#->ASP.NET->WPF。主要以看电子书为主,比较少写代码。现在回头学习以前接触过的
- 1、注意事项众所周知,对一个文件进行读写操作时,我们需要创建对应的输入流和输出流但需要注意的是,读写操作不能同时进行(边读边写),即不能同时
- 本文实例为大家分享了java实现双色球抽奖的具体代码,供大家参考,具体内容如下实现双色球先考虑整体思路:1.随机生成7位数的数组为大奖号码(