プログラミングreport#5

名前:上原和樹
番号:065709D
所属:情報工学科1年次

課題

Report#5:GUIプログラム:講義資料を参考に、以下の課題を行え。{〜 01/15(Mon)}

GUIaaの考察

まず、GUIaaを動作させる。 GUIaaのソースは以下の通りである。
GUIaa
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);
            }
        });
    }
}

ソースの解説および考察

ソース解説 プログラムの動作について考察する 続いて、ソースについて考察する。
GUI部品を生成するとき
部品の種類 部品名 = new 部品の種類();
として、生成する

GUI部品をウィンドウに追加する場合
add(部品名);
で追加する。

ウィンドウ内で部品の位置を指定するとき
部品名.setBounds (x座標、y座標、幅、高さ)
として指定される。xとyについては左上の隅が(0,0)となる。

GUIについて

GUI(Graphical User Interface)
ユーザに対する情報の表示にグラフィックを多用して、ほとんどの操作はマウス
などで行うようにできるインターフェース。

例外処理についての考察

上でGUIaaプログラムを動作させた時に文字、あるいは上限を超えた値を入力し たときにエラー文を出力した。
これを処理するために例外処理を施す。
例外処理の構文には以下のようなのがある。
例外処理を加えて上でのGUIaaプログラムを書き換える。
        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try{
                    int i = (new Integer(t0.getText())).intValue();
                    t0.setText("input the number");
                    if(i % 2 == 0) {
                        x0.setText("Even");
                    }else {
                        throw new ArithmeticException();
                    }
                    }catch (ArithmeticException ex){
                        x0.setText("Odd");
                    }catch(NumberFormatException ex) {
                        x0.setText("Error");
                    }
                    finally{
                        t0.setText("");
                }
                }
            });
    }

例外処理によってエラーが出るようになった

GUI部品を使用したプログラム

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

public class Gui5_1 extends Frame {

    int a;
    String m, h, time;
    TextField t0 = new TextField("imput the seconds");
    Choice ch0 = new Choice();
    Button b0 = new Button("change");
    TextField t1 = new TextField();
    Label x0 = new Label("Change time");
    public Gui5_1() {
        setLayout(null);

        add(x0); x0.setBounds(10,90,80,30);
        add(t0); t0.setBounds(10,30,150,30);
        add(ch0); ch0.setBounds(200,30,150,30);
        ch0.add("minutes");     m = "minutes";
        ch0.add("hours");       h = "hours";
        ch0.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent evt){
                    time = ch0.getSelectedItem();
                    if (time == m) {a = 1;}
                    else if (time == h) {a = 0;}
                }
            });

        add(b0); b0.setBounds(160,90,80,20);
        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                if (a == 1){
                    try {
                int j = (new Integer(t0.getText())).intValue();
                int d = (j/60);
                        t1.setText("" + d + "minutes");
                    }catch(Exception ex) {
                        t1.setText("Error");
                    }
                }
                else if (a == 0) {
                    try {
                        int j = (new Integer(t0.getText())).intValue();
                        int d = (j/360);
                        t1.setText("" + d + "hours");
                    }catch(Exception ex){
                        t1.setText("Error");
                    }
                }
            }
            });

    add(t1); t1.setBounds(50,150,100,30);
    }
    public static void main(String args[]){
        Frame win = new Gui5_1();
        win.setSize(400,200);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    System.exit(0);
                }
            });
    }
}
実行結果

解説と考察

秒を分か時間に変換するプログラムである。 プログラムの流れとしては というながれになっている。
考察として・・・
処理において文字を出力させるためには
部品名.setText("~~~")
とすることによって()内の文字がその配置した部品のところに出力されることが わかった。
add(部品名)は部品名を追加するということであり、setBoundsで部品の位置の設 定になる(前述参照)
入力の値を得るにはTextFieldに入力になり、それを得るには
部品名.getText()として得られる。

華氏と摂氏の変換プログラム

プログラムソース
Gui5_2.java
import java.awt.*;
import java.awt.event.*;

public class Gui5_2 extends Frame {
    Label x0 = new Label("CelsiusFahrenheit");
    TextField t0 = new TextField("input the Celsius");
    TextField t1 = new TextField("input the Fahrenheit");
    Button b0 = new Button("Change");
    Button b1 = new Button("Change");
    Label x1 = new Label();
    Label x2 = new Label();


