GUI部品

  1. 目的
    GUIを利用して、数値を入力し、インチからcmに変換します。

  2. GUI部品
    1. TextFieldクラス
      文字入力には、TextFieldクラスを利用します。
       private TextField textField1 = new TextField();
      で、入力フィールド(部品)textField1を定義します。
       textField1.setText("1");
      で、初期入力を設定し、
       textField1.setBounds(new Rectangle(30, 25, 130, 30));
      で入力領域のサイズを指定します。

      ここでは、このtextField1に「インチ」を入力することにします。入力の編集機能はTextFieldクラスに内蔵されています。
       同様に、cmを表示する TextField クラスを用意します。
       private TextField textField2 = new TextField();
       textField2.setText("2.54");
       textField2.setBounds(new Rectangle(30, 125, 130, 30));

    2. 変換ボタン
      変換ボタン conv を用意します。
       private Button conv = new Button();
       conv.setLabel("inch>cm");
       conv.setBounds(new Rectangle(60, 80, 80, 30));
       conv.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
       conv_actionPerformed(e); } });

    3. 部品の登録
      部品の配置をマニュアルで指定しますから、自動配置を無効にします。

       this.setLayout(null);

      最後に、ボタンと文字入力部品を配置します。
       this.add(textField1, null);
       this.add(textField2, null);
       this.add(conv, null);

    4. イベントハンドラ
       convボタンを押したとき、特定のメソッドを呼び出す設定が必要です。これには、
        addActionListener()
      で行います。ここでは、ActionListener() クラスを内部(インナー)クラスとして生成し、この内部クラスの、actionPerformed() メソッドで、conv_actionPerformed(e); を実行します。

      conv.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
       conv_actionPerformed(e); } });

      これで、convボタンを押すと conv_actionPerformed(e); が呼び出されます。

    5. 数字と文字の型変換
       文字入力部品から文字を取り出すには

       String s1=textField1.getText()

      を利用します。文字列の型にはStringを利用します。入力される値は文字型ですから、計算をするには、数値型(ここでは double)に変換する必要があります。これには、

       double inch=Double.parseDouble(s1);

      とします。parseは文字を「解析」する意味です。整数に変換する場合は、Integer.parseInt(s1); とします。
       逆に、double型の小数cmを小数型の文字にs2変換するには
       String s2=Double.toString(cm);
      とします。この文字を textField2 に表示するには
       textField2.setText(s2);
      です。

  3. プログラム
    1. ソースプログラム
      アプレットで作成します。以下のようになります。jbInit() で、GUI部品の生成を行います。jbInitはinit()から呼び出します。単位の変換処理は、conv_actionPerformed(ActionEvent e) で行います。jbInit()を含むソース全体は、こちらにあります。

      import java.awt.*;
      import java.awt.event.*;
      import java.applet.*;
      
      public class inch2cm extends Applet {
        private TextField textField1 = new TextField();
        private Button conv = new Button();
        private TextField textField2 = new TextField();
      
        //アプレットの初期化
        public void init() {
          try {
            jbInit();
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }
      
        //部品の生成と配置
        private void jbInit() throws Exception {
          textField1.setText("1");
          textField1.setBounds(new Rectangle(30, 25, 130, 30));
          this.setLayout(null);
          conv.setLabel("inch>cm");
          conv.setBounds(new Rectangle(60, 80, 80, 30));
      
           //ボタン処理メソッドの定義
          conv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              conv_actionPerformed(e); } });
          textField2.setText("2.54");
          textField2.setBounds(new Rectangle(30, 125, 130, 30));
          this.add(textField1, null);
          this.add(textField2, null);
          this.add(conv, null);
        }
      
      //ボタン処理 インチをcmに変換
       void conv_actionPerformed(ActionEvent e) {
          String s1=textField1.getText();
          double inch=Double.parseDouble(s1);
          double cm=inch*2.54;
          String s2=Double.toString(cm);
          textField2.setText(s2);
        }
      }
    2. 実行
       inchに数値を入力し、ボタンを押します。