java寫一年的日曆(用java寫一個日曆)

本文目錄一覽:

怎樣用java編寫日曆

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.SystemColor;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Locale;

import java.util.Date;

import java.util.StringTokenizer;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JToggleButton;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

/**

* pTitle: Swing日曆/p

* pDescription: 操作日期/p

* @author duxu2004

* @version 1.0.1

*/

class JCalendar extends JPanel{

//動態表示年月日

private int year=0;

private int month=0;

private int day=0;

//主面板

private JPanel Main = new JPanel();

//日面板

private JPanel jPanelDay = new JPanel();

//月面板

private JPanel jPanelMonth = new JPanel();

//年的輸入位置

private JTextField Year = new JTextField();

//月的輸入位置

private JTextField Month = new JTextField();

//減少月份

private JButton MonthDown = new JButton();

//增加月份

private JButton MonthUp = new JButton();

private JPanel jPanelButton = new JPanel();

//減少年份

private JButton YearDown = new JButton();

//增加年份

private JButton YearUp = new JButton();

//顯示日期的位置

private JLabel Out = new JLabel();

//中國時區,以後可以從這裡擴展可以設置時區的功能

private Locale l=Locale.CHINESE;

//主日曆

private GregorianCalendar cal=new GregorianCalendar(l);

//星期面板

private JPanel weekPanel=new JPanel();

//天按鈕組

private JToggleButton[] days=new JToggleButton[42];

//天面板

private JPanel Days = new JPanel();

//標示

private JLabel jLabel1 = new JLabel();

private JLabel jLabel2 = new JLabel();

private JLabel jLabel3 = new JLabel();

private JLabel jLabel4 = new JLabel();

private JLabel jLabel5 = new JLabel();

private JLabel jLabel6 = new JLabel();

private JLabel jLabel7 = new JLabel();

//當前選擇的天數按鈕

private JToggleButton cur=null;

//月份天數數組,用來取得當月有多少天

// 1 2 3 4 5 6 7 8 9 10 11 12

private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};

//空日期構造函數

public JCalendar() {

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//帶日期設置的構造函數

public JCalendar(int year, int month, int day) {

cal.set(year, month, day);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//帶日曆輸入的構造函數

public JCalendar(GregorianCalendar calendar) {

cal=calendar;

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//帶日期輸入的構造函數

public JCalendar(Date date) {

cal.setTime(date);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//初始化組件

private void jbInit() throws Exception {

//初始化年、月、日

iniCalender();

this.setLayout(new BorderLayout());

this.setBorder(BorderFactory.createRaisedBevelBorder());

this.setMaximumSize(new Dimension(200, 200));

this.setMinimumSize(new Dimension(200, 200));

this.setPreferredSize(new Dimension(200, 200));

Main.setLayout(new BorderLayout());

Main.setBackground(SystemColor.info);

Main.setBorder(null);

Out.setBackground(Color.lightGray);

Out.setHorizontalAlignment(SwingConstants.CENTER);

Out.setMaximumSize(new Dimension(100, 19));

Out.setMinimumSize(new Dimension(100, 19));

Out.setPreferredSize(new Dimension(100, 19));

jLabel1.setForeground(Color.red);

jLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel1.setText(“日”);

jLabel2.setForeground(Color.blue);

jLabel2.setHorizontalAlignment(SwingConstants.CENTER);

jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel2.setText(“六”);

jLabel3.setHorizontalAlignment(SwingConstants.CENTER);

jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel3.setText(“五”);

jLabel4.setHorizontalAlignment(SwingConstants.CENTER);

jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel4.setText(“四”);

jLabel5.setHorizontalAlignment(SwingConstants.CENTER);

jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel5.setText(“三”);

jLabel6.setBorder(null);

jLabel6.setHorizontalAlignment(SwingConstants.CENTER);

jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel6.setText(“二”);

jLabel7.setBackground(Color.lightGray);

jLabel7.setForeground(Color.black);

jLabel7.setBorder(null);

jLabel7.setHorizontalAlignment(SwingConstants.CENTER);

jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel7.setText(“一”);

weekPanel.setBackground(UIManager.getColor(“InternalFrame.activeTitleGradient”));

weekPanel.setBorder(BorderFactory.createEtchedBorder());

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

weekPanel.add(jLabel1, null);

weekPanel.add(jLabel7, null);

weekPanel.add(jLabel6, null);

weekPanel.add(jLabel5, null);

weekPanel.add(jLabel4, null);

weekPanel.add(jLabel3, null);

weekPanel.add(jLabel2, null);

MonthUp.setAlignmentX((float) 0.0);

MonthUp.setActionMap(null);

jPanelMonth.setBackground(SystemColor.info);

jPanelMonth.setLayout(new BorderLayout());

jPanelMonth.setBorder(BorderFactory.createEtchedBorder());

Month.setBorder(null);

Month.setHorizontalAlignment(SwingConstants.CENTER);

Month.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Month_mouseClicked(e);

}

});

Month.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Month_keyPressed(e);

}

});

