/************************************************************** * * MakeData1.java : テキスト形式の問題データ作成用プログラム * by totora (1999/11) * ***************************************************************/ import java.awt.*; import java.awt.event.*; import java.io.*; //ファイルの入出力に必要 public class MakeData1 extends Frame implements ActionListener, TextListener{ //TextListenerはTextFieldの入力事項の変更の有無をチェック //するために必要 Panel pnlBase, pnlLeft, pnlRight, pnlNorth, pnlNorth1, pnlNorth2; Panel pnlNorth2b; Button btn1, btn2, btn3, btn4, btn5; Label lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7; Label lblFname, lblRecNo; TextField tf1, tf2, tf3, tf4, tf5; TextField tfFname, tfRecNo; //*************入力には使わないからLabelに変更した方が良い MenuBar mainMenuBar; //メニューを使ってみる Menu menu1; MenuItem menuItem1, menuItem2, menuItem3; FileDialog freadDialog; //ファイル選択のために int recordNo; //現在のレコードナンバーを入れる int recordCount; //ファイルに記録されているレコードの数 String fileName; //オープンするファイル名 String recordData[];//データ読み込み用配列 static int maxRecordNo = 50;//データは最大50件に設定 boolean isChanged = false;//TextFieldの内容変更があればtrue public String newFname; //新規作成ファイルの名前を格納 public boolean answer; //ダイアログボックスに対する応答チェック //OKボタンが押されたらtrue public MakeData1(){ //コンストラクタ:各種初期設定 setLayout(new BorderLayout()); setSize(450,300); setTitle("MakeData1"); pnlBase = new Panel(); pnlLeft = new Panel(); pnlLeft.setLayout(new GridLayout(0,1)); pnlLeft.setSize(100,300); pnlRight = new Panel(); pnlRight.setLayout(new GridLayout(0,1)); pnlNorth = new Panel(); pnlNorth.setLayout(new GridLayout(0,1)); pnlNorth1 = new Panel(); pnlNorth2 = new Panel(); Font btnFont = new Font("Dialog", Font.BOLD, 12); Font largeFont = new Font("Dialog", Font.BOLD, 14); lblFname = new Label("File Name"); //処理ファイル名表示用 lblFname.setFont(btnFont); tfFname = new TextField(16); //処理ファイル名表示用 tfFname.setFont(largeFont); tfFname.setEnabled(false); pnlNorth1.add(lblFname); pnlNorth1.add(tfFname); lblRecNo = new Label("No / 総数:", Label.RIGHT); lblRecNo.setFont(btnFont); tfRecNo = new TextField(4); tfRecNo.setFont(largeFont); btn1 = new Button(" NEXT "); btn1.setFont(btnFont); btn1.addActionListener(this); btn2 = new Button(" PREV "); btn2.setFont(btnFont); btn2.addActionListener(this); btn3 = new Button("Append"); btn3.setFont(btnFont); btn3.addActionListener(this); btn4 = new Button("SAVE"); btn4.setFont(btnFont); btn4.addActionListener(this); btn5 = new Button("CLOSE"); btn5.setFont(btnFont); btn5.addActionListener(this); pnlNorth2b = new Panel(); pnlNorth2b.add(lblRecNo); pnlNorth2b.add(tfRecNo); pnlNorth2.add(pnlNorth2b); pnlNorth2.add(btn1); pnlNorth2.add(btn2); pnlNorth2.add(btn3); pnlNorth2.add(btn4); pnlNorth2.add(btn5); pnlNorth.add(pnlNorth1); pnlNorth.add(pnlNorth2); add(pnlNorth, "North"); lbl1 = new Label("データ1", Label.RIGHT); lbl1.setFont(btnFont); lbl2 = new Label("データ2", Label.RIGHT); lbl2.setFont(btnFont); lbl3 = new Label("データ3", Label.RIGHT); lbl3.setFont(btnFont); lbl4 = new Label("データ4", Label.RIGHT); lbl4.setFont(btnFont); lbl5 = new Label("データ5", Label.RIGHT); lbl5.setFont(btnFont); tf1 = new TextField(30); tf2 = new TextField(30); tf3 = new TextField(30); tf4 = new TextField(30); tf5 = new TextField(30); tf1.addTextListener(this); tf2.addTextListener(this); tf3.addTextListener(this); tf4.addTextListener(this); tf5.addTextListener(this); pnlLeft.add(lbl1); pnlLeft.add(lbl2); pnlLeft.add(lbl3); pnlLeft.add(lbl4); pnlLeft.add(lbl5); pnlRight.add(tf1); pnlRight.add(tf2); pnlRight.add(tf3); pnlRight.add(tf4); pnlRight.add(tf5); pnlBase.add(pnlLeft); pnlBase.add(pnlRight); add(pnlBase, "Center"); //////以上はほとんど画面設定 //以下はMENUの設定 mainMenuBar = new MenuBar(); Font menuFont = new Font("Dialog", Font.PLAIN,12); menu1 = new Menu("ファイル"); menu1.setFont(menuFont); menuItem1 = new MenuItem("開  く"); menuItem2 = new MenuItem("新規作成"); menuItem3 = new MenuItem("閉 じ る"); menuItem1.addActionListener(this); menuItem2.addActionListener(this); menuItem3.addActionListener(this); menu1.add(menuItem1); menu1.add(menuItem2); menu1.addSeparator(); menu1.add(menuItem3); mainMenuBar.add(menu1); setMenuBar(mainMenuBar); //MENU設定終了 fileName = new String(); recordNo = 1; addWindowListener(new WinListener()); } //////イベント処理(メニュー、ボタン)//////// public void actionPerformed(ActionEvent e){ if (e.getSource() == menuItem1){ //「開く」メニュー選択 if(isChanged) saveData(); if (freadDialog == null){ //ファイル選択ダイアログを読み込みように開く(LOAD) //タイトルは「ファイルの選択」 freadDialog = new FileDialog(this, "ファイルの選択", FileDialog.LOAD); } freadDialog.show(); if (freadDialog.getFile() == null) return; // fileName = freadDialog.getDirectory() // + freadDialog.getFile(); fileName= freadDialog.getFile(); tfFname.setText(fileName); //選択されたファイル名を表示 recordNo = 1; readData(); //ファイルのデータを読み込むメソッドを呼ぶ showData(recordNo); } else if (e.getSource() == menuItem2){ //「新規作成」を選択 if(isChanged) saveData(); makeNewFile(); //新しいファイル作成のメソッドを呼ぶ } else if (e.getSource() == menuItem3){ System.exit(0);//上記以外、つまり「閉じる」が選択された } else if (e.getSource() == btn1){ //NEXTボタンが押された if(isChanged) saveData(); recordNo = recordNo + 1; if(recordNo > recordCount){ recordNo = recordCount; } showData(recordNo); } else if (e.getSource() == btn2){ //PREVIOUSEボタンが押された if(isChanged) saveData(); recordNo = recordNo - 1; if(recordNo < 1){ recordNo = 1; } showData(recordNo); } else if (e.getSource() == btn3){ //APPENDボタンが押された if(isChanged) saveData(); appendRec(); } else if(e.getSource() == btn4){ //SAVEボタンが押された saveData(); } else if(e.getSource() == btn5){ //CLOSEボタンが押された System.exit(0); } } ////TextFieldの内容に変更があると起動、isChangedをtrueにする public void textValueChanged(TextEvent e){ recordData[recordNo - 1] = "\t" + tf1.getText() + "\t" + tf2.getText() + "\t" + tf3.getText() + "\t" + tf4.getText() + "\t" + tf5.getText(); //変更があったら配列のデータを書き換えて isChanged = true; //変更有りにしておく } ////ファイルからデータを配列に読み込む ////ファイルの扱いには例外処理がからむことが多いので注意 public void readData(){ String buff; int i; FileReader fr; recordData = new String[maxRecordNo]; for(int j = 0; j