/*
*
*2009年9月25日
* 今天终于把它写完了,花了我三个晚上的时间 。感觉很充实,从中学到了很多东西;在实践中获取经验是很宝贵的。
* 要写好一个程序,不是一件容易的事,我感觉我这个程序只能说是他能运行,很乱就好像是拼凑出来的一样。所以如果
* 对他进行扩展,我想这是一件不可能的事。(所以在以后的练习中,要更加注重类和函数的设计。在此之前是从未感受到的)
*
* 功能:
* 根据输入的日期,判断是星期几;打印一个月或一整年的日历;
* 适用在2009.1.1以后的日期,最大限是一万年
*
*/
import java.util.*;
public class PerpetualCalendar
{
static int NowYear = 0;//定义变量:表输入的日期;
static int NowMonth = 0;
static int NowDay = 0;
static boolean leapYear = true;//逻辑变量为真表明此年为闰年;
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
String StringOne = new String(" ");
String StringYes = new String("yes");
String StringNo = new String("no"); human_computer();//完成交换信息,并对不合法的数据进行处理;记录输入的日期; DateClass DateClassOne = new DateClass(NowYear, NowMonth, NowDay);//此类完成输入日期是星期几的操作; System.out.println("\n*********************");
System.out.println("你输入的日期为:公元" + NowYear + "年" + NowMonth + "月" + NowDay + "日星期" + date(DateClassOne.getConterTotalOfWeek()));//提示用户所输入的信息和输出返回的星期数;
System.out.println("\n*********************");
System.out.println("\n 日 一 二 三 四 五 六");//输出格式; PrintCalendar();//调用函数打印该月的日历;
System.out.println("\n*********************\n");//打印整年;
System.out.println("要打印整年日历吗? 如果要请输入:\" yes\"! 否则请输入: \"no\"!\n\n");//合法检测,用户选择 yes打印, no不打印;
do
{ System.out.print("请做出你的选择:");
reader.hasNextLine(); StringOne = reader.nextLine();//用户输入
if (StringYes.equals(StringOne))
{ System.out.println("\n*********************\n");
PrintMonth();//选择输入,调用函数打印一年
break; } else if (StringNo.equals(StringOne))
{
System.out.println("\n本程序到此结束!");//选择不输入;
break;
}
else
{
System.out.println("\n你输入的数据不正确!");//输入了 不合法的数据;
System.out.println("\n请你再输入一次!"); }
}
while (!(StringYes.equals(StringOne) || StringNo.equals(StringOne)));//用户输入检测; } static char date(int IsDay)//通过返回的星期数IsDay,转化输出: 一,二,三, 四, 五,六,日;
{
char c = '错'; switch (IsDay)
{ case 1: c = '一'; break;
case 2: c = '二'; break;
case 3: c = '三'; break;
case 4: c = '四'; break;
case 5: c = '五'; break;
case 6: c = '六'; break;
case 0: c = '日'; break;
}
return c;
} static String dateTwo(int IsWeek)//十二月输出格式;
{
String c = new String("错"); switch (IsWeek)
{ case 1: c = "一"; break;
case 2: c = "二"; break;
case 3: c = "三"; break;
case 4: c = "四"; break;
case 5: c = "五"; break;
case 6: c = "六"; break;
case 7: c = "七"; break;
case 8: c = "八"; break;
case 9: c = "九"; break;
case 10: c = "十"; break;
case 11: c = "十一"; break;
case 12: c = "十二"; break; }
return c;
} static void human_computer()//人机交换信息
{
Scanner reader = new Scanner(System.in);
int TestYear = 0;
int TestMonth = 0;
int TestDay = 0;
int ConterDay = 0;
boolean Istrue = true;
boolean LeapYear = true; System.out.println(" 万年历 ");
System.out.println("请输入要查询的日期!"); do
{
System.out.print("输入年份:");
reader.hasNextInt();
TestYear = reader.nextInt();
Istrue = true;
if ((TestYear > 10000) || (TestYear < 2009))//判断有效年数;
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowYear = TestYear; do
{
System.out.print("\n输入月份:");//判断有效月数;
reader.hasNextInt();
TestMonth = reader.nextInt();
Istrue = true;
if ((TestMonth > 12) || (TestMonth <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowMonth = TestMonth;
//判断该月的天数 if (TestYear % 400 == 0 || TestYear % 4 == 0 && TestYear % 100 != 0)//判断闰年
LeapYear = true;
else LeapYear = false; do //对于号数的判断?
{
System.out.print("\n输入号数:");
reader.hasNextInt();
TestDay = reader.nextInt();
Istrue = true;
if (TestMonth == 1 || TestMonth == 3 || TestMonth == 5 || TestMonth == 7 || TestMonth == 8 || TestMonth == 10 || TestMonth == 12)//天数判断; ConterDay = 31; else if (TestMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; if ((TestDay > ConterDay) || (TestDay <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowDay = TestDay; } static void PrintCalendar()//打印 一个月;
{
DateClass DateClassThree = new DateClass(NowYear, NowMonth, 1);//创建对象DateClassTree
boolean LeapYear = true;
int ConterDay = 0;
int Week = DateClassThree.getConterTotalOfWeek();//通过对象DateClassTree调用函数,获得该年该月一号的 星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)
LeapYear = true;
else LeapYear = false; if (NowMonth == 1 || NowMonth == 3 || NowMonth == 5 || NowMonth == 7 || NowMonth == 8 || NowMonth == 10 || NowMonth == 12)//判断该月的天数; ConterDay = 31; else if (NowMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (Week * 4); p++)//该月第一天的位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//该月总天数循环
{
if (d < 10)
System.out.print(" " + d);//格式控制
else
System.out.print(" " + d); if ((Week + d) % 7 == 0)//跳转打印
System.out.print("\n");
} } static void PrintMonth()//打印 1--12个月;
{
DateClass DateClassTwo = new DateClass(NowYear, 1, 1);//该年一月一日; boolean LeapYear = true;
int ConterDay = 0;
int EveryOfWeek = DateClassTwo.getConterTotalOfWeek();//获得一月一日星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)//闰年判断
LeapYear = true;
else LeapYear = false;
for (int m = 1; m <= 12; m++)
{
System.out.println("公元" + NowYear + "年" + dateTwo(m) + "月");// 月的输入格式;
System.out.println("\n 日 一 二 三 四 五 六");
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)//整年循环; ConterDay = 31; else if (m == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (EveryOfWeek * 4); p++)//每月的第一天打印位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//当月循环;
{
if (d < 10)
System.out.print(" " + d);
else
System.out.print(" " + d); if ((EveryOfWeek + d) % 7 == 0)
System.out.print("\n");
}
System.out.println("\n*********************\n");
EveryOfWeek = (ConterDay + EveryOfWeek) % 7;//判断下月第一天是星期几; } }
}class DateClass//主要用来返回星期;
{
int Year;
int Month;
int Day;
int ConterWeek;
DateClass(int y, int m, int d)//构造函数;
{
Year = y;
Month = m;
Day = d;
} boolean getLeap(int Year)//闰年
{
boolean LeapYear = true;
if (Year % 400 == 0 || Year % 4 == 0 && Year % 100 != 0)
return LeapYear;
else return LeapYear = false;
} int getConterTotalOfWeek()//返回星期
{
int ChangYear = Year;
long ConterTotal = 0;
long ConterYear = 0;
int ConterMonth = 0; for (; ChangYear > 2009; ChangYear--)//整年天数的累计 if (getLeap(ChangYear))
ConterYear += 366;
else
ConterYear += 365;
ConterMonth = getConterMonth(Month - 1);//调用返回月累计; ConterTotal = ConterYear + ConterMonth + (Day - 1);//总计天数; return ConterWeek = (int)((ConterTotal + 4) % 7);//返回;
} int getConterMonth(int m)//月累计;
{
int ConterMonth = 0;
switch (m)
{
case 11: ConterMonth += 30;
case 10: ConterMonth += 31;
case 9: ConterMonth += 30;
case 8: ConterMonth += 31;
case 7: ConterMonth += 31;
case 6: ConterMonth += 30;
case 5: ConterMonth += 31;
case 4: ConterMonth += 30;
case 3: ConterMonth += 31;
case 2: ConterMonth += (getLeap(Year) ? 29 : 28);
case 1: ConterMonth += 31;
case 0: ;
}
return ConterMonth; }
}
//到此大功告成!我看了一下大概有300多行!嘿嘿嘿!!!
*
*2009年9月25日
* 今天终于把它写完了,花了我三个晚上的时间 。感觉很充实,从中学到了很多东西;在实践中获取经验是很宝贵的。
* 要写好一个程序,不是一件容易的事,我感觉我这个程序只能说是他能运行,很乱就好像是拼凑出来的一样。所以如果
* 对他进行扩展,我想这是一件不可能的事。(所以在以后的练习中,要更加注重类和函数的设计。在此之前是从未感受到的)
*
* 功能:
* 根据输入的日期,判断是星期几;打印一个月或一整年的日历;
* 适用在2009.1.1以后的日期,最大限是一万年
*
*/
import java.util.*;
public class PerpetualCalendar
{
static int NowYear = 0;//定义变量:表输入的日期;
static int NowMonth = 0;
static int NowDay = 0;
static boolean leapYear = true;//逻辑变量为真表明此年为闰年;
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
String StringOne = new String(" ");
String StringYes = new String("yes");
String StringNo = new String("no"); human_computer();//完成交换信息,并对不合法的数据进行处理;记录输入的日期; DateClass DateClassOne = new DateClass(NowYear, NowMonth, NowDay);//此类完成输入日期是星期几的操作; System.out.println("\n*********************");
System.out.println("你输入的日期为:公元" + NowYear + "年" + NowMonth + "月" + NowDay + "日星期" + date(DateClassOne.getConterTotalOfWeek()));//提示用户所输入的信息和输出返回的星期数;
System.out.println("\n*********************");
System.out.println("\n 日 一 二 三 四 五 六");//输出格式; PrintCalendar();//调用函数打印该月的日历;
System.out.println("\n*********************\n");//打印整年;
System.out.println("要打印整年日历吗? 如果要请输入:\" yes\"! 否则请输入: \"no\"!\n\n");//合法检测,用户选择 yes打印, no不打印;
do
{ System.out.print("请做出你的选择:");
reader.hasNextLine(); StringOne = reader.nextLine();//用户输入
if (StringYes.equals(StringOne))
{ System.out.println("\n*********************\n");
PrintMonth();//选择输入,调用函数打印一年
break; } else if (StringNo.equals(StringOne))
{
System.out.println("\n本程序到此结束!");//选择不输入;
break;
}
else
{
System.out.println("\n你输入的数据不正确!");//输入了 不合法的数据;
System.out.println("\n请你再输入一次!"); }
}
while (!(StringYes.equals(StringOne) || StringNo.equals(StringOne)));//用户输入检测; } static char date(int IsDay)//通过返回的星期数IsDay,转化输出: 一,二,三, 四, 五,六,日;
{
char c = '错'; switch (IsDay)
{ case 1: c = '一'; break;
case 2: c = '二'; break;
case 3: c = '三'; break;
case 4: c = '四'; break;
case 5: c = '五'; break;
case 6: c = '六'; break;
case 0: c = '日'; break;
}
return c;
} static String dateTwo(int IsWeek)//十二月输出格式;
{
String c = new String("错"); switch (IsWeek)
{ case 1: c = "一"; break;
case 2: c = "二"; break;
case 3: c = "三"; break;
case 4: c = "四"; break;
case 5: c = "五"; break;
case 6: c = "六"; break;
case 7: c = "七"; break;
case 8: c = "八"; break;
case 9: c = "九"; break;
case 10: c = "十"; break;
case 11: c = "十一"; break;
case 12: c = "十二"; break; }
return c;
} static void human_computer()//人机交换信息
{
Scanner reader = new Scanner(System.in);
int TestYear = 0;
int TestMonth = 0;
int TestDay = 0;
int ConterDay = 0;
boolean Istrue = true;
boolean LeapYear = true; System.out.println(" 万年历 ");
System.out.println("请输入要查询的日期!"); do
{
System.out.print("输入年份:");
reader.hasNextInt();
TestYear = reader.nextInt();
Istrue = true;
if ((TestYear > 10000) || (TestYear < 2009))//判断有效年数;
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowYear = TestYear; do
{
System.out.print("\n输入月份:");//判断有效月数;
reader.hasNextInt();
TestMonth = reader.nextInt();
Istrue = true;
if ((TestMonth > 12) || (TestMonth <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowMonth = TestMonth;
//判断该月的天数 if (TestYear % 400 == 0 || TestYear % 4 == 0 && TestYear % 100 != 0)//判断闰年
LeapYear = true;
else LeapYear = false; do //对于号数的判断?
{
System.out.print("\n输入号数:");
reader.hasNextInt();
TestDay = reader.nextInt();
Istrue = true;
if (TestMonth == 1 || TestMonth == 3 || TestMonth == 5 || TestMonth == 7 || TestMonth == 8 || TestMonth == 10 || TestMonth == 12)//天数判断; ConterDay = 31; else if (TestMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; if ((TestDay > ConterDay) || (TestDay <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowDay = TestDay; } static void PrintCalendar()//打印 一个月;
{
DateClass DateClassThree = new DateClass(NowYear, NowMonth, 1);//创建对象DateClassTree
boolean LeapYear = true;
int ConterDay = 0;
int Week = DateClassThree.getConterTotalOfWeek();//通过对象DateClassTree调用函数,获得该年该月一号的 星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)
LeapYear = true;
else LeapYear = false; if (NowMonth == 1 || NowMonth == 3 || NowMonth == 5 || NowMonth == 7 || NowMonth == 8 || NowMonth == 10 || NowMonth == 12)//判断该月的天数; ConterDay = 31; else if (NowMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (Week * 4); p++)//该月第一天的位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//该月总天数循环
{
if (d < 10)
System.out.print(" " + d);//格式控制
else
System.out.print(" " + d); if ((Week + d) % 7 == 0)//跳转打印
System.out.print("\n");
} } static void PrintMonth()//打印 1--12个月;
{
DateClass DateClassTwo = new DateClass(NowYear, 1, 1);//该年一月一日; boolean LeapYear = true;
int ConterDay = 0;
int EveryOfWeek = DateClassTwo.getConterTotalOfWeek();//获得一月一日星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)//闰年判断
LeapYear = true;
else LeapYear = false;
for (int m = 1; m <= 12; m++)
{
System.out.println("公元" + NowYear + "年" + dateTwo(m) + "月");// 月的输入格式;
System.out.println("\n 日 一 二 三 四 五 六");
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)//整年循环; ConterDay = 31; else if (m == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (EveryOfWeek * 4); p++)//每月的第一天打印位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//当月循环;
{
if (d < 10)
System.out.print(" " + d);
else
System.out.print(" " + d); if ((EveryOfWeek + d) % 7 == 0)
System.out.print("\n");
}
System.out.println("\n*********************\n");
EveryOfWeek = (ConterDay + EveryOfWeek) % 7;//判断下月第一天是星期几; } }
}class DateClass//主要用来返回星期;
{
int Year;
int Month;
int Day;
int ConterWeek;
DateClass(int y, int m, int d)//构造函数;
{
Year = y;
Month = m;
Day = d;
} boolean getLeap(int Year)//闰年
{
boolean LeapYear = true;
if (Year % 400 == 0 || Year % 4 == 0 && Year % 100 != 0)
return LeapYear;
else return LeapYear = false;
} int getConterTotalOfWeek()//返回星期
{
int ChangYear = Year;
long ConterTotal = 0;
long ConterYear = 0;
int ConterMonth = 0; for (; ChangYear > 2009; ChangYear--)//整年天数的累计 if (getLeap(ChangYear))
ConterYear += 366;
else
ConterYear += 365;
ConterMonth = getConterMonth(Month - 1);//调用返回月累计; ConterTotal = ConterYear + ConterMonth + (Day - 1);//总计天数; return ConterWeek = (int)((ConterTotal + 4) % 7);//返回;
} int getConterMonth(int m)//月累计;
{
int ConterMonth = 0;
switch (m)
{
case 11: ConterMonth += 30;
case 10: ConterMonth += 31;
case 9: ConterMonth += 30;
case 8: ConterMonth += 31;
case 7: ConterMonth += 31;
case 6: ConterMonth += 30;
case 5: ConterMonth += 31;
case 4: ConterMonth += 30;
case 3: ConterMonth += 31;
case 2: ConterMonth += (getLeap(Year) ? 29 : 28);
case 1: ConterMonth += 31;
case 0: ;
}
return ConterMonth; }
}
//到此大功告成!我看了一下大概有300多行!嘿嘿嘿!!!
当年,我刚学C++的时候,貌似第一个程序也是日历
嘿嘿,lz继续努力!
public static void main(String[] args){
System.out.println("*****************欢迎使用万年历****************************");
Scanner input=new Scanner(System.in);
System.out.print("输入年份:");
int nian= input.nextInt();
System.out.print("输入月份: ");
int yue = input.nextInt();
int day=0;
int tianShu=0;
for(;;){ //计算1900年之今天数
if ((nian%4==0&&nian%100!=0||nian%400==0)&&(yue==2)){
day=29;
System.out.println(nian+"润年"+yue+"月有"+day+"天");
}else if(!(nian%4==0&&nian%100!=0||nian%400==0)&&(yue==2)){
day=28;
System.out.println(nian+"年"+yue+"月有"+day+"天");
}else if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12){
day=31;
System.out.println(nian+"年"+yue+"月有"+day+"天");
}else if(yue==4||yue==6||yue==9||yue==11){
day=30;
System.out.println(nian+"年"+yue+"月有"+day+"天");
}else{
System.out.println("输入的月份不正确");
}
break;
}
int benYueTian=day;
for(int a=1900;a<nian;a++)
if(a%4==0&&a%100!=0||a%400==0){
tianShu=366+tianShu;
}else{
tianShu=365+tianShu;
}
int yueTians=0; //计算今月到一月天数
for(int i=1;i<yue;i++){
if ((nian%4==0&&nian%100!=0||nian%400==0)&&(i==2)){
day=29;
}else if(i==2){
day=28;
}else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
day=31;
}else if(i==4||i==6||i==9||i==11){
day=30;
}yueTians=yueTians+day;
}
//算星期
int zongTianshu=yueTians+1+tianShu;
int xingQi=zongTianshu%7;
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
int xing=xingQi;
if(xingQi<7){
while(xingQi>0){
System.out.print("\t");
xingQi--;
}
}
for(int i=1,j=1;i<=benYueTian;i++,j++){
if((j+xing-1)%7==0&&i!=1){
System.out.print("\n");
}
System.out.print(" "+i+"\t");
}
}}*****************欢迎使用万年历****************************
输入年份:2009
输入月份: 10
2009年10月有31天
星期日 星期一 星期二 星期三 星期四 星期五 星期六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31功能方面没有lz多,还需要完善
package com.gui;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;public class MyRiLi extends JPanel { protected int yy; protected int mm, dd; /** 存放按钮 */
protected JButton labs[][]; /** 每个月份开头的几个空白日期 */
protected int leadGap = 0; /** 日期对象 */
Calendar calendar = new GregorianCalendar(); /** 年份 */
protected final int thisYear = calendar.get(Calendar.YEAR); /** 月份 */
protected final int thisMonth = calendar.get(Calendar.MONTH); /** 取消按钮高亮时以这个按钮为基准 */
private JButton b0; /** 存放月份 */
private JComboBox monthChoice; /** 存放年份 */
private JComboBox yearChoice; /**
* 构造方法,获取今天的日期
*/
MyRiLi() {
super();
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
} MyRiLi(int year, int month, int today) {
super();
setYYMMDD(year, month, today);
buildGUI();
recompute();
} private void setYYMMDD(int year, int month, int today) {
yy = year;
mm = month;
dd = today;
} String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
"十月", "十一月", "十二月" }; private void buildGUI() {
getAccessibleContext().setAccessibleDescription("日历未上载");
setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); JPanel tp = new JPanel();
tp.add(monthChoice = new JComboBox());
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
monthChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = monthChoice.getSelectedIndex();
if (i >= 0) {
mm = i;
recompute();
}
}
});
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription("选择一个月份"); tp.add(yearChoice = new JComboBox());
yearChoice.setEditable(true);
for (int i = yy - 5; i < yy + 5; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = yearChoice.getSelectedIndex();
if (i >= 0) {
yy = Integer.parseInt(yearChoice.getSelectedItem()
.toString());
recompute();
}
}
});
add(BorderLayout.CENTER, tp); JPanel bp = new JPanel();
bp.setLayout(new GridLayout(7, 7));
labs = new JButton[6][7]; // 星期
bp.add(b0 = new JButton("日"));
bp.add(new JButton("一"));
bp.add(new JButton("二"));
bp.add(new JButton("三"));
bp.add(new JButton("四"));
bp.add(new JButton("五"));
bp.add(new JButton("六")); ActionListener dateSetter = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (!num.equals("")) {
// 将当前日期高亮
setDayActive(Integer.parseInt(num));
}
}
}; // 创建所有的按钮
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
bp.add(labs[i][j] = new JButton(""));
labs[i][j].addActionListener(dateSetter);
} add(BorderLayout.SOUTH, bp);
} // 列出每个月的天数
public final static int dom[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 }; /** 计算每个月份的日期,显示在面板上 */
protected void recompute() {
if (mm < 0 || mm > 11)
throw new IllegalArgumentException("月份 " + mm + " 必须在0-11之间");
clearDayActive();
calendar = new GregorianCalendar(yy, mm, dd); // 计算每个月份开头的几个空白日期
leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = dom[mm];
if (isLeap(calendar.get(Calendar.YEAR)) && mm > 1)
++daysInMonth; for (int i = 0; i < leadGap; i++) {
labs[0][i].setText("");
} // 填入日期数字
for (int i = 1; i <= daysInMonth; i++) {
JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7];
b.setText(Integer.toString(i));
} for (int i = leadGap + 1 + daysInMonth; i < 6 * 7; i++) {
labs[(i) / 7][(i) % 7].setText("");
} if (thisYear == yy && mm == thisMonth)
setDayActive(dd); repaint();
} /**
* 判断是否闰年
*/
public boolean isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
return false;
} /** 设置年月日 */
public void setDate(int yy, int mm, int dd) {
this.yy = yy;
this.mm = mm;
this.dd = dd;
recompute();
} /** 取消按钮高亮 */
private void clearDayActive() {
JButton b; if (activeDay > 0) {
b = labs[(leadGap + activeDay - 1) / 7][(leadGap + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
} private int activeDay = -1; /** 设置按钮高亮 */
public void setDayActive(int newDay) { clearDayActive(); if (newDay <= 0)
dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else
dd = newDay;
Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
} public static void main(String[] av) {
JFrame f = new JFrame("MyRiLi");
Container c = f.getContentPane();
c.setLayout(new FlowLayout()); c.add(new MyRiLi()); f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
有时间的话LZ你可以为 Struts提供一下组件服务, 向EJB那样。
这个 你可以自己运行一下 ,只要你输入的数据不对 他都会提示的 比如 某年2月是不是有29日等2009年以前我也 考虑了 就是 我调了很多次都调不出来,这个我会好好想想至于楼上所说的oop思想,刚开始我也注意到了。可是我水平有限:我只能说是能把它做出来我qq号是759445923 我在学Java/c/c++ 有兴趣的可以加我!
下面是我写的:
/*
*
*2009年9月25日
* 今天终于把它写完了,花了我三个晚上的时间 。感觉很充实,从中学到了很多东西;在实践中获取经验是很宝贵的。
* 要写好一个程序,不是一件容易的事,我感觉我这个程序只能说是他能运行,很乱就好像是拼凑出来的一样。所以如果
* 对他进行扩展,我想这是一件不可能的事。(所以在以后的练习中,要更加注重类和函数的设计。在此之前是从未感受到的)
*
* 功能:
* 根据输入的日期,判断是星期几;打印一个月或一整年的日历;
* 适用在2009.1.1以后的日期,最大限是一万年
*
*/
import java.util.*;
public class PerpetualCalendar
{
static int NowYear = 0;//定义变量:表输入的日期;
static int NowMonth = 0;
static int NowDay = 0;
static boolean leapYear = true;//逻辑变量为真表明此年为闰年;
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
String StringOne = new String(" ");
String StringYes = new String("yes");
String StringNo = new String("no"); human_computer();//完成交换信息,并对不合法的数据进行处理;记录输入的日期; DateClass DateClassOne = new DateClass(NowYear, NowMonth, NowDay);//此类完成输入日期是星期几的操作; System.out.println("\n*********************");
System.out.println("你输入的日期为:公元" + NowYear + "年" + NowMonth + "月" + NowDay + "日星期" + date(DateClassOne.getConterTotalOfWeek()));//提示用户所输入的信息和输出返回的星期数;
System.out.println("\n*********************");
System.out.println("\n 日 一 二 三 四 五 六");//输出格式; PrintCalendar();//调用函数打印该月的日历;
System.out.println("\n*********************\n");//打印整年;
System.out.println("要打印整年日历吗? 如果要请输入:\" yes\"! 否则请输入: \"no\"!\n\n");//合法检测,用户选择 yes打印, no不打印;
do
{ System.out.print("请做出你的选择:");
reader.hasNextLine(); StringOne = reader.nextLine();//用户输入
if (StringYes.equals(StringOne))
{ System.out.println("\n*********************\n");
PrintMonth();//选择输入,调用函数打印一年
break; } else if (StringNo.equals(StringOne))
{
System.out.println("\n本程序到此结束!");//选择不输入;
break;
}
else
{
System.out.println("\n你输入的数据不正确!");//输入了 不合法的数据;
System.out.println("\n请你再输入一次!"); }
}
while (!(StringYes.equals(StringOne) || StringNo.equals(StringOne)));//用户输入检测; } static char date(int IsDay)//通过返回的星期数IsDay,转化输出: 一,二,三, 四, 五,六,日;
{
char c = '错'; switch (IsDay)
{ case 1: c = '一'; break;
case 2: c = '二'; break;
case 3: c = '三'; break;
case 4: c = '四'; break;
case 5: c = '五'; break;
case 6: c = '六'; break;
case 0: c = '日'; break;
}
return c;
} static String dateTwo(int IsWeek)//十二月输出格式;
{
String c = new String("错"); switch (IsWeek)
{ case 1: c = "一"; break;
case 2: c = "二"; break;
case 3: c = "三"; break;
case 4: c = "四"; break;
case 5: c = "五"; break;
case 6: c = "六"; break;
case 7: c = "七"; break;
case 8: c = "八"; break;
case 9: c = "九"; break;
case 10: c = "十"; break;
case 11: c = "十一"; break;
case 12: c = "十二"; break; }
return c;
} static void human_computer()//人机交换信息
{
Scanner reader = new Scanner(System.in);
int TestYear = 0;
int TestMonth = 0;
int TestDay = 0;
int ConterDay = 0;
boolean Istrue = true;
boolean LeapYear = true; System.out.println(" 万年历 ");
System.out.println("请输入要查询的日期!"); do
{
System.out.print("输入年份:");
reader.hasNextInt();
TestYear = reader.nextInt();
Istrue = true;
if ((TestYear > 10000) || (TestYear < 2009))//判断有效年数;
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowYear = TestYear; do
{
System.out.print("\n输入月份:");//判断有效月数;
reader.hasNextInt();
TestMonth = reader.nextInt();
Istrue = true;
if ((TestMonth > 12) || (TestMonth <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowMonth = TestMonth;
//判断该月的天数 if (TestYear % 400 == 0 || TestYear % 4 == 0 && TestYear % 100 != 0)//判断闰年
LeapYear = true;
else LeapYear = false; do //对于号数的判断?
{
System.out.print("\n输入号数:");
reader.hasNextInt();
TestDay = reader.nextInt();
Istrue = true;
if (TestMonth == 1 || TestMonth == 3 || TestMonth == 5 || TestMonth == 7 || TestMonth == 8 || TestMonth == 10 || TestMonth == 12)//天数判断; ConterDay = 31; else if (TestMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; if ((TestDay > ConterDay) || (TestDay <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowDay = TestDay; } static void PrintCalendar()//打印 一个月;
{
DateClass DateClassThree = new DateClass(NowYear, NowMonth, 1);//创建对象DateClassTree
boolean LeapYear = true;
int ConterDay = 0;
int Week = DateClassThree.getConterTotalOfWeek();//通过对象DateClassTree调用函数,获得该年该月一号的 星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)
LeapYear = true;
else LeapYear = false; if (NowMonth == 1 || NowMonth == 3 || NowMonth == 5 || NowMonth == 7 || NowMonth == 8 || NowMonth == 10 || NowMonth == 12)//判断该月的天数; ConterDay = 31; else if (NowMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (Week * 4); p++)//该月第一天的位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//该月总天数循环
{
if (d < 10)
System.out.print(" " + d);//格式控制
else
System.out.print(" " + d); if ((Week + d) % 7 == 0)//跳转打印
System.out.print("\n");
} } static void PrintMonth()//打印 1--12个月;
{
DateClass DateClassTwo = new DateClass(NowYear, 1, 1);//该年一月一日; boolean LeapYear = true;
int ConterDay = 0;
int EveryOfWeek = DateClassTwo.getConterTotalOfWeek();//获得一月一日星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)//闰年判断
LeapYear = true;
else LeapYear = false;
for (int m = 1; m <= 12; m++)
{
System.out.println("公元" + NowYear + "年" + dateTwo(m) + "月");// 月的输入格式;
System.out.println("\n 日 一 二 三 四 五 六");
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)//整年循环; ConterDay = 31; else if (m == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (EveryOfWeek * 4); p++)//每月的第一天打印位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//当月循环;
{
if (d < 10)
System.out.print(" " + d);
else
System.out.print(" " + d); if ((EveryOfWeek + d) % 7 == 0)
System.out.print("\n");
}
System.out.println("\n*********************\n");
EveryOfWeek = (ConterDay + EveryOfWeek) % 7;//判断下月第一天是星期几; } }
}class DateClass//主要用来返回星期;
{
int Year;
int Month;
int Day;
int ConterWeek;
DateClass(int y, int m, int d)//构造函数;
{
Year = y;
Month = m;
Day = d;
} boolean getLeap(int Year)//闰年
{
boolean LeapYear = true;
if (Year % 400 == 0 || Year % 4 == 0 && Year % 100 != 0)
return LeapYear;
else return LeapYear = false;
} int getConterTotalOfWeek()//返回星期
{
int ChangYear = Year;
long ConterTotal = 0;
long ConterYear = 0;
int ConterMonth = 0; for (; ChangYear > 2009; ChangYear--)//整年天数的累计 if (getLeap(ChangYear))
ConterYear += 366;
else
ConterYear += 365;
ConterMonth = getConterMonth(Month - 1);//调用返回月累计; ConterTotal = ConterYear + ConterMonth + (Day - 1);//总计天数; return ConterWeek = (int)((ConterTotal + 4) % 7);//返回;
} int getConterMonth(int m)//月累计;
{
int ConterMonth = 0;
switch (m)
{
case 11: ConterMonth += 30;
case 10: ConterMonth += 31;
case 9: ConterMonth += 30;
case 8: ConterMonth += 31;
case 7: ConterMonth += 31;
case 6: ConterMonth += 30;
case 5: ConterMonth += 31;
case 4: ConterMonth += 30;
case 3: ConterMonth += 31;
case 2: ConterMonth += (getLeap(Year) ? 29 : 28);
case 1: ConterMonth += 31;
case 0: ;
}
return ConterMonth; }
}
//到此大功告成!我看了一下大概有300多行!嘿嘿嘿!!!
/*
*
*2009年9月25日
* 今天终于把它写完了,花了我三个晚上的时间 。感觉很充实,从中学到了很多东西;在实践中获取经验是很宝贵的。
* 要写好一个程序,不是一件容易的事,我感觉我这个程序只能说是他能运行,很乱就好像是拼凑出来的一样。所以如果
* 对他进行扩展,我想这是一件不可能的事。(所以在以后的练习中,要更加注重类和函数的设计。在此之前是从未感受到的)
*
* 功能:
* 根据输入的日期,判断是星期几;打印一个月或一整年的日历;
* 适用在2009.1.1以后的日期,最大限是一万年
*
*/
import java.util.*;
public class PerpetualCalendar
{
static int NowYear = 0;//定义变量:表输入的日期;
static int NowMonth = 0;
static int NowDay = 0;
static boolean leapYear = true;//逻辑变量为真表明此年为闰年;
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
String StringOne = new String(" ");
String StringYes = new String("yes");
String StringNo = new String("no"); human_computer();//完成交换信息,并对不合法的数据进行处理;记录输入的日期; DateClass DateClassOne = new DateClass(NowYear, NowMonth, NowDay);//此类完成输入日期是星期几的操作; System.out.println("\n*********************");
System.out.println("你输入的日期为:公元" + NowYear + "年" + NowMonth + "月" + NowDay + "日星期" + date(DateClassOne.getConterTotalOfWeek()));//提示用户所输入的信息和输出返回的星期数;
System.out.println("\n*********************");
System.out.println("\n 日 一 二 三 四 五 六");//输出格式; PrintCalendar();//调用函数打印该月的日历;
System.out.println("\n*********************\n");//打印整年;
System.out.println("要打印整年日历吗? 如果要请输入:\" yes\"! 否则请输入: \"no\"!\n\n");//合法检测,用户选择 yes打印, no不打印;
do
{ System.out.print("请做出你的选择:");
reader.hasNextLine(); StringOne = reader.nextLine();//用户输入
if (StringYes.equals(StringOne))
{ System.out.println("\n*********************\n");
PrintMonth();//选择输入,调用函数打印一年
break; } else if (StringNo.equals(StringOne))
{
System.out.println("\n本程序到此结束!");//选择不输入;
break;
}
else
{
System.out.println("\n你输入的数据不正确!");//输入了 不合法的数据;
System.out.println("\n请你再输入一次!"); }
}
while (!(StringYes.equals(StringOne) || StringNo.equals(StringOne)));//用户输入检测; } static char date(int IsDay)//通过返回的星期数IsDay,转化输出: 一,二,三, 四, 五,六,日;
{
char c = '错'; switch (IsDay)
{ case 1: c = '一'; break;
case 2: c = '二'; break;
case 3: c = '三'; break;
case 4: c = '四'; break;
case 5: c = '五'; break;
case 6: c = '六'; break;
case 0: c = '日'; break;
}
return c;
} static String dateTwo(int IsWeek)//十二月输出格式;
{
String c = new String("错"); switch (IsWeek)
{ case 1: c = "一"; break;
case 2: c = "二"; break;
case 3: c = "三"; break;
case 4: c = "四"; break;
case 5: c = "五"; break;
case 6: c = "六"; break;
case 7: c = "七"; break;
case 8: c = "八"; break;
case 9: c = "九"; break;
case 10: c = "十"; break;
case 11: c = "十一"; break;
case 12: c = "十二"; break; }
return c;
} static void human_computer()//人机交换信息
{
Scanner reader = new Scanner(System.in);
int TestYear = 0;
int TestMonth = 0;
int TestDay = 0;
int ConterDay = 0;
boolean Istrue = true;
boolean LeapYear = true; System.out.println(" 万年历 ");
System.out.println("请输入要查询的日期!"); do
{
System.out.print("输入年份:");
reader.hasNextInt();
TestYear = reader.nextInt();
Istrue = true;
if ((TestYear > 10000) || (TestYear < 2009))//判断有效年数;
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowYear = TestYear; do
{
System.out.print("\n输入月份:");//判断有效月数;
reader.hasNextInt();
TestMonth = reader.nextInt();
Istrue = true;
if ((TestMonth > 12) || (TestMonth <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowMonth = TestMonth;
//判断该月的天数 if (TestYear % 400 == 0 || TestYear % 4 == 0 && TestYear % 100 != 0)//判断闰年
LeapYear = true;
else LeapYear = false; do //对于号数的判断?
{
System.out.print("\n输入号数:");
reader.hasNextInt();
TestDay = reader.nextInt();
Istrue = true;
if (TestMonth == 1 || TestMonth == 3 || TestMonth == 5 || TestMonth == 7 || TestMonth == 8 || TestMonth == 10 || TestMonth == 12)//天数判断; ConterDay = 31; else if (TestMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; if ((TestDay > ConterDay) || (TestDay <= 0))
{
System.out.println("\n请输入有效数据!");
Istrue = false;
}
if (Istrue)
{ break; } }
while (!Istrue);
NowDay = TestDay; } static void PrintCalendar()//打印 一个月;
{
DateClass DateClassThree = new DateClass(NowYear, NowMonth, 1);//创建对象DateClassTree
boolean LeapYear = true;
int ConterDay = 0;
int Week = DateClassThree.getConterTotalOfWeek();//通过对象DateClassTree调用函数,获得该年该月一号的 星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)
LeapYear = true;
else LeapYear = false; if (NowMonth == 1 || NowMonth == 3 || NowMonth == 5 || NowMonth == 7 || NowMonth == 8 || NowMonth == 10 || NowMonth == 12)//判断该月的天数; ConterDay = 31; else if (NowMonth == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (Week * 4); p++)//该月第一天的位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//该月总天数循环
{
if (d < 10)
System.out.print(" " + d);//格式控制
else
System.out.print(" " + d); if ((Week + d) % 7 == 0)//跳转打印
System.out.print("\n");
} } static void PrintMonth()//打印 1--12个月;
{
DateClass DateClassTwo = new DateClass(NowYear, 1, 1);//该年一月一日; boolean LeapYear = true;
int ConterDay = 0;
int EveryOfWeek = DateClassTwo.getConterTotalOfWeek();//获得一月一日星期;
if (NowYear % 400 == 0 || NowYear % 4 == 0 && NowYear % 100 != 0)//闰年判断
LeapYear = true;
else LeapYear = false;
for (int m = 1; m <= 12; m++)
{
System.out.println("公元" + NowYear + "年" + dateTwo(m) + "月");// 月的输入格式;
System.out.println("\n 日 一 二 三 四 五 六");
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)//整年循环; ConterDay = 31; else if (m == 2)
ConterDay = LeapYear ? 29 : 28;
else ConterDay = 30; for (int p = 1; p <= (EveryOfWeek * 4); p++)//每月的第一天打印位置; System.out.print(" ");
for (int d = 1; d <= ConterDay; d++)//当月循环;
{
if (d < 10)
System.out.print(" " + d);
else
System.out.print(" " + d); if ((EveryOfWeek + d) % 7 == 0)
System.out.print("\n");
}
System.out.println("\n*********************\n");
EveryOfWeek = (ConterDay + EveryOfWeek) % 7;//判断下月第一天是星期几; } }
}class DateClass//主要用来返回星期;
{
int Year;
int Month;
int Day;
int ConterWeek;
DateClass(int y, int m, int d)//构造函数;
{
Year = y;
Month = m;
Day = d;
} boolean getLeap(int Year)//闰年
{
boolean LeapYear = true;
if (Year % 400 == 0 || Year % 4 == 0 && Year % 100 != 0)
return LeapYear;
else return LeapYear = false;
} int getConterTotalOfWeek()//返回星期
{
int ChangYear = Year;
long ConterTotal = 0;
long ConterYear = 0;
int ConterMonth = 0; for (; ChangYear > 2009; ChangYear--)//整年天数的累计 if (getLeap(ChangYear))
ConterYear += 366;
else
ConterYear += 365;
ConterMonth = getConterMonth(Month - 1);//调用返回月累计; ConterTotal = ConterYear + ConterMonth + (Day - 1);//总计天数; return ConterWeek = (int)((ConterTotal + 4) % 7);//返回;
} int getConterMonth(int m)//月累计;
{
int ConterMonth = 0;
switch (m)
{
case 11: ConterMonth += 30;
case 10: ConterMonth += 31;
case 9: ConterMonth += 30;
case 8: ConterMonth += 31;
case 7: ConterMonth += 31;
case 6: ConterMonth += 30;
case 5: ConterMonth += 31;
case 4: ConterMonth += 30;
case 3: ConterMonth += 31;
case 2: ConterMonth += (getLeap(Year) ? 29 : 28);
case 1: ConterMonth += 31;
case 0: ;
}
return ConterMonth; }
}
//到此大功告成!我看了一下大概有300多行!嘿嘿嘿!!!
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DayTask extends JFrame{
int temp;
public JList list;
public JComboBox comboboxMonth;
public JButton[]buttonDay;
public JLabel[]labelWeek;
public Container container;
public BorderLayout layout;
public JPanel panel;
public JPanel panelN;
public JPanel panelC;
public JPanel panelCN;
public JPanel panelCC;
public JPanel panelS;
public JTextArea area;
public JScrollBar scrollbar;
public Calendar calendar;
public Calendar nowtime;
public String year;
public String month;
public String day;
public String listYears[];
public String week[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
public String listMonth[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
public int i;
public int k; public DayTask() {
// TODO Auto-generated constructor stub
super("李世贵万年历");
container=getContentPane();
layout=new BorderLayout();
container.setLayout(layout);
calendar=Calendar.getInstance();
calendar.setTime(new Date());
nowtime=Calendar.getInstance();
nowtime.setTime(new Date());
year=String.valueOf(calendar.get(Calendar.YEAR));
month=String.valueOf(calendar.get(Calendar.MONTH)+1);
//String listYears[]={"2007","2008","2009"};
listYears=new String[70];
for(int i=1980;i<2050;i++)
listYears[i-1980]=new String(""+i);
list=new JList(listYears);
list.setSelectedValue(year, true);
comboboxMonth=new JComboBox(listMonth);
comboboxMonth.setSelectedIndex(calendar.get(Calendar.MONTH));
comboboxMonth.addActionListener(
new ActionListener(){ public void actionPerformed(ActionEvent arg0){
//public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
int month=comboboxMonth.getSelectedIndex();
String year=(String)list.getSelectedValue();
DayLayout(year,month);
}
}
);
list.setVisibleRowCount(1);
JScrollPane scrollpane=new JScrollPane(list);
scrollbar=scrollpane.getVerticalScrollBar();
k=calendar.get(Calendar.YEAR)-1980;
i=20*(calendar.get(Calendar.YEAR)-1980)+10;
scrollbar.setMaximum(i);
scrollbar.setValue(i);
i=scrollbar.getValue();
scrollbar.addAdjustmentListener(
new AdjustmentListener(){ public void adjustmentValueChanged(AdjustmentEvent arg0) {
// TODO Auto-generated method stub
if(i<scrollbar.getValue())list.setSelectedIndex(++k);
if(i>scrollbar.getValue())list.setSelectedIndex(--k);
i=scrollbar.getValue();
int month=comboboxMonth.getSelectedIndex();
String year=(String)list.getSelectedValue();
DayLayout(year,month);
}
}
);
panelN=new JPanel();
panelN.setLayout(new FlowLayout());
panelN.add(scrollpane);
panelN.add(comboboxMonth);
//week[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
panelCN=new JPanel();
labelWeek=new JLabel[7];
panelCN.setLayout(new GridLayout(1,7,1,1));
for(int i=0;i<7;i++){
labelWeek[i]=new JLabel(week[i]);
panelCN.add(labelWeek[i]);
}
calendar.set(Integer.parseInt(year), Integer.parseInt(month)-1,1);
int w=calendar.get(Calendar.DAY_OF_WEEK);
int Wyear=Integer.parseInt(year);
int Mdays;
int Ymonth=Integer.parseInt(month);
if(Ymonth==2){
if((Wyear%4==0&&Wyear%100!=0)||Wyear%400==0)
Mdays=29;
else
Mdays=28;
}
else{
if(Ymonth==1||Ymonth==3||Ymonth==5||Ymonth==7||Ymonth==8||Ymonth==10||Ymonth==12)
Mdays=31;
else
Mdays=30;
}
panelCC=new JPanel();
panelCC.setLayout(new GridLayout(6,7,1,1));
buttonDay=new JButton[42];
for(int i=1;i<w;i++){
buttonDay[i-1]=new JButton("");
//buttonDay[i-1].setVisible(false);
panelCC.add(buttonDay[i-1]);
}
for(int i=0;i<Mdays;i++){
if(nowtime.get(Calendar.DAY_OF_MONTH)==(i+1)){
buttonDay[w+i]=new JButton(""+(i+1));
buttonDay[w+i].setBackground(Color.GREEN);
panelCC.add(buttonDay[w+i]);
}
else{
buttonDay[w+i]=new JButton(""+(i+1));
panelCC.add(buttonDay[w+i]);
}
}
for(int i=0;i<(42-w-Mdays+1);i++){
buttonDay[w+Mdays+i-1]=new JButton("");
//buttonDay[w+Mdays+i-1].setVisible(false);
panelCC.add(buttonDay[w+Mdays+i-1]);
}
area=new JTextArea(20,10);
JLabel label=new JLabel("今天学到了:");
panelS=new JPanel();
panelS.setLayout(new BorderLayout());
panelS.add(label,BorderLayout.NORTH);
panelS.add(new JScrollPane(area),BorderLayout.CENTER);
panelC=new JPanel();
panelC.setLayout(new BorderLayout());
panelC.add(panelCN,BorderLayout.NORTH);
panelC.add(panelCC,BorderLayout.CENTER);
container.add(panelN,BorderLayout.NORTH);
container.add(panelC,BorderLayout.CENTER);
//container.add(panelS,BorderLayout.SOUTH);
setSize(400,300);
setVisible(true);
} protected void DayLayout(String year,int month) {
// TODO Auto-generated method stub
calendar.set(Integer.parseInt(year),month,1);
int w=calendar.get(Calendar.DAY_OF_WEEK);
int Wyear=Integer.parseInt(year);
int Mdays;
int Ymonth=month+1;
if(Ymonth==2){
if((Wyear%4==0&&Wyear%100!=0)||Wyear%400==0)
Mdays=29;
else
Mdays=28;
}
else{
if(Ymonth==1||Ymonth==3||Ymonth==5||Ymonth==7||Ymonth==8||Ymonth==10||Ymonth==12)
Mdays=31;
else
Mdays=30;
}
panelCC.removeAll();
buttonDay=new JButton[42];
for(int i=1;i<w;i++){
buttonDay[i-1]=new JButton("");
//buttonDay[i-1].setVisible(false);
panelCC.add(buttonDay[i-1]);
}
for(int i=0;i<Mdays;i++){
if(nowtime.get(Calendar.DAY_OF_MONTH)==(i+1)){
buttonDay[w+i]=new JButton(""+(i+1));
buttonDay[w+i].setBackground(Color.GREEN);
panelCC.add(buttonDay[w+i]);
}
else{
buttonDay[w+i]=new JButton(""+(i+1));
panelCC.add(buttonDay[w+i]);
}
}
for(int i=0;i<(42-w-Mdays+1);i++){
buttonDay[w+Mdays+i-1]=new JButton("");
//buttonDay[w+Mdays+i-1].setVisible(false);
panelCC.add(buttonDay[w+Mdays+i-1]);
}
setSize(400,300);
setVisible(true);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DayTask application=new DayTask();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
E:\>javac PerpetualCalendar.java
PerpetualCalendar.java:12: cannot resolve symbol
symbol : class Scanner
location: class PerpetualCalendar
Scanner reader = new Scanner(System.in);
^
PerpetualCalendar.java:12: cannot resolve symbol
symbol : class Scanner
location: class PerpetualCalendar
Scanner reader = new Scanner(System.in);
^
PerpetualCalendar.java:119: cannot resolve symbol
symbol : class Scanner
location: class PerpetualCalendar
Scanner reader = new Scanner(System.in);
^
PerpetualCalendar.java:119: cannot resolve symbol
symbol : class Scanner
location: class PerpetualCalendar
Scanner reader = new Scanner(System.in);
就是这些
F:\>java PerpetualCalendar
万年历
请输入要查询的日期!
输入年份:2111输入月份:2输入号数:29请输入有效数据!输入号数:28*********************
你输入的日期为:公元2111年2月28日星期六********************* 日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28*********************要打印整年日历吗? 如果要请输入:" yes"! 否则请输入: "no"!
请做出你的选择:yes*********************公元2111年一月 日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31*********************公元2111年二月 日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28*********************公元2111年三月 日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
*********************公元2111年四月 日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
*********************公元2111年五月 日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
*********************公元2111年六月 日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
*********************公元2111年七月 日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
*********************公元2111年八月 日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
*********************公元2111年九月 日 一 二 三 四 五 六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
*********************公元2111年十月 日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31*********************公元2111年十一月 日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
*********************公元2111年十二月 日 一 二 三 四 五 六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
*********************
F:\>
万年历
请输入要查询的日期!
输入年份:444444444444444444444444
Exception in thread "main" java.util.InputMismatchException: For input string: "444444444444444444444444"
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at test.human_computer(test.java:155)
at test.main(test.java:16)
万年历
请输入要查询的日期!
输入年份:dfg
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at test.human_computer(test.java:155)
at test.main(test.java:16)楼主,你没有做异常收集哦,这个是很重要的~