MonthDown.setBorder(null);

MonthDown.setText(“\u25C4”);

MonthDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthDown_actionPerformed(e);

}

});

MonthUp.setBorder(null);

MonthUp.setText(“\u25BA”);

MonthUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthUp_actionPerformed(e);

}

});

jPanelButton.setLayout(null);

jPanelButton.setBorder(null);

jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {

public void componentResized(java.awt.event.ComponentEvent evt) {

jPanelButtonComponentResized(evt);

}

});

Year.setBorder(BorderFactory.createEtchedBorder());

Year.setMaximumSize(new Dimension(80, 25));

Year.setMinimumSize(new Dimension(80, 25));

Year.setPreferredSize(new Dimension(80, 25));

Year.setHorizontalAlignment(SwingConstants.CENTER);

Year.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Year_mouseClicked(e);

}

});

Year.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Year_keyPressed(e);

}

});

YearDown.setBorder(null);

YearDown.setMaximumSize(new Dimension(16, 16));

YearDown.setMinimumSize(new Dimension(16, 16));

YearDown.setPreferredSize(new Dimension(16, 16));

YearDown.setSize(new Dimension(16, 16));

YearDown.setText(“▼”);

YearDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearDown_actionPerformed(e);

}

});

YearUp.setBorder(null);

YearUp.setMaximumSize(new Dimension(16, 16));

YearUp.setMinimumSize(new Dimension(16, 16));

YearUp.setPreferredSize(new Dimension(16, 16));

YearUp.setSize(new Dimension(16, 16));

YearUp.setText(“▲”);

YearUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearUp_actionPerformed(e);

}

});

jPanelDay.setLayout(new BorderLayout());

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

Days.setBackground(SystemColor.info);

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

days[i]=new JToggleButton();

days[i].setBorder(null);

days[i].setBackground(SystemColor.info);

days[i].setHorizontalAlignment(SwingConstants.CENTER);

days[i].setHorizontalTextPosition(SwingConstants.CENTER);

//days[i].setSize(l,l);

days[i].addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(ActionEvent e) {

day=Integer.parseInt(((JToggleButton)e.getSource()).getText());

showDate();

showDays();

}

});

Days.add(days[i]);

}

this.add(Main, BorderLayout.NORTH);

this.add(jPanelDay, BorderLayout.CENTER);

this.add(jPanelMonth, BorderLayout.SOUTH);

Main.add(Year, BorderLayout.CENTER);

Main.add(Out, BorderLayout.WEST);

Main.add(jPanelButton, BorderLayout.EAST);

jPanelButton.add(YearUp);

jPanelButton.add(YearDown);

jPanelDay.add(weekPanel,BorderLayout.NORTH);

jPanelDay.add(Days, BorderLayout.CENTER);

jPanelMonth.add(Month, BorderLayout.CENTER);

jPanelMonth.add(MonthDown, BorderLayout.WEST);

jPanelMonth.add(MonthUp, BorderLayout.EAST);

showMonth();

showYear();

showDate();

showDays();

}

//自定義重畫年選擇面板

