java写日历的代码,java写日历代码swing

java编写日历

我给你贴上我在java核心技术中看到的代码吧,当然没有输入年份和月份,是按照当前时间创建的,写有我写的注释,应该能看的懂

成都创新互联专注于网站建设,为客户提供网站设计制作、成都网站建设、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,品牌网站建设,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。

/*

* 2012年5月13日10:37:58

* 日历程序

* Function:

* 显示当前月份的日历

* 总结

* 1. 0-11分别代表1-12月

* 1-7分别代表周日-周六

* 2. 使用GregorianCalendar对象的get方法(参数)获取月,日,年等信息

* 3.

*/

import java.text.DateFormatSymbols;

import java.util.*;

public class CalendarTest {

public static void main(String[] args) {

//construct d as current date构造一个日期

GregorianCalendar d = new GregorianCalendar();

//获取今天是这个月的第几天

int today = d.get(Calendar.DAY_OF_MONTH); //Calendar.DAY_OF_MONTH作为参数调用,得到今天是这个月的第几天

int month = d.get(Calendar.MONTH); //月份

d.set(Calendar.DAY_OF_MONTH, 1); //设置d的日期是本月的1号

int weekDay = d.get(Calendar.DAY_OF_WEEK); //获取当天位于本星期的第几天,也就确定了星期几,值的范围是1-7

int firstDayOfWeek = d.getFirstDayOfWeek(); //获取一星期的第一天,我们得到的是Calendar.SUNDAY,因为我们一星期的第一天是周日

int indent = 0; //为了定位本月第一天,定义索引

while (weekDay != firstDayOfWeek) {

//注意,月份用0-11代表1-12月,为了清晰起见,使用常量代替,下面获取月份得到的实际是当前月-1的值,所以我们要加1

//System.out.printf("当前星期的第%d天,位于当月的第%d天, 现在是%d月\n",

// weekDay, d.get(Calendar.DAY_OF_MONTH), d.get(Calendar.MONTH)+1); //Test Code

indent++;//缩进个数+1

d.add(Calendar.DAY_OF_MONTH, -1);//当前天数-1,如果现在是1号,则执行本条代码后,时间变为上一个月最后一天

weekDay = d.get(Calendar.DAY_OF_WEEK); //重新获取当天位于本星期的第几天

}

String[] weekDayNames = new DateFormatSymbols().getShortWeekdays();//获取简短形式的星期字符串数组

//System.out.println(weekDayNames.length);getShortWeekdays()得到的数组的长度是8,下标为0的是没有值1为星期日...7为星期六

//注释代码1

//Java核心技术的代码

/*

do {

//System.out.printf("%4s", weekDayNames[weekDay]); //经过上面定义索引,weekDay代表的是本星期日

d.add(Calendar.DAY_OF_MONTH, 1); //天数加1

weekDay = d.get(Calendar.DAY_OF_WEEK); //重新获得weekDay的值

} while (weekDay != firstDayOfWeek); //当循环完一个星期后,这里判断不成立,退出循环

*/

//我写的代码,替换上面注释代码1

for (int i=1; iweekDayNames.length; i++)//打印星期标题

System.out.printf("%3s\t", weekDayNames[i]);//引号内是一个全角的空格,因为是中文版,不是书上英文环境,中文和空格对于不上,这里我们用\t解决

//System.out.printf("%3s ", weekDayNames[i]); //方式2

System.out.println();//换行

for (int i=1; i=indent; i++)//确定一星期的一天位置,利用上面indent

System.out.printf("\t");//如用方式2,则这里内容是四个全角空格

//实现输出日期

d.set(Calendar.MONTH, month);

d.set(Calendar.DAY_OF_MONTH, 1);

do {

//print day

int day = d.get(Calendar.DAY_OF_MONTH);

System.out.printf("%3d", day);

if (day == today)

System.out.print("*");

System.out.print("\t");

d.add(Calendar.DATE, 1);//天数加1

weekDay = d.get(Calendar.DAY_OF_WEEK);//刷新weekDay

if (weekDay == firstDayOfWeek) //如果这天等于星期天则换行

System.out.println();

} while (d.get(Calendar.MONTH) == month);

}

}

用java写的日历记事本代码?

详细代码

//CalendarWindow类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

public class CalendarWindow extends JFrame implements ActionListener,

