1 / 5

イヴェント処理(2):テキストフィールド

イヴェント処理(2):テキストフィールド. テキスト・フィールド:数値などをアプレット上で入力し、プログラムを実行。 ボタンとテキストフィールド: インターフェイスは ActionListener メソッドは actionPerformed. イヴェント処理を行うプログラムの基本構造(雛形) Import java.applet.*; Import java.awt.event.*; // イヴェント処理に必要 public class クラス名 extends Applet implements イヴェントリスナー・インターフェイス

telma
Télécharger la présentation

イヴェント処理(2):テキストフィールド

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. イヴェント処理(2):テキストフィールド テキスト・フィールド:数値などをアプレット上で入力し、プログラムを実行。 ボタンとテキストフィールド: インターフェイスはActionListener メソッドはactionPerformed

  2. イヴェント処理を行うプログラムの基本構造(雛形)イヴェント処理を行うプログラムの基本構造(雛形) Import java.applet.*; Import java.awt.event.*; // イヴェント処理に必要 public class クラス名 extends Applet implements イヴェントリスナー・インターフェイス ・・・     // ボタンとテキストフィールドはActionListener public void init() { // アプレットロード時の初期化メソッド ・・・  GUI部品の作成 } // end init public void actionPerformed(ActionEvent e) { ・・・  ボタンが押されたときの処理 } // end actionPerformed

  3. イヴェントリスナー イヴェント発生(例えばボタンが押された)  →イヴェントを処理する場所であるリスナーに伝達  →リスナーに実装されているメソッドが実行される 手順1:implements イヴェントリスナー 手順2:イヴェントに対応するメソッドを addイヴェントリスナー()メソッドで設定 (手順3:プログラムが実行される)

  4. 例: sample 8 import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class PreyPred extends Applet implements ActionListener { // 手順1 TextField aTXT, bTXT, cTXT; // 係数a、b、cのためのテキストフィールド Button rButton; double a, b, c; public void init() { // アプレットロード時にまず読み込む a=1.42; b=0.55; c=5.14; //係数の初期値 add(new Label(“被食・捕食方程式”)); // 文字列を表示 rButton = new Button(“描画”); // ボタン・オブジェクトの生成 add(rButton); add(new Label(“a=”)); aTXT=new TextField(“1.42, 3”); // aの初期値を表示。bとcは省略 rButton.addActionListener(this); // 手順2 aTXT.addActionListener(this); // 手順2、bとcは省略 }

  5. public void actionPerformed(ActionEvent e) { String str; if (e.getSource() == rButton) { str=aTXT.getText().trim; // アプレット上から入力された // 係数aの値を文字列として読み込む a=new Double(str).doubleValue(); // 読み込んだ文字列を // double型に変換して格納   ・・・ bとcは省略 repaint();// ボタンが押されたときの処理 }

More Related