Report#5

GUIプログラム:講義資料を参考に、以下の課題を行え。

   1. 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
   2. 例外処理について、考察せよ。
   3. 上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
   4. 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。。
   5. 「電卓」プログラム。中身は自分の思うように。 

(1)偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。


ソースプログラム

GUIaa.java
import java.awt.*;
import java.awt.event.*;

public class GUIaa extends Frame {
    Button    b0 = new Button("Even/Odd?");
    Label     x0 = new Label("Type a number and press...");
    TextField t0 = new TextField();

    public GUIaa() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 90, 30);
        add(b0); b0.setBounds(110, 40, 80, 30);
        add(x0); x0.setBounds(10, 80, 180, 30);
        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = (new Integer(t0.getText())).intValue();
                    t0.setText("");
                    if(i % 2 == 0) {
                        x0.setText(i + " is Even");
                    } else {
                        x0.setText(i + " is Odd");
                    }
                }
            });
    }
    public static void main(String[] args) {
        Frame win = new GUIaa();
        win.setSize(200, 150); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}



実行結果


考察


(1) java.awtパッケージとjava.awt.eventパッケージをimport。 
   1行目のimport java.awt.*でjava.awt内のすべてのクラスは参照できるようになるが 
   java.awt中のパッケージ内のクラスまでは参照することはできないので、 
   さらにimport java.awt.event.*を記述している。

(2) ウインドウを作成するためにFrameクラスを継承したサブクラスGUIaaを作っている。

(3) Buttonクラス、Labelクラス、TextFieldクラスのインスタンスを作り 
   それぞれb0,x0,t0に代入。これらはコンポーネントと呼ばれるGUIの部品である。   
   Button  
   ユーザーがボタン境界の内側でマウスをクリックすると、 
   ユーザーにフィードバックを提供するようにボタンの外観が変化する。     
   Label(String str, int align)    
   strはラベルテキストで、alignは、ラベルを左揃え、中央揃え、右揃えのうちどれにするかを示す定数。    
   定数LEFT,CENTER,RIGHTとして位置合わせの値を設定する。    
   GUI上に表示される文字列で、プログラムでは変更できますが、ユーザーが変更することはできない。    
   TextField    
   TextFieldでは、1行のテキストを入力することができます。    
   文字を入力すると、テキストイベントが生成される。    
   ENTERキーを押したときにアクションイベントを生成。

(4) コンテナと呼ばれるコンポーネントを配置できるコンポーネントに addメソッドを使用してButton等のGUI部品を格納。 
   setBoundsで実際に配置する座標と大きさを指定。

(5) ActionEvent ボタンを押したり、リスト項目をダブルクリックしたとき、メニュー項目を選択したときに生成。

(6) int i = (new Integer(t0.getText())).intValue(); t0.getText()でt0のテキストを読み込みIntegerで生成後のオブジェクトを不変なものとする。

(7) Frame型の変数winにGUIaaクラスのインスタンスを代入している。 
   GUIaaクラスはFrameクラスを継承して作ったサブクラスなので代入が可能である。 
   setSizeメソッドでウインドウの大きさを指定し、setVisible(true)でウインドウを見えるようにする。


(2)例外処理について、考察せよ。


例外処理の構文
try{
   例外が発生するかもしれない処理
}
catch(例外の型1 引数1){
   例外1が発生したときの処理
}
catch(例外の型2 引数2){
   例外2が発生したときの処理
}
・・・
catch(例外の型n 引数n){
   例外nが発生したときの処理
}
finally{
   例外が発生しなくても必ず実行する処理
}



 tryブロックでは例外が発生するかもしれない処理を記述。
この処理中で例外が発生したときには、以降の処理を中止して発生した例外に対応する catchブロックを
探します。対応するcatchブロックがなかった場合は、メソッドの呼び出し元をさかのぼって対応する
catchブロックを探します。
 発生した例外に対応するcatchブロックがみつかれば、そのcatchブロックの処理を実行して例外処理を
終わります。(残りのcatchブロックの処理は行われません。)

 また、tryブロックで例外が発生してもしなくても、finallyブロックは必ず実行されます。tryブロック
やcatchブロックの中で returnステートメントによりメソッドを抜ける場合にもfinallyブロックは実行
されるので、ファイルのclose処理などを行うのに便利です。
 catchブロックやfinallyブロックは省略できますが、すくなくともcatchブロックかfinallyブロック
のどちらかがひとつ必要です。

(3)上述のサンプルプログラムに出れきたGUI部品を、全て使ったプログラムを作 成せよ。


ソースプログラム

import java.awt.*;
import java.awt.event.*;

public class report5_3 extends Frame{

    Button bt1 = new Button("計算開始");
    Button bt2 = new Button("CLEAR");
    Label l1 = new Label("縦の長さ");
    Label l2 = new Label("横の長さ");
    Label l3 = new Label("高さ");
    Label l4 = new Label("結果");
    TextField t1 = new TextField();
    TextField t2 = new TextField();
    TextField t3 = new TextField();
    TextField t4 = new TextField();