    public Gui5_2(){
        setLayout(null);
        add(x0); x0.setBounds(10,30,150,30);
        add(x1); x1.setBounds(260,80,60,30);
        add(x2); x2.setBounds(260,130,60,30);
        add(t0); t0.setBounds(10,80,150,30);
        add(t1); t1.setBounds(10,130,150,30);
        add(b0); b0.setBounds(180,80,70,30);
        b0.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt) {
                try {
                    int i = (new Integer(t0.getText())).intValue();
                    i = (int)Math.rint(1.8*i+32);
                    x1.setText(i+"F");
                }catch(Exception err) {x1.setText("Err.");}
            }
            });
        add(b1); b1.setBounds(180,130,70,30);
        b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt) {
                    try {
                        int j = (new Integer(t1.getText())).intValue();
                        j = (int)Math.rint((j-32)/1.8);
                        x2.setText(j+"C");
                    }catch(Exception err) {x2.setText("Err.");}
                }
            });
    }
    public static void main(String[] args){
        Frame win = new Gui5_2();
        win.setSize(400,200);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    System.exit(0);
                }
            });
    }
}

実行結果


↓温度入力

解説と考察

上の秒を換算するプログラムとやっていることは同じであることに気づいた
違いはこのプログラムはボタンを押したらすぐに結果が出るようになっている
プログラムの流れとしては というながれで行われている。

電卓プログラム

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

public class Gui5_3 extends Frame {
    Label x0 = new Label("calculator");
    Label x1 = new Label("?");
    Label x2 = new Label();
    TextField t0 = new TextField();
    TextField t1 = new TextField();
    Button b1 = new Button("+");
    Button b2 = new Button("-");
    Button b3 = new Button("*");
    Button b4 = new Button("/");
    Button b5 = new Button("%");
    public Gui5_3(){
        setLayout(null);
        add(x0); x0.setBounds(10,30,100,30);
        add(t0); t0.setBounds(10,80,100,30);
        add(x1); x1.setBounds(120,85,30,30);
        add(t1); t1.setBounds(160,80,100,30);
        add(x2); x2.setBounds(300,80,100,30);
        add(b1); b1.setBounds(10,130,30,30);
        b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    try{
                        int i = (new Integer(t0.getText())).intValue();
                        int j = (new Integer(t1.getText())).intValue();
                        int k = i + j;
                        x2.setText("= " + k);
                        x1.setText("+");
                    }catch(NumberFormatException ex)
    {x2.setText("Err.");}
                }
            });
        add(b2); b2.setBounds(50,130,30,30);
        b2.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    try{
                        int i1 = (new Integer(t0.getText())).intValue();
                        int j1 = (new Integer(t1.getText())).intValue();
                        int k1 = i1 - j1;
                        x2.setText("= " + k1);
                        x1.setText("-");
                    }catch(NumberFormatException ex)
    {x2.setText("Err.");}
                }
            });
        add(b3); b3.setBounds(90,130,30,30);
        b3.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    try{
                        int i2 = (new Integer(t0.getText())).intValue();
                        int j2 = (new Integer(t1.getText())).intValue();
                        int k2 = i2 * j2;
                        x2.setText("= " + k2);
                        x1.setText("*");
                    }catch(NumberFormatException ex)
    {x2.setText("Err.");}
                }
            });
        add(b4); b4.setBounds(130,130,30,30);
        b4.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    try{
                        int i3 = (new Integer(t0.getText())).intValue();
                        int j3 = (new Integer(t1.getText())).intValue();
                        int k3 = i3 / j3;
                        x2.setText("= " + k3);
                        x1.setText("/");
                    }catch(NumberFormatException ex)
    {x2.setText("Err");}
                }
            });
        add(b5); b5.setBounds(170,130,30,30);
        b5.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    try{
                        int i4 = (new Integer(t0.getText())).intValue();
                        int j4 = (new Integer(t1.getText())).intValue();
                        int k4 = i4 % j4;
                        x2.setText("= " + k4);
                        x1.setText("%");
                    }catch(NumberFormatException ex)
    {x2.setText("Err.");}
                }
            });
    }
    public static void main(String[] args){
        Frame win = new Gui5_3();
        win.setSize(400,200);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    System.exit(0);
                }
            });
    }
}

実行結果


解説と考察
入力欄を2つ用意し、その入力された2つの値を押したボタンによって演算結果 がかわる電卓プログラムになった。
少し言い訳っぽくすれば、ボタンを沢山作って電卓みたいに数字を押してやるの も楽しそうだったけど、文字の量が多くなるのでそういう風にはしなかった。
プログラムの流れは上とほとんど同じであるため割愛する。

参考

独習Java第3版 ジョセフ・オニール著 トップスタジオ訳