按照你的要求編寫的Java swing 帶界面的萬年歷代碼如下
創(chuàng)新互聯(lián)服務(wù)項目包括利津網(wǎng)站建設(shè)、利津網(wǎng)站制作、利津網(wǎng)頁制作以及利津網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,利津網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到利津省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
//日歷
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Calendar;
import?javax.swing.BorderFactory;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
public?class?CCI?extends?JFrame?implements?ActionListener{
JButton?jb1=new?JButton("");
JButton?jb2=new?JButton("");
JButton?jb3=new?JButton("");
JButton?jb4=new?JButton("");
JPanel?jp1=new?JPanel();
JPanel?jp2=new?JPanel();
JPanel?jp3=new?JPanel();
JPanel?jp4=new?JPanel();
JLabel?jl1=new?JLabel();
JLabel?jl2=new?JLabel();
JLabel[]jl=new?JLabel[49];
String?[]week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendar?c=Calendar.getInstance();
int?year,month,day;
int?nowyear,nowmonth,nowday;
CCI(){
super("簡單日歷");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
String?s=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(new?Font(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(new?Font(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public?void?actionPerformed(ActionEvent?ae)?{
if(ae.getSource()==jb1){
year=year-1;
String?s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
String?s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
String?s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
String?s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
public?static?void?main(String[]?args)?{
new?CCI();
}
public?int?getMonthDays(int?year,?int?month)?{?
switch?(month)?{
case?1:?
case?3:?
case?5:?
case?7:
case?8:?
case?10:?
case?12:
return?31;?
case?2:?
if?((year%4==0year%100!=0)||year%400==0)?{?
return?29;?
}?else?{?
return?28;?
}?
default:?
return?30;?
}?
}?
public?void?createMonthPanel(){
c.set(year,?month-1,?getMonthDays(year,month));
int?weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(new?GridLayout(7,7));
jp3.setBounds(50,?20,?420,?350);
}else{
jp3.setLayout(new?GridLayout(6,7));
jp3.setBounds(50,?20,?420,?300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(int?i=0;i7;i++){
jl[i]=new?JLabel(week[i],JLabel.CENTER);
jl[i].setFont(new?Font(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year,?month-1,?1);
int?emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
int?daysOfMonth=getMonthDays(year,month);
for(int?i=6+emptyFirst;i=7;i--){
int?intyear=year;
int?intmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
int?intdays=getMonthDays(intyear,intmonth);
jl[i]=new?JLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(new?Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(int?i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){
jl[i]=new?JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(new?Font(null,Font.BOLD,20));
if((i+1)%7==0?||?(i+1)%7==1){
jl[i].setForeground(Color.RED);
}else?if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(int?i=48;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new?JLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(new?Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(int?i=41;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new?JLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(new?Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
我改造了一下你的代碼 :
package?com.mikuma.calendar;
import?java.util.GregorianCalendar;
import?java.util.Scanner;
public?class?Calendar{
public?static?void?main(String[]?args){
int?year?=?0;
int?month?=?0;
Scanner?scanner?=?new?Scanner(System.in);
System.out.println("請輸入您要查詢的年份");
year?=?scanner.nextInt();
System.out.println("請輸入您要查詢的月份");
while?(true){
month?=?scanner.nextInt();
if?(month??0?||?month??12){
System.out.println("月份輸入有誤,請重新輸入");
}else{
break;
}
}
printPermanentCalendar(year,?month);
}
/**
*?輸出萬年歷
*?
*?@param?year
*?@param?month
*/
private?static?void?printPermanentCalendar(int?year,int?month){
int?days?=?0;
int?totaldays?=?0;//獲取1990年至查詢的年份的天數(shù)
for?(int?i?=?1900;?i??year;?i++){
totaldays?=?totaldays?+?(isLeapYear(i)???366?:?365);
}
int?beforedays?=?0;//到指定月份的天數(shù)
for?(int?i?=?1;?i?=?month;?i++){
switch?(i)?{
case?1:
case?3:
case?5:
case?7:
case?8:
case?10:
case?12:
days?=?31;
break;
case?4:
case?6:
case?9:
case?11:
days?=?30;
break;
case?2:
days?=?isLeapYear(year)???29?:?28;
break;
default:
break;
}
if?(i??month){
beforedays?=?beforedays?+?days;
}
}
totaldays?=?totaldays?+?beforedays;//總計天數(shù),以判斷周幾;
int?weekDay?=?0;
int?temp?=?(1?+?totaldays)?%?7;
if?(temp?==?0){//1990年1月1日星期一,據(jù)此日0天星期一,以此類推
weekDay?=?0;
}else{
weekDay?=?temp;
}
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for?(int?i?=?0;?i??weekDay;?i++){
System.out.print("\t");
}
for?(int?i?=?1;?i?=?days;?i++){
System.out.print(i?+?"\t");
if?((totaldays?+?i)?%?7?==?6){
System.out.print("\n");
}
}
}
private?static?boolean?isLeapYear(int?year){
return?new?GregorianCalendar().isLeapYear(year);
}
}
運行:
對比了下 360日歷:
結(jié)果正確
我們再測試下 2017 年 2月
對比 ?360 日歷
也是正確
import?java.util.Calendar;
import?java.util.Date;
import?java.util.Scanner;
public?class?MyCalendar
{
public?static?void?main(String[]?args)
{
Scanner?scanner?=?new?Scanner(System.in);
String?reg?=?"^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";
while(true)
{
System.out.println("輸入年月(年和月用非數(shù)字隔開:如2015.1)(什么都不輸入直接退出)");
String?line?=?scanner.nextLine().trim();
if("".equals(line))
{
scanner.close();
break;
}
if(!line.matches(reg))
{
continue;
}
int?year?=?Integer.parseInt(line.replaceAll(reg,?"$1"));
int?month?=?Integer.parseInt(line.replaceAll(reg,?"$2"));
System.out.println("日\t一\t二\t三\t四\t五\t六");
Calendar?calendar?=?Calendar.getInstance();
//?這個月的1號是星期幾
calendar.set(year,?month?-?1,?1);
int?day?=?calendar.get(Calendar.DAY_OF_WEEK);
int?start?=?Calendar.SUNDAY;
calendar.add(Calendar.DATE,?-day?+?start);
while(start??day)
{
System.out.print(calendar.get(Calendar.DATE)?+?"\t");
calendar.add(Calendar.DATE,?1);
start++;
}
calendar.set(year,?month?-?1,?1);
Date?now?=?calendar.getTime();
calendar.set(year,?month,?1);
Date?next?=?calendar.getTime();
for(Date?cur?=?now;?cur.before(next);)
{
calendar.setTime(cur);
int?x?=?calendar.get(Calendar.DATE);
String?tmp?=?x??10???"0"?+?x?:?x?+?"";
System.out.print(tmp?+?"\t");
if(calendar.get(Calendar.DAY_OF_WEEK)?==?Calendar.SATURDAY)
{
System.out.println();
}
calendar.add(Calendar.DATE,?1);
cur?=?calendar.getTime();
}
calendar.add(Calendar.DATE,?-1);
int?to?=?calendar.get(Calendar.DAY_OF_WEEK);
int?end?=?Calendar.SATURDAY;
while(to??end)
{
calendar.add(Calendar.DATE,?1);
int?x?=?calendar.get(Calendar.DATE);
String?tmp?=?x??10???"0"?+?x?:?x?+?"";
System.out.print(tmp?+?"\t");
to++;
}
System.out.println();
}
}
}
沒分的話……就只給你個代碼吧……這個是用比較簡單的語句寫的:
import java.util.Scanner;
public class PrintCalendar {
/**
* 打印日歷
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請輸入年:");
int year = input.nextInt();
System.out.println("請輸入月:");
int month = input.nextInt();
//定義變量,存儲該月多少天
int daysOfMonth = 0;
//計算該月多少天
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysOfMonth = 31;
break;
case 2:
if(year % 400 == 0 || (year % 4 == 0 year % 100 != 0))
{
daysOfMonth = 29;
}else
{
daysOfMonth = 28;
}
break;
default:
daysOfMonth = 30;
}
//System.out.println("該月"+daysOfMonth+"天");
//計算從1099.01.01到輸入的年一共過去了多少天
//定義變量,存儲總天數(shù)
int totalDays = 0;
for(int i=1900; iyear; i++)
{
if(i % 400 == 0 || (i % 4 == 0 i % 100 != 0))
{
totalDays += 366;
}else
{
totalDays += 365;
}
}
//輸入的年中,從1月到輸入的月,過去了多少天
for(int i=1; imonth; i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
totalDays += 31;
break;
case 2:
if(year % 400 == 0 || (year % 4 == 0 year % 100 != 0))
{
totalDays += 29;
}else
{
totalDays += 28;
}
break;
default:
totalDays += 30;
}
}
//輸入月的1號是周幾
int weekDay = totalDays % 7 + 1;
//計算需要打印的空格數(shù)
int spaces = weekDay % 7;
//打印日歷,先打印日歷的頭
System.out.println("日\t一\t二\t三\t四\t五\t六");
//定義變量,存儲打印了幾次
int count = 0;
//打印空格
for(int i=1; i=spaces; i++)
{
System.out.print(" \t");
count++;
}
//打印日歷中的每一天
for(int i=1; i=daysOfMonth; i++)
{
System.out.print(i+"\t");
count++;
if(count % 7 == 0)
{
System.out.println();
}
}
}
}
/*
題目:輸出任意年份任意月份的日歷表(公元后)
思路:
1.已知1年1月1日是星期日,1?%?7?=?1?對應(yīng)的是星期日,2?%?7?=?2?對應(yīng)的是星期一,以此類推;
2.計算當(dāng)年以前所有天數(shù)+當(dāng)年當(dāng)月1號之前所有天數(shù);
a.年份分平年閏年,平年365天,閏年366天;
b.閏年的判斷方法year?%?400?==?0?||?(year?%?100?!=?0??year?%?4?==?0)若為真,則為閏年否則為平年;
c.定義平年/閏年數(shù)組,包含各月天數(shù);
d.遍歷數(shù)組求和,計算當(dāng)年當(dāng)月前總天數(shù);
e.當(dāng)年以前所有天數(shù)+當(dāng)年當(dāng)月前總天數(shù)+1即為1年1月1日到當(dāng)年當(dāng)月1日的總天數(shù);
3.總天數(shù)對7取模,根據(jù)結(jié)果判斷當(dāng)月1號是星期幾,輸出空白區(qū)域;
4.輸出當(dāng)月日歷表,逢星期六換行
*/
import?java.util.Scanner;
class?FindMonthList?{
public?static?void?main(String[]?args){
Scanner?sc?=?new?Scanner(System.in);
System.out.println("請輸入年份:");
int?year?=?sc.nextInt();????????????//年份
if?(year??1)?{????????????????????????//判斷非法輸入年份
System.out.println("輸入錯誤!");
return;
}
System.out.println("請輸入月份:");
int?month?=?sc.nextInt();????????????//月份
if?(month??1?||?month??12)?{????????//判斷非法輸入月份
System.out.println("輸入錯誤!");
return;
}
//輸出表頭
System.out.println("-------"?+?year?+?"?年?"?+?month?+?"?月?"?+?"-------");
System.out.println();
System.out.println("日??一??二??三??四??五??六");
//計算當(dāng)前年份以前所有天數(shù)beforeYearTotalDay;每4年一個閏年,閏年366天,平年365天
int?beforeYearTotalDay?=?((year?-?1)?/?4?*?366)?+?(year-1?-?((year?-?1)?/?4))?*?365;
int[]?arrLeapYear?=?{0,31,29,31,30,31,30,31,31,30,31,30,31};????//閏年各月天數(shù)????int數(shù)組
int[]?arrNormalYear?=?{0,31,28,31,30,31,30,31,31,30,31,30,31};????//平年各月天數(shù)????int數(shù)組
int?beforeMonthTotalDay?=?0;????????????????????????????????????//定義本年當(dāng)月之前月份的總天數(shù)
if?(year?%?400?==?0?||?(year?%?100?!=?0??year?%?4?==?0))?{????//判斷當(dāng)前年份是否是閏年
for?(int?i?=?0?;?i??month?;?i?++?)?{????//for循環(huán)計算當(dāng)月之前總天數(shù)
//計算當(dāng)前月份之前的所有天數(shù)
beforeMonthTotalDay?=?beforeMonthTotalDay?+?arrLeapYear[i];
}
//判斷當(dāng)月1日是星期幾
int?totalDay?=?beforeYearTotalDay?+?beforeMonthTotalDay?+?1;
int?week?=?totalDay?%?7;//已知1年1月1日是星期日,即模7得1對應(yīng)的是星期日
for?(int?i?=?0?;?i??(week?-?1?+?7)?%?7?;?i?++)?{????//如果寫成i??(week-1)會出現(xiàn)i-1的情況
System.out.print("????");//輸出開頭空白
}
for?(int?i?=?1?;i?=?arrLeapYear[month]?;i?++?)?{????//for循環(huán)輸出各月天數(shù)
System.out.print(i?+?"??");
if?(i??10?)?{????????//小于10的數(shù)補一個空格,以便打印整齊
System.out.print("?");
}
if?(i?%?7?==?((7-(week?-?1))?%?7?)?||?i?==?arrLeapYear[month])?{//每逢星期六/尾數(shù)換行
System.out.println();
}
}
}?else?{????????//不是閏年就是平年
for?(int?i?=?0?;?i??month?;?i?++?)?{????//for循環(huán)計算出當(dāng)月之前月份總天數(shù)
beforeMonthTotalDay?=?beforeMonthTotalDay?+?arrNormalYear[i];
}
//判斷當(dāng)月1日是星期幾
int?totalDay?=?beforeYearTotalDay?+?beforeMonthTotalDay?+?1;
int?week?=?totalDay?%?7;//已知1年1月1日是星期日,即模7得1對應(yīng)的是星期日
for?(int?i?=?0?;?i??(week?-?1?+?7)?%?7?;?i?++)?{????//如果寫成i??(week-1)會出現(xiàn)i-1的情況
System.out.print("????");//輸出開頭空白
}
for?(int?i?=?1?;i?=?arrNormalYear[month]?;i?++?)?{//for循環(huán)輸出各月天數(shù)
System.out.print(i?+?"??");
if?(i??10?)?{????????????//小于10的數(shù)補一個空格,以便打印整齊
System.out.print("?");
}
if?(i?%?7?==?((7-(week?-?1))?%?7?)?||?i?==?arrNormalYear[month])?{//每逢星期六/尾數(shù)換行
System.out.println();
}
}
}
}
}
顯示效果:
import java.io.*;
class putout{
public void putout(int f,int x,int y){
int i;
int a[]= new int[40];
System.out.println(" 日 一 二 三 四 五 六 "+" "+f+"月");
for (i=0;ix;i++)
{System.out.print(" "); }
for(i=x;ix+y;i++)
a[i]=i-x+1;
for(i=x;ix+y;i++)
{
if ((i%7==0)(i0))
System.out.print("\n");
if (a[i]10)
System.out.print(" "+a[i]);
else System.out.print(" "+a[i]);
}
System.out.println("\n");
}
}
class st{
public static void main(String args[])throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print("請輸入一個年份:");
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;iyear;i++)
{if((i%4==0 i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i13;i++){
switch(i){
case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
}
}
}
}