    public report7(){
        setLayout(null);
        add(l1); l1.setBounds(20, 50,  60, 30);
        add(l2); l2.setBounds(20, 100, 60, 30);
        add(l3); l3.setBounds(20, 150, 60, 30);
        add(l4); l4.setBounds(20, 250, 60, 30);
        add(t1); t1.setBounds(150, 50, 100, 30);
        add(t2); t2.setBounds(150, 100, 100, 30);
        add(t3); t3.setBounds(150, 150, 100, 30);
        add(t4); t4.setBounds(150, 250, 150, 30);
        add(bt1);bt1.setBounds(  20, 200, 100, 40);
        add(bt2);bt2.setBounds( 140, 200, 100, 40);

        bt1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    try{
                        double length1 =  (new
                Double(t1.getText())).doubleValue();
                        double length2 = (new
                Double(t2.getText())).doubleValue();
                        double length3 = (new
                Double(t3.getText())).doubleValue();
                        double answer = (length1 * length2 * length3);
                        t4.setText(Double.toString(answer));
                    }catch(Exception ex){
                        t4.setText("整数を入れて下さい");
                    }

                }
            });

        bt2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    t1.setText("");
                    t2.setText("");
                    t3.setText("");
                    t4.setText("");
                }
            });
    }
    public static void main(String[] args) {
        Frame win = new report7();
        win.setTitle("立体の体積");
        win.setSize(350, 300);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}



考察


(1) このプログラムはサンプルプログラムで使用されている Button,Label,TextFieldのコンポーネントを使用している。

(2) mainメソッドはまずreport4クラスをインスタンス化しコンストラクタを呼 び出す。

(3)クラスreport4は2つのButtonと4つのLabel、TextFieldのオブジェクトを 作成。

(4) コンストラクタ内でFrameコンテナに作成した全てのコンポーネントを追加。

(5)レイアウトマネージャは不使用。 


(4)摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。


ソースプログラム

import java.awt.*;
import java.awt.event.*;

public class report5_4 extends Frame {
    Button    b0 = new Button("華氏へ");
    Button    b1 = new Button("摂氏へ");
    Label     x0 = new Label("温度換算");
    Label     x1 = new Label("結果");

    TextField t0 = new TextField();
    TextField t1 = new TextField();

    public report5_4() {
        setLayout(null);
        add(t0); t0.setBounds(10, 60, 80, 30);
        add(t1); t1.setBounds(100, 120, 80, 30);
        add(b0); b0.setBounds(100,  40, 80, 30);
        add(b1); b1.setBounds(100, 80, 80, 30);

        add(x0); x0.setBounds(10, 20, 180, 30);
        add(x1); x1.setBounds(65, 130, 180, 30);
        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try{
                        int i = (new Integer(t0.getText())).intValue();
                        t1.setText("" + (i*1.8+32));
                    }catch(Exception ex) {
                        t1.setText("error");
                    }
                }
            });
        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try{
                        int i = (new Integer(t0.getText())).intValue();
                        t1.setText("" + (i-32)/1.8);
                    }catch(Exception ex) {
                        t1.setText("error");
                    }
                }
            });
    }

    public static void main(String[] args) {
        Frame win = new report5_4();
        win.setSize(200, 160);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    System.exit(0);
                }
            });
    }
}


考察


(1) getSelectedItem()メソッドを使いChoiceで選択された項目で処理を分岐。 
摂氏から華氏への変換式は 華氏 = 摂氏 × 1.8 + 32 なので 
Choiceで"華氏へ"が選択された場合は i = (int)(i * 1.8f + 32)と処理する。 
(int)は数字を分かりやすくするために整数型に変換するために記述。 
また、"摂氏へ"が選択されていた場合は逆に i = (int)((i - 32) / 1.8f)となる。 


(5)「電卓」プログラム。中身は自分の思うように。


ソースプログラム

 import java.awt.*;
 import java.awt.event.*;

 public class report5_5 extends Frame {

     Button    a0 =  new Button("+");
     Button    b0 =  new Button("-");
     Button    c0 =  new Button("*");
     Button    d0 =  new Button("/");
     TextField e0 =  new TextField();
     TextField e1 =  new TextField();
     Label     f0 =  new Label("input integer");
     Button    g0 =  new Button("Reset");

     public report5_5() {
         setLayout(null);
         add(e0); e0.setBounds(60,87,80,30);
         add(a0); a0.setBounds(150,30,40,30);
         add(b0); b0.setBounds(150,70,40,30);
         add(c0); c0.setBounds(150,110,40,30);
         add(d0); d0.setBounds(150,150,40,30);
         add(e1); e1.setBounds(200,87,80,30);
         add(f0); f0.setBounds(60,180,80,30);
         add(g0); g0.setBounds(60,220,80,30);
         a0.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 int i = (new Integer(e0.getText())).intValue();
                 int j = (new Integer(e1.getText())).intValue();
                 int k = i+j;
                 f0.setText("answer is "+ new Integer (k).toString());
             }
         });
         b0.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 int i = (new Integer(e0.getText())).intValue();
                 int j = (new Integer(e1.getText())).intValue();
                 int k = i-j;
                 f0.setText("answer is "+ new Integer (k).toString());
             }
         });
         c0.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 int i = (new Integer(e0.getText())).intValue();
                 int j = (new Integer(e1.getText())).intValue();
                 int k = i*j;
                 f0.setText("answer is "+ new Integer (k).toString());
             }
         });
         d0.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 int i = (new Integer(e0.getText())).intValue();
                 int j = (new Integer(e1.getText())).intValue();
                 int k = i/j;
                 f0.setText("answer is "+ new Integer (k).toString());
             }
         });
         g0.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 e0.setText("");
                 e1.setText("");
                 f0.setText("input integer");
             }
         });
     }
     public static void main(String[] args) {
         Frame win = new denntaku();
         win.setSize(340, 260); win.setVisible(true);
         win.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent evt) {
                 System.exit(0);
             }
         });
     }
 }

感想


taihendesita---