さて、先日書いたカレンダーアプリの中身を書きたいと思います。
まだ、ソースが汚いままですが・・
ヴァージョン1.3になってます。
画面の再掲
このカレンダーは
CalendarTest クラス
Daily クラス
Todo クラス
の3つのクラスを自分で作成し、CalendarTest クラスの中にmainメソッドを作成した形で作りました。
Daily クラス
Todo クラス
の2つは、extendsでCalendarTest クラスを継承させています。
以下、かなり長くなりますが・・・各クラスごとのソースです。
本当に長くなったんで、どっかにファイルをUPしたいけど、どこかいいとこあるかな?
次回から個別にも見ていこうと思います。
◆◇◆◇◆◇◆◇◆◇CalendarTestクラス◆◇◆◇◆◇◆◇◆◇
package calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import javax.sound.sampled.ReverbType;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.CloseAction;
import javax.swing.text.Document;
public class CalendarTest extends JFrame implements ActionListener,
WindowListener {
CalendarTest() {
}
/*
* カレンダーの考え方のメモ getで今日を求めれる。setで任意に設定できる。 月の初めの曜日を求めます。 calendar.set(year,
* month - 1, 1); // 引数: 1月: 0, 2月: 1, ... startDay =
* calendar.get(Calendar.DAY_OF_WEEK); 月末の日付を求めます。
* calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DATE, -1);
* lastDate = calendar.get(Calendar.DATE);
*/
JPanel calendarpane, backpane;
JButton dayhozon;
Daily daily;
Todo todo;
// 月間カレンダーの作成時
File yeardir;
File monthdir;
File daysfile;
static int a;// セットする年
static int b;
static int c = 1;
static int state = 0;
// 起動時
static File HMCalendar;
{
// セットする月
this.setSize(1280, 960);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.addWindowListener(this);
this.addComponentListener(null);
}
public CalendarTest(String title) { // コンストラクタでsyoriメソッドを呼び出している。
// syori();
}
public void syori() {// syoriメソッドを作成してフレームの中身だけの処理とフレームを分けた。
String[] dayweek = { "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日" };
// JPanel backpane = new JPanel(new BorderLayout()); // 背景用・・一番後ろのパネル
calendarpane = new JPanel(new BorderLayout()); // 背景用・・一番後ろのパネル
backpane = new JPanel(new BorderLayout()); // 背景用・・月間カレンダーの一番後ろのパネル
this.add(calendarpane, BorderLayout.CENTER);
calendarpane.add(backpane, BorderLayout.CENTER);
JPanel infopane = new JPanel(new BorderLayout()); // 年・次の月などや曜日を記入するようのパネル
JPanel daybackpane = new JPanel(new GridLayout(0, 7)); // 日付用のバックパネル
backpane.add(infopane, BorderLayout.NORTH);
backpane.add(daybackpane, BorderLayout.CENTER);
int setyear = a; // セットする年
int setmonth = b; // セットする月
int setday = c; // セットする日にち
Calendar calendar = Calendar.getInstance();
calendar.set(setyear, setmonth - 1, 1); // 日付のセット(月初めの日付)
int year = calendar.get(Calendar.YEAR);// ↑でセットされた年を読み込む
int dayofweek = calendar.get(Calendar.DAY_OF_WEEK); // 曜日の数字。
// このフィールドの値は1から7で、SUNDAY(1)、MONDAY(2)、TUESDAY(3)、WEDNESDAY(4)、THURSDAY(5)、FRIDAY(6)、およびSATURDAY(7)になります。
// calendar.setで月初めの日付をゲットしているので、この曜日は必ず月初めの曜日となる。
// 各ラベル・パネルの作成
JLabel yearlabel = new JLabel(String.valueOf(year) + "年"
+ String.valueOf(setmonth) + "月のカレンダー");
yearlabel.setHorizontalAlignment(JLabel.CENTER);// ラベルの文字を中央に配置
infopane.add(yearlabel, BorderLayout.CENTER);// セットした年をinfopaneのCENTERに貼り付けている
if (setmonth == 12)
setmonth = 0;
JButton nextmonth = new JButton(String.valueOf(setmonth + 1 + "月"));// 月間カレンダーの次の月へのボタン
nextmonth.addActionListener(this);
nextmonth.setActionCommand("next");
infopane.add(nextmonth, BorderLayout.EAST);
if (setmonth == 1)
setmonth = 13;
else if (setmonth == 0)
setmonth = 12;
JButton backmonth = new JButton(String.valueOf(setmonth - 1 + "月"));// 月間カレンダーの前の月へのボタン
backmonth.addActionListener(this);
backmonth.setActionCommand("back");
infopane.add(backmonth, BorderLayout.WEST);
JPanel dayname = new JPanel(new GridLayout(0, 7));
infopane.add(dayname, BorderLayout.SOUTH);// 曜日のボタンを配置するためのパネルをinfopaneのSOUTHに貼り付けている
EtchedBorder border = new EtchedBorder(EtchedBorder.RAISED,
Color.white, Color.black);// Borderクラスを使ってボーダーを作成。
// ボタンに曜日を入れて作成。配列dayweekの値を入れている
for (int r = 0; r < 7; r++) {
JButton daynamebtn = new JButton(dayweek[r]);
dayname.add(daynamebtn);
}
for (int j = 1; j < 2; j++) {// つきの値は0-11だからjの値も1で初期化すること
int month = calendar.get(Calendar.MONTH) + j;// 月の値を取る 0-11で入る
int monthdays = 0;
if (month == 2) {
monthdays = 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
monthdays = 30;
} else {
monthdays = 31;
}
// 前月の終わりを取得して作成
calendar.add(Calendar.DATE, -(dayofweek - 1));
int bday = calendar.get(Calendar.DATE);
int bmonth = calendar.get(Calendar.MONTH) + 1;
for (int k = 1; k < dayofweek; k++) { // つき始めの曜日まで空パネルを配置する。
JPanel karapane = new JPanel(new BorderLayout());
daybackpane.add(karapane);
JButton karabtn = new JButton(String.valueOf(bmonth + "/"
+ (bday + (k - 1))));
karapane.add(karabtn, BorderLayout.NORTH);
JTextArea karamemo = new JTextArea(); // ここはテキストエリアを使ったほうがいいのかな?
karamemo.setEditable(false);
karamemo.setBorder(border);// TextAreaは枠がないので、setBorderで枠を作成。
karamemo.setBackground(Color.LIGHT_GRAY);
karamemo.setOpaque(true);
karapane.add(karamemo, BorderLayout.CENTER);
}
// ///月間カレンダーの本体部分/////
calendar.set(setyear, setmonth - 1, 1); // カレンダーの初期化。日付のセット(月初めの日付)
for (int i = 0; i < monthdays; i++) {
int date = calendar.get(Calendar.DATE) + i;
// monthのフィールドの値は0から11
JPanel daypane = new JPanel(new BorderLayout());// 日付用のパネル
daybackpane.add(daypane);
JButton btn = new JButton(String.valueOf(month + "/" + date));
btn.addActionListener(this);
btn.setActionCommand(String.valueOf(date));
daypane.add(btn, BorderLayout.NORTH);
daysfile = new File("C:\\HMCalendar\\" + String.valueOf(a)
+ "\\" + String.valueOf(b) + "\\"
+ String.valueOf(date) + ".txt");
String key = "", setkey = key;
try {
BufferedReader br = new BufferedReader(new FileReader(
daysfile));
while ((key = br.readLine()) != null) {
if (setkey == null) {
setkey = key;
} else {
setkey = setkey + "\n" + key;
// System.out.println("strs="+strs);
}
}
br.close();
} catch (FileNotFoundException e) {
// System.out.println("ファイルが見つかりません");
// System.out.println(daysfile);
// System.out.println("a=" + a + " b=" + b + " c=" + c);
} catch (IOException e) {
System.out.println("エラーです");
}
JTextArea memo = new JTextArea(setkey);
memo.setEditable(false);
memo.setBorder(border);
memo.setBackground(Color.white);
memo.setOpaque(true);
daypane.add(memo, BorderLayout.CENTER);
}
// 月末の曜日を求めてdayofweekに代入する
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DATE, -1);
dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
// 次の月の日にちを取得する
calendar.add(Calendar.DATE, 1);
int nday = calendar.get(Calendar.DATE);
int nmonth = calendar.get(Calendar.MONTH) + 1;
for (int i = 0; i < 7 - dayofweek; i++) {// 月末の曜日から空パネルを配置する。
JPanel karapane = new JPanel(new BorderLayout());
daybackpane.add(karapane);
JButton karabtn = new JButton(String.valueOf(nmonth + "/"
+ (nday + i)));
karapane.add(karabtn, BorderLayout.NORTH);
JTextArea karamemo = new JTextArea(); // ここはテキストエリアを使ったほうがいいのかな?
karamemo.setEditable(false);
karamemo.setBorder(border);// TextAreaは枠がないので、setBorderで枠を作成。
karamemo.setBackground(Color.LIGHT_GRAY);
karamemo.setOpaque(true);
karapane.add(karamemo, BorderLayout.CENTER);
}
}
// System.out.println("year" + setyear + "" + "month" + setmonth);
System.out.println("a=" + a + " b=" + b + " c=" + c);
if (state == 1) {
dailysyori();
} else {
yeardir();
}
this.setVisible(true);
}
void dailysyori() {
daily = new Daily();
daily.dailydir();
calendarpane.add(daily.dayback, BorderLayout.SOUTH);
todosyori();
}
void todosyori() {
todo = new Todo();
todo.todofile();
calendarpane.add(todo.todoback, BorderLayout.EAST);
dayhozon = new JButton("保存する");
dayhozon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
restart();
}
});
daily.daybackinfo.add(dayhozon, BorderLayout.EAST);
}
void yeardir() {
yeardir = new File("C:\\HMCalendar\\" + String.valueOf(a));
if (yeardir.exists()) {
} else {
yeardir.mkdir();
}
monthdir();
}
void monthdir() {
monthdir = new File("C:\\HMCalendar\\" + String.valueOf(a) + "\\"
+ String.valueOf(b));
if (monthdir.exists()) {
} else {
monthdir.mkdir();
}
daysfile();
}
void daysfile() {
daysfile = new File("C:\\HMCalendar\\" + String.valueOf(a) + "\\"
+ String.valueOf(b) + "\\" + String.valueOf(c) + ".txt");
if (daysfile.exists()) {
} else {
try {
daysfile.createNewFile();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
dailysyori();
}
public static void main(String[] args) {
// 現在の年月日を取得してa,b,cに代入
Calendar calendar = Calendar.getInstance();
a = calendar.get(Calendar.YEAR);
b = calendar.get(Calendar.MONTH) + 1;
c = calendar.get(Calendar.DATE);
HMCalendar = new File("C:\\HMCalendar");
if (HMCalendar.exists()) {
// System.out.println("ファイルが見つかりました");
} else {
// System.out.println("ファイルが見つかりません、作成します。");
HMCalendar.mkdir();
}
CalendarTest calendars = new CalendarTest("カレンダー");
calendars.syori();
}
void restart() {
daily.monthkeyhozonsyori();
daily.hozonsyori();
todo.hozonsyori();
calendarpane.removeAll();
syori();
}
public void actionPerformed(ActionEvent e) {
String btncmd = e.getActionCommand();
System.out.println(btncmd);
if (btncmd.equals("next")) { // nextボタンが押されたときの処理
if (b == 12) { // 12月なら年に+1して1月に戻す。
a = a + 1;
b = 1;
} else {
b = b + 1;
}
restart();
System.out.println("if next");
} else if (btncmd.equals("back")) {
if (b == 1) { // 1月なら年に-1して12月に戻す
a = a - 1;
b = 12;
} else {
b = b - 1;
}
restart();
} else if (Integer.parseInt(btncmd) != 0
&& Integer.parseInt(btncmd) <= 31) {
System.out.println(b + "/" + btncmd + "のボタンが押されました");
c = Integer.parseInt(btncmd);
System.out.println(c);
restart();
}
}
void componentResized(ComponentEvent e) {
}
void componentMoved(ComponentEvent e) {
}
// ウィンドウが最小化から戻るとき
public void windowDeiconified(WindowEvent e) {
this.removeAll();
dispose();
CalendarTest calendars = new CalendarTest("カレンダー");
calendars.syori();
}
public void windowActivated(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
daily.monthkeyhozonsyori();
daily.hozonsyori();
todo.hozonsyori();
}
public void windowOpened(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
// ウィンドウが最小化されるとき
public void windowIconified(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
}
◆◇◆◇◆◇◆◇◆◇Dailyクラス◆◇◆◇◆◇◆◇◆◇
package calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
public class Daily extends CalendarTest {
JPanel dayback,// デイリーのバックパネル(一番後ろ
daybackinfo,// デイリーパネルのインフォメーション用(保存ボタンなども)
keyarea, dayarea,// デイリーのグリッドレイアウト用パネル
daymemo;// グリッドレイアウトに乗せるパネル
JButton dayhozon;
JTextArea dayMemo1, dayMemo2, dayMemo3, dayMemo4;
JTextArea dayMemo[] = { dayMemo1, dayMemo2, dayMemo3, dayMemo4 };
JTextArea monthkeywords, keyexplain;
// デイリーノートの作成時
File dailydir;
File dailydaysdir;
String[] daystimename = { "morning", "afternoon", "evening", "night" };
File daystimefile;
JLabel daytitle = new JLabel(a + "年" + b + "月" + c + "日" + "のデイリーテキストエリアです");
String daytimes[] = { "午前", "午後", "夕方", "夜 " };
String str = "";
String strs = str;
EtchedBorder border = new EtchedBorder(EtchedBorder.RAISED, Color.white,
Color.black);// Borderクラスを使ってボーダーを作成。
Daily() {
}
void dailymemo() {
// JPanel dayback = new JPanel(new BorderLayout());
dayback = new JPanel(new BorderLayout());
daybackinfo = new JPanel(new BorderLayout());
dayarea = new JPanel(new GridLayout(0, 4));
dayhozon = new JButton("保存する");
dayhozon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
monthkeyhozonsyori();
hozonsyori();
todo.hozonsyori();
restart();
}
});
dayback.add(daybackinfo, BorderLayout.NORTH);
dayback.add(dayarea, BorderLayout.CENTER);
daybackinfo.add(daytitle, BorderLayout.WEST);
daybackinfo.add(dayhozon, BorderLayout.EAST);
// デイリーテキストエリアの処理forで4回まわして4つのエリアを作成している。
monthkeywordssyori();
daystextsyori();
int search = dayarea.getComponentCount();
// System.out.println("コンポーネントの数" + search);
// System.out.println("strs=" + strs);
}
void dailydir() {
dailydir = new File("C:\\HMCalendar\\" + String.valueOf(a) + "\\"
+ String.valueOf(b) + "\\dailydir
");
if (dailydir.exists()) {
} else {
dailydir.mkdir();
}
dailydaysdir();
}
void dailydaysdir() {
dailydaysdir = new File("C:\\HMCalendar\\" + String.valueOf(a) + "\\"
+ String.valueOf(b) + "\\dailydir
" + "\\" + String.valueOf(c));
if (dailydaysdir.exists()) {
} else {
dailydaysdir.mkdir();
}
daystimefile();
}
void daystimefile() {
for (int i = 0; i < 4; i++) {
daystimefile = new File("C:\\HMCalendar\\" + String.valueOf(a)
+ "\\" + String.valueOf(b) + "\\dailydir
" + "\\"
+ String.valueOf(c) + "\\" + daystimename[i] + ".txt");
if (daystimefile.exists()) {
} else {
try {
daystimefile.createNewFile();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
dailymemo();
}
void monthkeywordssyori() {
strs = null;
// daysfileをそのまま使ったら上手くいかなかったのでもう一度作成しなおす。(多分、配列や変数を指定しているから。)
daysfile = new File("C:\\HMCalendar\\" + String.valueOf(a) + "\\"
+ String.valueOf(b) + "\\" + String.valueOf(c) + ".txt");
// テキストエリアを作成するごとにファイルのテキストを読み込む処理
// strsにファイルの中身を格納して引数として渡す。
// try-catchで例外処理
try {
BufferedReader br = new BufferedReader(new FileReader(daysfile));
while ((str = br.readLine()) != null) {
if (strs == null) {
strs = str;
} else {
strs = strs + "\n" + str;
// System.out.println("strs="+strs);
}
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("ファイルが見つかりません");
} catch (IOException e) {
System.out.println("エラーです");
}
// テキストエリアを作成
monthkeywords = new JTextArea(strs, 3, 30);
// テキストエリアにスクロールバーを取り付け。(スクロールバーをつけた場合、スクロールバーのパネルを貼り付ける。テキストエリアは直接貼り付けないことに注意)
JScrollPane monthKeyScrollpane = new JScrollPane(monthkeywords);
// 折り返しを設定
monthkeywords.setLineWrap(true);
monthkeywords.setWrapStyleWord(true);
// 余白を設定(各5ピクセル)
monthkeywords.setMargin(new Insets(5, 5, 5, 5));
// setCaretPositionでフォーカスしたときのカーソルの位置を指定(getText().length()でセットされている文字の長さを取得して、最後にカーソルが来るようにしている)
monthkeywords.setCaretPosition(monthkeywords.getText().length());
monthkeywords.setBorder(border);
keyexplain = new JTextArea("右側の欄に入力すると" + "\n" + "月間カレンダーに表示されます");
// 余白を設定(各5ピクセル)
keyexplain.setMargin(new Insets(5, 5, 5, 5));
keyexplain.setBorder(border);// TextAreaは枠がないので、setBorderで枠を作成。
keyexplain.setBackground(Color.lightGray);
keyarea = new JPanel(new BorderLayout());
keyarea.add(monthKeyScrollpane, BorderLayout.CENTER);
keyarea.add(keyexplain, BorderLayout.WEST);
daybackinfo.add(keyarea, BorderLayout.CENTER);
}
void daystextsyori() {
for (int i = 0; i < 4; i++) {
strs = null;
// daybackに貼るパネルを作成
daymemo = new JPanel(new BorderLayout());
// daystimefileをそのまま使ったら上手くいかなかったのでもう一度作成しなおす。(多分、配列や変数を指定しているから。)
daystimefile = new File("C:\\HMCalendar\\" + String.valueOf(a)
+ "\\" + String.valueOf(b) + "\\dailydir
" + "\\"
+ String.valueOf(c) + "\\" + daystimename[i] + ".txt");
// テキストエリアを作成するごとにファイルのテキストを読み込む処理
// strsにファイルの中身を格納して引数として渡す。
// try-catchで例外処理
try {
BufferedReader br = new BufferedReader(new FileReader(
daystimefile));
while ((str = br.readLine()) != null) {
if (strs == null) {
strs = str;
} else {
strs = strs + "\n" + str;
// System.out.println("strs="+strs);
}
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("ファイルが見つかりません");
} catch (IOException e) {
System.out.println("エラーです");
}
// テキストエリアを作成
dayMemo[i] = new JTextArea(strs, 7, 30);
// テキストエリアにスクロールバーを取り付け。(スクロールバーをつけた場合、スクロールバーのパネルを貼り付ける。テキストエリアは直接貼り付けないことに注意)
JScrollPane dayMemoScrollpane = new JScrollPane(dayMemo[i]);
// 折り返しを設定
dayMemo[i].setLineWrap(true);
dayMemo[i].setWrapStyleWord(true);
// 余白を設定(各5ピクセル)
dayMemo[i].setMargin(new Insets(5, 5, 5, 5));
// setCaretPositionでフォーカスしたときのカーソルの位置を指定(getText().length()でセットされている文字の長さを取得して、最後にカーソルが来るようにしている)
dayMemo[i].setCaretPosition(dayMemo[i].getText().length());
JLabel daytime = new JLabel(daytimes[i]);
daytime.setBackground(Color.white);
daytime.setBorder(border);
daytime.setOpaque(true);
daymemo.setBorder(border);
dayarea.add(daymemo);
daymemo.add(dayMemoScrollpane, BorderLayout.CENTER);
daymemo.add(daytime, BorderLayout.WEST);
// 以下のsysoutで年月日の変数の確認と取得したファイルの確認
// System.out.println(daystimefile);
// System.out.println("a=" + a + " b=" + b + " c=" + c);
}
}
void monthkeyhozonsyori() {
daysfile = new File("C:\\HMCalendar\\" + String.valueOf(a) + "\\"
+ String.valueOf(b) + "\\" + String.valueOf(c) + ".txt");
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
daysfile)));
String str = monthkeywords.getText();
pw.println(str);
pw.close();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
void hozonsyori() {
for (int i = 0; i < 4; i++) {
daystimefile = new File("C:\\HMCalendar\\" + String.valueOf(a)
+ "\\" + String.valueOf(b) + "\\dailydir
" + "\\"
+ String.valueOf(c) + "\\" + daystimename[i] + ".txt");
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(
new FileWriter(daystimefile)));
String str = dayMemo[i].getText();
pw.println(str);
System.out.println("strの中身は=" + str);
pw.close();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
}
◆◇◆◇◆◇◆◇◆◇Todoクラス◆◇◆◇◆◇◆◇◆◇
package calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
public class Todo extends CalendarTest {
JPanel todoback,// Todoエリアのバックパネル(一番後ろ)
todobackinfo,// Todoエリアのインフォメーション用
todoarea,// Todoエリアのグリッドレイアウト
todomemo;// Todoエリアのグリッドに載せるパネル
JLabel todotitle = new JLabel("重要度と緊急度のToDoリスト");
JTextArea todoMemo1, todoMemo2, todoMemo3, todoMemo4;
JTextArea todoMemo[] = { todoMemo1, todoMemo2, todoMemo3, todoMemo4 };
// デイリーノートの作成時
String[] todofilename = { "Aarea", "Barea", "Carea", "Darea" };
File todofile;
String todoArea[] = { "緊急で重要", "緊急でないが重要", "緊急だが重要でない", "緊急でも重要でもない" };
String str = "";
String strs = str;
EtchedBorder border = new EtchedBorder(EtchedBorder.RAISED, Color.white,
Color.black);// Borderクラスを使ってボーダーを作成。
Todo() {
}
void todomemo() {
todoback = new JPanel(new BorderLayout());
todobackinfo = new JPanel(new BorderLayout());
todoarea = new JPanel(new GridLayout(4, 0));
todoback.add(todobackinfo, BorderLayout.NORTH);
todoback.add(todoarea, BorderLayout.CENTER);
todobackinfo.add(todotitle, BorderLayout.CENTER);
// デイリーテキストエリアの処理forで4回まわして4つのエリアを作成している。
todostextsyori();
int search = todoarea.getComponentCount();
// System.out.println("コンポーネントの数" + search);
// System.out.println("strs=" + strs);
}
void todofile() {
for (int i = 0; i < 4; i++) {
todofile = new File("C:\\HMCalendar\\" + todofilename[i] + ".txt");
if (todofile.exists()) {
} else {
try {
todofile.createNewFile();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
todomemo();
}
void todostextsyori() {
for (int i = 0; i < 4; i++) {
strs = null;
// todobackに貼るパネルを作成
todomemo = new JPanel(new BorderLayout());
// todofileをそのまま使ったら上手くいかなかったのでもう一度作成しなおす。(多分、配列や変数を指定しているから。)
todofile = new File("C:\\HMCalendar\\" + todofilename[i] + ".txt");
// テキストエリアを作成するごとにファイルのテキストを読み込む処理
// strsにファイルの中身を格納して引数として渡す。
// try-catchで例外処理
try {
BufferedReader br = new BufferedReader(new FileReader(todofile));
while ((str = br.readLine()) != null) {
if (strs == null) {
strs = str;
} else {
strs = strs + "\n" + str;
// System.out.println("strs="+strs);
}
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("ファイルが見つかりません");
} catch (IOException e) {
System.out.println("エラーです");
}
// テキストエリアを作成
todoMemo[i] = new JTextArea(strs, 5, 30);
// テキストエリアにスクロールバーを取り付け。(スクロールバーをつけた場合、スクロールバーのパネルを貼り付ける。テキストエリアは直接貼り付けないことに注意)
JScrollPane todoMemoScrollpane = new JScrollPane(todoMemo[i]);
// 折り返しを設定
todoMemo[i].setLineWrap(true);
todoMemo[i].setWrapStyleWord(true);
// 余白を設定(各5ピクセル)
todoMemo[i].setMargin(new Insets(5, 5, 5, 5));
// setCaretPositionでフォーカスしたときのカーソルの位置を指定(getText().length()でセットされている文字の長さを取得して、最後にカーソルが来るようにしている)
todoMemo[i].setCaretPosition(todoMemo[i].getText().length());
JLabel todotime = new JLabel(todoArea[i]);
todotime.setBackground(Color.white);
todotime.setBorder(border);
todotime.setOpaque(true);
todomemo.setBorder(border);
todoarea.add(todomemo);
todomemo.add(todoMemoScrollpane, BorderLayout.CENTER);
todomemo.add(todotime, BorderLayout.NORTH);
// 以下のsysoutで年月日の変数の確認と取得したファイルの確認
// System.out.println(todofile);
// System.out.println("a=" + a + " b=" + b + " c=" + c);
}
}
void hozonsyori() {
for (int i = 0; i < 4; i++) {
todofile = new File("C:\\HMCalendar\\" + todofilename[i] + ".txt");
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(
new FileWriter(todofile)));
String str = todoMemo[i].getText();
pw.println(str);
System.out.println("strの中身は=" + str);
pw.close();
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
}