カレンダー


[前月][次月]ボタンで表示月を1つずつ移動できる。

右欄で,表示したい年と月を入力し,[変更]ボタンで移動できる。
[今月に戻る]ボタンで,現在月に戻る。

カレンダーApplet

ソースコード

import java.applet.*;
import java.awt.*;
import java.util.*;

public class ja03f extends Applet
{
  Date nowDate;     	//現在の日付
  Date showDate;   	//カレンダーに表示する日付
  int yr,mt;      	//年と月の数値
  int w,h;        	//カレンダーの幅と高さ
  String week[] = {"日","月","火","水","木","金","土"};	//カレンダーに表示する曜日の文字列配列
  String monthName[] = {"1月 January","2月 February","3月 March","4月 April","5月 May","6月 June",
    "7月 July","8月 August","9月 September","10月 October","11月 November","12月 December"};

  Label la1,la2,la3;  		//ラベル
  TextField tf1,tf2;  		//テキストフィールド
  Button bt1,bt2;  		//ボタン

  public void init(){
    setBackground(Color.black);	//Appletの背景色を黒色に
    w = size().width;   	//Appletの幅を取得
    h = size().height;  	//Appletの高さを取得
    if(w < h)h = w;     	//Appletのサイズに応じて,カレンダーが最大正方形になるように調整
    else w = h;

    setLayout(null);  		//レイアウトマネージャーを使わない
    Panel p = new Panel();  	//パネルを作成し,登録する
    add(p);
    p.reshape(5,5,120,25); 	//パネルを指定位置に配置
    p.add(new Button("前月"));  //パネルにボタンを2つ配置
    p.add(new Button("次月"));

    nowDate = new Date();   	//現在の日付を取得
    yr = nowDate.getYear();  	//年の数値を取得
    mt = nowDate.getMonth(); 	//月の数値を取得
    showDate = new Date(yr,mt,1);  //カレンダーに表示する日付(月初めの1日を設定)

    //以下は,カレンダーの右横に配置するコンポーネント
    setForeground(Color.white); //前景色を白に
    la1 =new Label("表示したい年月を入力");
    add(la1);
    la1.reshape(310,50,130,30);
    tf1 = new TextField(4); 	//年を入力するTextField
    add(tf1);
    tf1.reshape(330,80,50,20);
    la2 =new Label("年");
    add(la2);
    la2.reshape(400,80,20,20);
    tf2 = new TextField(2); 	//月を入力するTextField
    add(tf2);
    tf2.reshape(330,110,30,20);
    la3 =new Label("月");
    add(la3);
    la3.reshape(370,110,20,20);
    bt1 = new Button("変更");
    add(bt1);
    bt1.reshape(330,150,50,20);
    bt2 = new Button("今月に戻す");
    add(bt2);
    bt2.reshape(330,180,100,20);
  }

  public void paint(Graphics g){
    g.setColor(Color.orange); 	//カレンダー部分をオレンジ色で塗り潰す
    g.fillRect(0,0,w,h);

    Font f = new Font("TimesRoman",Font.BOLD,h/16);//フォントの設定(大きさをカレンダーの高さの1/16に)
    g.setFont(f);
    g.setColor(Color.blue);
    g.drawString((1900 +showDate.getYear()) + "年:" + monthName[showDate.getMonth()],60,h/6);
          //カレンダーの見出しに表示する年と月(monthName配列から文字列を得る)

    g.setColor(Color.black);
    for(int i = 2;i < 9;i++){   	//横線を描画
      int y = (h * i)/ 8;   		//横線に位置を,カレンダーの高さを8等分して計算
      g.drawLine(0,y,w,y);
    }
    for(int i = 0;i < 8;i++){   	//縦線を描画
      int x = (w * i)/7;      		//縦線の位置を,カレンダーの幅を7等分して計算
      g.drawLine(x,h/4,x,h);
    }

    g.setColor(Color.red);
    for(int i = 0;i < 7; i++){  	//日〜土曜日の記入
      g.drawString(week[i],(w * i) / 7 + 6,h / 4 - 2);  //6と2 は,少しズラスための調整値
    }

    g.setColor(Color.white);
    int day = showDate.getDay();  	//getDayメソッドは曜日を0(日)〜6(土)の数値で返す
    int totalDay; 			//その月の合計日数
    int sm = showDate.getMonth(); 	//カレンダーに表示する月を得る(0〜11の数値)
    if(sm == 3 || sm == 5 || sm == 8 ||sm == 10)totalDay = 30;
          //4,6,9,11月の場合,日数は30日
    else if(sm == 1){   		//2月の場合,うるう年を計算
      if(showDate.getYear() % 4 != 0)totalDay = 28;
      else if(showDate.getYear() % 100 != 0)totalDay = 29;
      else if(showDate.getYear() % 400 != 0)totalDay = 28;
      else totalDay = 29;
    }else{totalDay = 31;} 		//その他の月は31日
    
    int y = (h * 5) / 16;
    for(int i = 1;i <=totalDay;i++){  	//日付をカレンダーに描画
      g.drawString("" + i,(w *day) / 7 + 5,y);
      if(++day > 6){  		//(前置きインクリメント)dayの値を1増やしてから条件を調べる
        day = 0;      		//dayが6(土曜日)より大きくなったら,0(日曜日)に
        y += h / 8;   		//描画のy座標を下に移動
      }
    }
  }

  public boolean action(Event e,Object o){
    if(o.equals("前月")){     	//[前月]ボタンがクリックされた場合
      if(--mt < 0){  		//(前置きインクリメント)mtの値を1減らしてから条件を調べる
        mt = 11; 		//mtが0より小さくなったら,mtを11(12月)にし,yr(年)を1減らす
        yr--;
      }
    }else if(o.equals("次月")){          //[次月]ボタンがクリックされた場合
       if(++mt > 11){  		//(前置きインクリメント)mtの値を1増やしてから条件を調べる
          mt = 0;  		//mtが11(12月)より大きくなったら,mtを0(1月)にし,yr(年)を1増やす
          yr++;
       }
    }else if(o.equals("変更")){
      int tfy = Integer.parseInt(tf1.getText());
      yr = tfy - 1900;
      int tfm = Integer.parseInt(tf2.getText());
      mt = tfm - 1;
    }else if(o.equals("今月に戻す")){
      int nowy = nowDate.getYear();  //現在の年と月を取得
      yr = nowy;
      int nowm = nowDate.getMonth();
      mt = nowm;
    }

    showDate = new Date(yr,mt,1); //カレンダーに表示する日付を再設定
    repaint();   		//再設定した日付によりカレンダーを再描画

    return true;
  }
}

末尾