import java.util.Scanner;/** * 实现日历的方法dayin * */public class printCalenderDemo { public static int year=Integer.MIN_VALUE; public static int month=Integer.MIN_VALUE; public static void main(String[] args) { /** * 1.提示用户输入打印时间 * 2.由用户输入的时间计算出 1900年1月1日到所要打印的日期的天数。 * 2.1年的总天数 * 2.2月的总天数 * 3.打印出月历的标题 * 4.打出月历的头 * 5.打印日历体 */// int sum =getSomeDaysOfYears();// sum+=getSomeDayOfMonth(); // sum++; //在上个月的最后一天 PrintCalender(); } public static void PrintCalender() { InputYearAndMonth(); int sum =getSomeDaysOfYears(); sum+=getSomeDayOfMonth(); sum++; System.out.println("第一天是星期"+sum%7); PrintMonthTitle(); PrintBody(); } private static void PrintMonthTitle() { String []monthNames={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; System.out.println(year+"\t"+monthNames[month-1]); System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天"); } public static void PrintBody() { int sum =getSomeDaysOfYears(); sum+=getSomeDayOfMonth(); sum++; int monthNumber=MonthNumber(); int blank=sum%7; if (blank==0) blank=; for (int i=1;iblank;i++) { System.out.print("\t"); } for(int i=1;i=monthNumber;i++) { System.out.print(i); if((i+blank-1)%7==0) System.out.println(); else System.out.print("\t"); } } /** * 获得1900到year的总天数 * 全局变量参数可以不用传 */ private static int getSomeDaysOfYears(){ if(year==Integer.MIN_VALUE) { System.out.println("输入错误,请重新输入:"); InputYearAndMonth(); } int sum=0; for(int i=1900;iyear;i++) { sum+=365; if(isLeapYear(i)) sum++; } return sum; } /** * * 求用户所输入非整年的天数 * 当年的一月到month-1的所有天数的和 * @return */ private static int getSomeDayOfMonth() { int sum=0; int[] dayOfMonth={31,28,31,30,31,30,31,31,30,31,30,31}; for(int i=0;imonth-1;i++) { sum+=dayOfMonth[i]; } if(isLeapYear(year)month=3) sum++; return sum; } public static int MonthNumber() { int[] dayOfMonth={31,28,31,30,31,30,31,31,30,31,30,31}; int monthNumber=dayOfMonth[month-1]; if(month==2isLeapYear(year)) monthNumber++; return monthNumber; } /** * 用于检验传入的年份是不是瑞年 * @param year * @return是瑞年则返回true */ private static boolean isLeapYear(int year){ return year%400==0||year%4==0year % 100 !=0; } private static void InputYearAndMonth() { Scanner input=new Scanner(System.in); System.out.println("请输入月份和月份:"); year=input.nextInt(); month=input.nextInt(); input.close(); input=null; }}
我们提供的服务有:网站建设、做网站、微信公众号开发、网站优化、网站认证、丰宁ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的丰宁网站制作公司
详细代码
//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();
}
}
源代码都知道了,那不叫编写,应该叫运行。
1.打开一个记事本(windows里面自带那个)
2.复制过去,保存为xxx.java(xxx是带main方法的那个类名)
3.到sun网站下载一个JDK安装包进行安装
4.打开一个cmd窗口,进入保存xxx.java的目录,打入编译命令javac xxx.java
编译完成后(源代码没问题就会编译通过),打入运行命令 java xxx.java
5.正常的话得到一个日历记事本窗口
--------
貌似这是java第一章讲的内容,我汗
日历记事本课程设计报告
java编写
/*
说明:主程序
*/
import
java.util.Calendar;
import
javax.swing.*;
import
java.awt.*;
import
java.awt.event.*;
import
java.io.*;
import
java.util.Hashtable;
public
class
CalendarPad
extends
JFrame
implements
MouseListener
{
int
year,month,day;
Hashtable
hashtable;
File
file;
JTextField
showDay[];
JLabel
title[];
Calendar
日历;
int
星期几;
NotePad
notepad=null;
Month
负责改变月;
Year
负责改变年;
String
星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JPanel
leftPanel,rightPanel;
public
CalendarPad(int
year,int
month,int
day)
{
leftPanel=new
JPanel();
//左边日历...........
JPanel
leftCenter=new
JPanel();
JPanel
leftNorth=new
JPanel();
leftCenter.setLayout(new
GridLayout(7,7));
rightPanel=new
JPanel();
this.year=year;
this.month=month;
this.day=day;
负责改变年=new
Year(this);
负责改变年.setYear(year);
负责改变月=new
Month(this);
负责改变月.setMonth(month);
title=new
JLabel[7];
showDay=new
JTextField[42];
for(int
j=0;j7;j++)
{
title[j]=new
JLabel();
title[j].setText(星期[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
leftCenter.add(title[j]);
}
........
看下是不是收到了啊
额外人提问