/* -------------------------------------- テキストボックスのイベント処理 --------------------------------------- */ import java.applet.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Applet_Textbox_event extends Applet implements ActionListener{ TextField txt; //テキストボックス変数 Label title,title2; //ラベル変数 int number,n=0; // ----------- 初期設定 ------------- public void init() { title=new Label("ゲームを始めます"); title.setForeground(Color.red); //文字の色 title.setBackground(Color.green); //背景色 this.add(title); //ラベルの表示 title2=new Label("数字を入れてください(1〜3)"); this.add(title2); txt=new TextField(10); this.add(txt); //テキストボックスの表示 txt.addActionListener(this);//アクションリスナーを追加 } // ----------- 描画処理 ------------- public void paint(Graphics g){ if(n>0){ g.drawString(n+"回目", 20,120); g.drawString(number+"が入力されました", 20,140); } } // ----------- イベント発生時の処理 ------------- public void actionPerformed(ActionEvent ev){ number=Integer.parseInt(txt.getText()); //テキストボックスの値を読みこむ n++; txt.setText(""); //テキストボックスをクリアする repaint(); //paintメソッドの呼び出し } }