/*----------------------------------------------- テキストボックスとラベルの表示 ------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Applet_Textbox_event extends Applet implements ActionListener { TextField txt1; // テキストボックス用の変数定義 Label title,label; // ラベル変数の定義 int number,n=0; // ------- 初期設定 --------- public void init() { title=new Label("数当てゲームを始めます"); title.setBackground(Color.green); // 背景色 title.setForeground(Color.red); // 文字色 this.add(title); // タイトルの表示 label=new Label("数字を入れてください"); this.add(label); txt1=new TextField(10); this.add(txt1); // テキストボックスの表示 txt1.addActionListener(this); // アクションリスナー追加 } // ------- 描画処理 --------- public void paint(Graphics g) { if (n>0) { g.setColor(Color.red); g.drawString(n+"回目", 30, 130); g.setColor(Color.blue); g.drawString(number+"が入力されました", 30, 150); } } // ------- アクションリスナー --------- public void actionPerformed(ActionEvent e) { number=Integer.parseInt(txt1.getText()); txt1.setText(""); n++; repaint(); } }