MouseListener,FocusListener{

int year,month,day;

CalendarMessage calendarMessage;

CalendarPad calendarPad;

NotePad notePad;

JTextField showYear,showMonth;

JTextField [] showDay;

CalendarImage calendarImage;

Clock clock;

JButton nextYear,previousYear,nextMonth,previousMonth;

JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;

File dir;

Color backColor=Color.gray;

public CalendarWindow(){

dir=new File("./dailyRecord");

dir.mkdir();

showDay=new JTextField[42];

for(int i=0;ishowDay.length;i++){

showDay[i]=new JTextField();

showDay[i].setBackground(backColor);

showDay[i].setLayout(new GridLayout(3,3));

showDay[i].addMouseListener(this);

showDay[i].addFocusListener(this);

}

calendarMessage=new CalendarMessage();

calendarPad=new CalendarPad();

notePad=new NotePad();

Calendar calendar=Calendar.getInstance();

calendar.setTime(new Date());

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH)+1;

day=calendar.get(Calendar.DAY_OF_MONTH);

calendarMessage.setYear(year);

calendarMessage.setMonth(month);

calendarMessage.setDay(day);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.setShowDayTextField(showDay);

notePad.setShowMessage(year,month,day);

calendarPad.showMonthCalendar();

doMark(); //给有日志的号码做标记,见后面的doMark()方法

calendarImage=new CalendarImage();

calendarImage.setImageFile(new File("sea.jpg"));

clock=new Clock();

JSplitPane splitV1=

new JSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);

JSplitPane splitV2=

new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);

JSplitPane splitH=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2);

add(splitH,BorderLayout.CENTER);

showYear=new JTextField(""+year,6);

showYear.setFont(new Font("TimesRoman",Font.BOLD,12));

showYear.setHorizontalAlignment(JTextField.CENTER);

showMonth=new JTextField(" "+month,4);

showMonth.setFont(new Font("TimesRoman",Font.BOLD,12));

showMonth.setHorizontalAlignment(JTextField.CENTER);

nextYear=new JButton("下年");

previousYear=new JButton("上年");

nextMonth=new JButton("下月");

previousMonth=new JButton("上月");

nextYear.addActionListener(this);

previousYear.addActionListener(this);

nextMonth.addActionListener(this);

previousMonth.addActionListener(this);

showYear.addActionListener(this);

JPanel north=new JPanel();

north.add(previousYear);

north.add(showYear);

north.add(nextYear);

north.add(previousMonth);

north.add(showMonth);

north.add(nextMonth);

add(north,BorderLayout.NORTH);

saveDailyRecord=new JButton("保存日志") ;

deleteDailyRecord=new JButton("删除日志");

readDailyRecord=new JButton("读取日志");

saveDailyRecord.addActionListener(this);

deleteDailyRecord.addActionListener(this);

readDailyRecord.addActionListener(this);

JPanel pSouth=new JPanel();

pSouth.add(saveDailyRecord);

pSouth.add(deleteDailyRecord);

pSouth.add(readDailyRecord);

add(pSouth,BorderLayout.SOUTH);

setVisible(true);//根据参数 b 的值显示或隐藏此 Window

setBounds(60,60,660,480);

validate();//验证此容器及其所有子组件

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==nextYear){

year++;

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==previousYear){

year--;

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==nextMonth){

month++;

if(month12) month=1;

showMonth.setText(" "+month);

calendarMessage.setMonth(month);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==previousMonth){

month--;

if(month1) month=12;

showMonth.setText(" "+month);

calendarMessage.setMonth(month);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==showYear){

String s=showYear.getText().trim();

char a[]=s.toCharArray();

boolean boo=false;

for(int i=0;ia.length;i++)

if(!(Character.isDigit(a[i])))

boo=true;

if(boo==true) //弹出“警告”消息对话框

JOptionPane.showMessageDialog(this,"您输入了非法年份","警告", JOptionPane.WARNING_MESSAGE);

else if(boo==false)

year=Integer.parseInt(s);

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==saveDailyRecord){

notePad.save(dir,year,month,day);

doMark();

}

else if(e.getSource()==deleteDailyRecord){

notePad.delete(dir,year,month,day);

doMark();

}

else if(e.getSource()==readDailyRecord)

notePad.read(dir,year,month,day);

}