void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){

YearUp.setLocation(0,0);

YearDown.setLocation(0,YearUp.getHeight());

jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);

jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));

jPanelButton.updateUI();

}

//測試用

public static void main(String[] args){

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

//增加年份

void YearUp_actionPerformed(ActionEvent e) {

year++;

showYear();

showDate();

showDays();

}

//減少年份

void YearDown_actionPerformed(ActionEvent e) {

year–;

showYear();

showDate();

showDays();

}

//減少月份

void MonthDown_actionPerformed(ActionEvent e) {

month–;

if(month0) {

month = 11;

year–;

showYear();

}

showMonth();

showDate();

showDays();

}

//增加月份

void MonthUp_actionPerformed(ActionEvent e) {

month++;

if(month==12) {

month=0;

year++;

showYear();

}

showMonth();

showDate();

showDays();

}

//初始化年月日

void iniCalender(){

year=cal.get(Calendar.YEAR);

month=cal.get(Calendar.MONTH);

day=cal.get(Calendar.DAY_OF_MONTH);

}

//刷新月份

void showMonth(){

Month.setText(Integer.toString(month+1)+”月”);

}

//刷新年份

void showYear(){

Year.setText(Integer.toString(year)+”年”);

}

//刷新日期

void showDate(){

Out.setText(Integer.toString(year)+”-“+Integer.toString(month+1)+”-“+Integer.toString(day));

}

//重畫天數選擇面板

void showDays() {

cal.set(year,month,1);

int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

int n=mm[month];

if(cal.isLeapYear(year)month==1) n++;

int i=0;

for(;ifirstDayOfWeek-1;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText(“”);

}

int d=1;

for(;d=n;d++){

days[i].setText(Integer.toString(d));

days[i].setEnabled(true);

if(d==day) days[i].setSelected(true);

else days[i].setSelected(false);;

i++;

}

for(;i42;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText(“”);

}

}

//單擊年份面板選擇整個年份字符串

void SelectionYear(){

Year.setSelectionStart(0);

Year.setSelectionEnd(Year.getText().length());

}

//單擊月份面板選擇整個月份字符串

void SelectionMonth(){

Month.setSelectionStart(0);

Month.setSelectionEnd(Month.getText().length());

}

//月份面板響應鼠標單擊事件

void Month_mouseClicked(MouseEvent e) {

//SelectionMonth();

inputMonth();

}

//檢驗輸入的月份

void inputMonth(){

String s;

if(Month.getText().endsWith(“月”))

{

s=Month.getText().substring(0,Month.getText().length()-1);

}

else s=Month.getText();

month=Integer.parseInt(s)-1;

this.showMe();

}

//月份面板鍵盤敲擊事件響應

void Month_keyPressed(KeyEvent e) {

if(e.getKeyChar()==10)

inputMonth();

}

//年份面板響應鼠標單擊事件

void Year_mouseClicked(MouseEvent e) {

//SelectionYear();

inputYear();

}

//年份鍵盤敲擊事件響應

void Year_keyPressed(KeyEvent e) {

//System.out.print(new Integer(e.getKeyChar()).byteValue());

if(e.getKeyChar()==10)

inputYear();

}

//檢驗輸入的年份字符串

void inputYear() {

String s;

if(Year.getText().endsWith(“年”))

{

s=Year.getText().substring(0,Year.getText().length()-1);

}

else s=Year.getText();

year=Integer.parseInt(s);

this.showMe();

}

//以字符串形式返回日期,yyyy-mm-dd

public String getDate(){return Out.getText();}

//以字符串形式輸入日期,yyyy-mm-dd

public void setDate(String date){

if(date!=null){

StringTokenizer f = new StringTokenizer(date, “-“);

if(f.hasMoreTokens())

year = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

month = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

day = Integer.parseInt(f.nextToken());

cal.set(year,month,day);

}

this.showMe();

}

//以日期對象形式輸入日期

public void setTime(Date date){

cal.setTime(date);

this.iniCalender();

this.showMe();

}

//返回日期對象

public Date getTime(){return cal.getTime();}

//返回當前的日

public int getDay() {

return day;

}

//設置當前的日

public void setDay(int day) {

this.day = day;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//設置當前的年

public void setYear(int year) {

this.year = year;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//返回當前的年

public int getYear() {

return year;

}

//返回當前的月

public int getMonth() {

return month;

}

//設置當前的月

public void setMonth(int month) {

this.month = month;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//刷新

public void showMe(){

this.showDays();

this.showMonth();

this.showYear();

this.showDate();

}

}

public class TestJCalendar {

public static void main(String[] args) {

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

}

用java(用calendar類)寫一個萬年曆,輸入年並且顯示當年的日曆

public class MyCalendar {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        Calendar c = Calendar.getInstance();

        System.out.println(“請輸入數字年:(比如 2014)”);

        int year = sc.nextInt();

        sc.close();

//      int year = 2014;

        c.set(Calendar.YEAR,year);

        

        

        for (int i = 0; i  12; i++) {

         c.set(Calendar.MONTH,i);  //

         c.set(Calendar.DATE,1);  //設置成1月

         printMonth(c);

        

    }

    

    public static void printMonth(Calendar c){

     c.set(Calendar.DAY_OF_MONTH,1);   //設置成一日

     System.out.printf(“\n\n=========  %s  月  =========\n”,c.get(Calendar.MONTH)+1);

        String[] weeks = { “日”, “一” ,  “二”,  “三”,  “四”,  “五”,  “六” };

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

            System.out.printf(“%s” + (i != 6 ?”\t”:”\n”),weeks[i]);

        }

         

        int offday = c.get(Calendar.DAY_OF_WEEK) – 1;

        

        for(int i = 0; i  offday; i++){

            System.out.printf(“\t”);

        }

         

        int month = c.get(Calendar.MONTH);

        while(c.get(Calendar.MONTH) == month ){

            System.out.printf(“%d” + ( (c.get(Calendar.DAY_OF_WEEK)) != 7 ? “\t”:”\n”) ,c.get(Calendar.DAY_OF_MONTH));

            c.add(Calendar.DAY_OF_MONTH, 1);

        }

    }

}

如何用JAVA編寫出一個簡單的日曆

import javax.swing.JOptionPane;

public class NewClass{

public static void main(String[] args){

int year,month;

Calender cal=new Calender(2008,10);

cal.showCalender();

year=Integer.parseInt(JOptionPane.showInputDialog(“Year:”));

month=Integer.parseInt(JOptionPane.showInputDialog(“Month:”));

cal.setYear(year);

cal.setMonth(month);

cal.showCalender();

}

}

class Calender{

private int year,month;

public Calender(){

year=0;

month=1;

}

public Calender(int year){

this.year=year;

month=1;

}

public Calender(int year,int month){

this.year=year;

if(month12)

this.month=month%12;

else

this.month=month;

}

public void setYear(int year){

this.year=year;

}

public void setMonth(int month){

if(month12)

this.month=month%12;

else

this.month=month;

}

private int dayOfMonth(){

int days=0;

switch(month){

case 1:days=31;break;

case 2:{

if(((year%4==0)(year%100!=0))||(year%400==0))

days=29;

else

days=28;

break;

}

case 3:days=31;break;

case 4:days=30;break;

case 5:days=31;break;

case 6:days=30;break;

case 7:days=31;break;

case 8:days=31;break;

case 9:days=30;break;

case 10:days=31;break;

case 11:days=30;break;

case 12:days=31;break;

default:

days=0;

}

return days;

}

private int dayOfWeek(){

int Y=year;

int M=month;

int D=1;

int A;

A = Y0?(5+(Y+1)+(Y-1)/4-(Y-1)/100+(Y-1)/400)%7:(5+Y+Y/4-Y/100+Y/400)%7;

A = M2?(A+2*(M+1)+3*(M+1)/5)%7:(A+2*(M+2)+3*(M+2)/5)%7;

if (((Y%4 == 0 Y%100 != 0)|| Y%400 == 0) M2) A =(A+1)%7;

A=(A+D)%7;

return A;

}

public void showCalender(){

String str=new String();

str=” “;

str+=year+”年”+month+”月”;

str+=”\n\n”;

str+=”日 一 二 三 四 五 六\n”;

int week=this.dayOfWeek();

for(int i=0,j=1;i7;i++){

if(iweek)

str+=” “;

else{

str+=” “+j+” “;

j++;

}

}

str+=”\n”;

end:

for(int i=7-week+1;i=this.dayOfMonth();){

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

if(i10)

str+=” “+i+” “;

else

str+=i+” “;

i++;

if(ithis.dayOfMonth())

break end;

}

str+=”\n”;

}

JOptionPane.showMessageDialog(null,str);

}

}

用一個類來實現

如何用JAVA寫日曆?

按照你的要求編寫的Java日曆驗證程序如下

UI.java

import java.util.Scanner;

public class UI {

 static Scanner sc=new Scanner(System.in);

 public static int askInt(String s){

  System.out.print(s);

  return sc.nextInt();

 }

 public static void println(String s){

  System.out.println(s);

 }

}

EE.java

public class EE {

 public void validateDateCore(){

  int year =UI.askInt(“Enter the year: “);

  int month=UI.askInt(“Enter the month: “);

  int day=UI.askInt(“Enter the day: “);

  if(year  1){

   UI.println(“The year is not a valid number.”);

   return;

  }

  if(month1 || month12){

   UI.println(“The month is not a valid number.”);

   return;

  }

  int monthDay=0;

  switch(month){

   case 1:

   case 3:

   case 5:

   case 7:

   case 8:

   case 10:

   case 12:monthDay=31;break;

   case 4:

   case 6:

   case 9:

   case 11:monthDay=30;break;

   case 2:

    if((year%4==0  year%100!=0) || year%400==0){

     monthDay=29;

    }else{

     monthDay=28;

    }

    break;

  }

  if(day1 || daymonthDay){

   UI.println(“The day is not a valid number.”);

   return;

  }else{

   UI.println(“It is “+day+”/”+month+”/”+year+”.”);

  }

 }

 public static void main(String[] args) {

  new EE().validateDateCore();

 }

}

運行結果

用java編寫一個2013全年日曆

下面是一個帶界面的java日曆。可以指定任意年月。默認為2013年1月

import java.awt.*;

import java.awt.event.*;

import java.util.Calendar;

import javax.swing.JOptionPane;

class CalendarBean

{

String day[];

int year=2011,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;

}

}

class CalendarFrame extends Frame implements ActionListener

{

Label labelDay[]=new Label[42];

Button titleName[]=new Button[7];

String name[]={“日”,”一”,”二”,”三”, “四”,”五”,”六”};

TextField text1,text2;Button nextMonth,previousMonth,Enter;Label lab1,lab2,lab3;

int year=2013,month=1;

CalendarBean calendar;

Label showMessage=new Label(“”,Label.CENTER);

public CalendarFrame()

{ Panel pCenter=new Panel();

pCenter.setLayout(new GridLayout(7,7));

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

{ titleName[i]=new Button(name[i]);

pCenter.add(titleName[i]);

}

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

{

labelDay[i]=new Label(“”,Label.CENTER);

pCenter.add(labelDay[i]);

}

calendar=new CalendarBean();

calendar.setYear(year);

calendar.setMonth(month);

String day[]=calendar.getCalendar();

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

{ labelDay[i].setText(day[i]);

}

lab1=new Label(“請輸入日期”);

lab2=new Label(“年份”);

lab3=new Label(“月份”);

Enter=new Button(“確定”);

text1=new TextField(10);

text2=new TextField(5);

nextMonth=new Button(“下月”);

previousMonth=new Button(“上月”);

Enter.addActionListener(this);

nextMonth.addActionListener(this);

previousMonth.addActionListener(this);

Panel pNorth=new Panel(),

pSouth=new Panel();

pNorth.add( lab1);

pNorth.add(lab2);

pNorth.add( text1);

pNorth.add(lab3);

pNorth.add(text2);

pNorth.add(Enter);

pNorth.add(previousMonth);

pNorth.add(nextMonth);

pSouth.add(showMessage);

showMessage.setText(“日曆:”+calendar.getYear()+”年”+ calendar.getMonth()+”月” );

ScrollPane scrollPane=new ScrollPane();

scrollPane.add(pCenter);

add(scrollPane,BorderLayout.CENTER);

add(pNorth ,BorderLayout.NORTH);

add(pSouth ,BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent e)

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

{ month=month+1;

if(month12)

month=1;

calendar.setMonth(month);

String day[]=calendar.getCalendar();

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

{ labelDay[i].setText(day[i]);

}

}

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

{ month=month-1;

if(month1)

month=12;

calendar.setMonth(month);

String day[]=calendar.getCalendar();

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

{ labelDay[i].setText(day[i]);

}

}else {

String yea=text1.getText();

String mon=text2.getText();

try{

year=Integer.parseInt(yea);

month=Integer.parseInt(mon);

if(month12||month1||year1){

JOptionPane.showMessageDialog(null, “請輸入正確月份或月份”);

return;

}

else{

calendar.setYear(year);

calendar.setMonth(month);

}

String day[]=calendar.getCalendar();

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

{

labelDay[i].setText(day[i]);

}

}catch(NumberFormatException ee){

JOptionPane.showMessageDialog(null, “請輸入正確的年份及月份”);

}

}

showMessage.setText(“日曆:”+calendar.getYear()+”年”+calendar.getMonth()+”月” );

}

}

public class CalendarMainClass

{ public static void main(String args[])

{ CalendarFrame frame=new CalendarFrame();

frame.setTitle(“日曆”);

frame.setBounds(300,200,500,300);

frame.setVisible(true);

frame.validate();

frame.addWindowListener(new java.awt.event.WindowAdapter()

{ public void windowClosing(java.awt.event.WindowEvent e)

{ System.exit(0);

}

}

);

}

}

有問題就追問。滿意請採納。

Java編寫程序,輸入年份,輸出本年度各月份日曆

寫了個簡明的,

import java.util.Calendar;

import java.util.Scanner;

public class Test {

static public void main(String 參數[]){

Calendar c = Calendar.getInstance();

Scanner sc = new Scanner(System.in);

System.out.println(“請輸入年份:”);

int year= sc.nextInt();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, Calendar.JANUARY);

c.set(Calendar.DAY_OF_MONTH, 1);

while(c.get(Calendar.YEAR)==year){

int wday=c.get(Calendar.DAY_OF_WEEK);

int mday=c.get(Calendar.DAY_OF_MONTH);

if(mday==1){

System.out.println(“\n日\t一\t二\t三\t四\t五\t六\t第”+(c.get(Calendar.MONTH)+1)+”月”);

System.out.println(“—————————————————“);

for(int i=0;iwday-1;i++) System.out.print(” \t”);

}

System.out.print(mday+”\t”);

if(wday==7) System.out.println();

c.add(Calendar.DAY_OF_YEAR, 1);

}

}

}

=======

請輸入年份:

2012

日 一 二 三 四 五 六 第1月

—————————————————

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

日 一 二 三 四 五 六 第2月

—————————————————

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

日 一 二 三 四 五 六 第3月

—————————————————

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

日 一 二 三 四 五 六 第4月

—————————————————

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

日 一 二 三 四 五 六 第5月

—————————————————

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

日 一 二 三 四 五 六 第6月

—————————————————

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

日 一 二 三 四 五 六 第7月

—————————————————

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

日 一 二 三 四 五 六 第8月

—————————————————

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

日 一 二 三 四 五 六 第9月

—————————————————

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

日 一 二 三 四 五 六 第10月

—————————————————

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

日 一 二 三 四 五 六 第11月

—————————————————

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

日 一 二 三 四 五 六 第12月

—————————————————

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

原創文章,作者:W4TBM,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130852.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
W4TBM的頭像W4TBM
上一篇 2024-10-03 23:27
下一篇 2024-10-03 23:27

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論