public void mousePressed(MouseEvent e){

JTextField text=(JTextField)e.getSource();

String str=text.getText().trim();

try{ day=Integer.parseInt(str);

}

catch(NumberFormatException exp){

}

calendarMessage.setDay(day);

notePad.setShowMessage(year,month,day);

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void focusGained(FocusEvent e){

Component com=(Component)e.getSource();

com.setBackground(Color.red);

}

public void focusLost(FocusEvent e){

Component com=(Component)e.getSource();

com.setBackground(backColor);

}

public void doMark(){

for(int i=0;ishowDay.length;i++){

showDay[i].removeAll();

String str=showDay[i].getText().trim();

try{

int n=Integer.parseInt(str);

if(isHaveDailyRecord(n)==true){ //见后面的isHaveDailyRecord()方法

JLabel mess=new JLabel("存");

mess.setFont(new Font("TimesRoman",Font.PLAIN,11));

mess.setForeground(Color.black) ;

showDay[i].add(mess);

} }

catch(Exception exp){}

}

calendarPad.repaint();

calendarPad.validate();

}

public boolean isHaveDailyRecord(int n){

String key=""+year+""+month+""+n;

String [] dayFile=dir.list();

boolean boo=false;

for(int k=0;kdayFile.length;k++){

if(dayFile[k].equals(key+".txt")){

boo=true;

break;

} }

return boo;

}

public static void main(String args[]){

new CalendarWindow();

}

}

//CalendarPad类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class CalendarPad extends JPanel{

int year,month,day;

CalendarMessage calendarMessage;

JTextField [] showDay;

JLabel title[];

String [] 星期={"SUN/日","MON/一","TUE/二","WED/三","THU/四","FRI/五","SAT/六"};

JPanel north,center;

public CalendarPad(){

setLayout(new BorderLayout());

north=new JPanel();

north.setLayout(new GridLayout(1,7));

center=new JPanel();

center.setLayout(new GridLayout(6,7));

add(center,BorderLayout.CENTER);

add(north,BorderLayout.NORTH);

title=new JLabel[7];

for(int j=0;j7;j++){

title[j]=new JLabel();

title[j].setFont(new Font("TimesRoman",Font.BOLD,12));

title[j].setText(星期[j]);

title[j].setHorizontalAlignment(JLabel.CENTER);

title[j].setBorder(BorderFactory.createRaisedBevelBorder());

north.add(title[j]);

}

title[0].setForeground(Color.red);

title[6].setForeground(Color.blue);

}

public void setShowDayTextField(JTextField [] text){

showDay=text;

for(int i=0;ishowDay.length;i++){

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,15));

showDay[i].setHorizontalAlignment(JTextField.CENTER);

showDay[i].setEditable(false);

center.add(showDay[i]);

}

}

public void setCalendarMessage(CalendarMessage calendarMessage){

this.calendarMessage=calendarMessage;

}

public void showMonthCalendar(){

String [] a=calendarMessage.getMonthCalendar();

for(int i=0;i42;i++)

showDay[i].setText(a[i]);

validate();

}

}

用JAVA做日历,要求源代码

import java.util.Scanner; public class Wan{ public static void main(String[] args){ Scanner name = new Scanner(System.in); System.out.print("请输入要查询的年份:"); int year = name.nextInt(); System.out.print("请输入该年的月份"); int month = name.nextInt(); } //累加 该年至输入的月份 天数 //比如 输入2009年的 3月分 // 那就累加 2009年的1月至 3月1号的总天数 public void sumDay(int year,int month){ int day = 0; int sumDay = 0; for(int i = 1;i=month;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: if(year % 4 == 0 || year % 400 == 0 year %100!=0){ day = 29; }else{ day = 28; } break; default: day = 30; } //最后一个月份不要累加 因为我们只是要算到该月的一号就可以了 if(i month){ sumDay += day; } } //累加 2000年到该年的一月一号天数 for(int i = 2000;iyear;i++){ if( i % 4 == 0 || i %400== 0 i % 100 != 0){ sumDay += 366; }else{ sumDay += 365; } } //求该月一号为星期几 int week = sumDay % 7 +1; if(week == 7){ week = 0; } } public void fomatDate(int week,int day){ int g = 0; for(int i = 0;iweek;i++){ System.out.print("\t"); } for(int i = 1;i=day;i++){ System.out.print(i+"\t"); g = week + i; if(g % 7 == 0){ System.out.println(); } } } } 给点分哈 写得好累


当前名称:java写日历的代码,java写日历代码swing
文章出自:http://ybzwz.com/article/dsggspg.html