課題

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

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

GUIaa.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.awt.*;
import java.awt.event.*;     //-------------------------------------------- (a)

public class GUIaa extends Frame {     //---------------------------------- (b)
    Button    b0 = new Button("Even/Odd?");             //
    Label     x0 = new Label("Type a number and press...");  //------------ (c)
    TextField t0 = new TextField();                          //
    
    public GUIaa() {
        setLayout(null);                //--------------------------------- (d)
        add(t0); t0.setBounds(10, 40, 90, 30);    //----------------------- (e)
        add(b0); b0.setBounds(110, 40, 80, 30);   //----------------------- (f)
        add(x0); x0.setBounds(10, 80, 180, 30);   //----------------------- (g)
        b0.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent evt) {  //------------- (h)
                int i = (new Integer(t0.getText())).intValue();  //-------- (i)
                t0.setText("");
                if(i % 2 == 0) { 
                    x0.setText(i + " is Even");   //----------------------- (j)
                } else {                        
                    x0.setText(i + " is Odd");    //----------------------- (k)
                }
            }
        });
    }
    public static void main(String[] args) {
        Frame win = new GUIaa();
        win.setSize(200, 150); win.setVisible(true);   //------------------ (l)
        win.addWindowListener(new WindowAdapter() {    //------------------ (m)
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

実行結果


考察

(a)-GUI関係のパッケージクラス群をインポート
(b)-Frameクラスの継承
(c)-インスタンス変数としてボタン、表示欄、入力欄を生成・初期化
(d)-部品の自動配置機能をoffにする
(e)-入力欄の貼り付け(左上隅のx/y座標、幅、高さ)
(f)-ボタンの貼り付け
(g)-表示欄の張り付け
(h)-ボタンが押された時の動作
(i)-変数iを設定
(j)-iが2で割り切れるなら"i is Even"と表示
(k)-それ以外なら"i is Odd"と表示
(l)-ウィンドウのサイズディスプレイへ表示
(m)-ウィンドウを閉じる(終了する)動作


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

ColorRGBa.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  import java.awt.*;
  import java.awt.event.*;                                         //---------------------------- (a)

  public class ColorRGBa extends Frame {                           //---------------------------- (b)
      Button      b0 = new Button("Display");                      //---------------------------- (c)
      Label[]     la = new Label[]{new Label ("Red"),              //---------------------------- (d)
                       new Label ("Green"),
                       new Label ("Blue")  };
      TextField[] ta = new TextField[]{new TextField(),            //---------------------------- (e)
                        new TextField();
                        new TextField()};
      Label       x0 = new Label("Input RGB values [0<-->255]");   //---------------------------- (f)
            public ColorRGBa() {                                         //---------- (g)
                setLayout(null);                                         //---------- (h)
                for(int i = 0; i < la.length; i++) {
                    add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);  //---------- (i)
                    add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);  //---------- (j)
                }
            add(b0); b0.setBounds(10, 160, 80, 30);                      //---------- (k)
            add(x0); x0.setBounds(10, 200, 220, 40);                     //---------- (l)

            b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try {                                                //---------- (m)
                        x0.setBackground(new Color(                      //---------- (n)
                        (new Integer(ta[0].getText())).intValue(),
                        (new Integer(ta[1].getText())).intValue(),
                        (new Integer(ta[2].getText())).intValue()));
                    } catch(Exception ex) { x0.setText(ex.toString()); } //---------- (o)
                    }
                });
            }

            public static void main(String[] args) {
                Frame win = new ColorRGBa();
                win.setSize(240, 280); win.setVisible(true);             //---------- (p)
              win.addWindowListener(new WindowAdapter() {              //---------- (q)
                    public void windowClosing(WindowEvent evt) {
                        System.exit(0);                                  //---------- (r)
                    }
              });
            }
          }
	    }

考察

(a)-GUI関係のパッケージクラス群をインポート
(b)-Frameクラスの継承
(c)-インスタンス変数としてボタンを生成・初期化
(d)-表示欄を生成・初期化
(e)-入力欄を生成・初期化
(f)-表示欄を生成・初期化
(g)-コンストラクタでウィンドウの初期化
(h)-部品の自動配置機能をoffにする
(i)-表示欄の張り付け
(j)-入力欄の張り付け
(k)-ボタン欄の張り付け
(l)-表示欄の貼り付け
(m)-tryで例外を検査
(n)-色の設定
(o)-数字以外が入力されるとエラーが起きる
(p)-ウィンドウのサイズディスプレイへ表示
(q)-ウィンドウを閉じる動作
(r)-ウィンドウを閉じるとシステム終了

Javaでは、プログラム実行時のエラー(例外)が発生した場合に「例外処理」という機能で処理する。

try { // 例外を検出する文をここに書く      } catch (例外のタイプ1 変数名) { // 例外のタイプ1が発生したとき処理する文をここに書く      } catch (例外のタイプ2 変数名) { // 例外のタイプ2が発生したとき処理する文をここに書く      }           ・           ・           ・                 catch (例外のタイプn 変数名) { // 例外のタイプnが発生したとき処理する文をここに書く      } finally { // 例外発生の有無にかかわらず必ず実行する文をここに書く      }

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

GUICF.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  import java.awt.*;
  import java.awt.event.*;
  
  public class GUICF extends Frame{
      Button b0 = new Button("C to F");
      Button b1 = new Button("F to C");
  
      Label  x0 = new Label("Result");
      Label  x1 = new Label("");

      TextField t0 = new TextField("");
  
      public GUICF(){
  
      setLayout(null);

      add(x0); x0.setBounds(90, 50, 50, 10);
      add(x1); x1.setBounds(140, 50, 100, 10);
  
      add(t0); t0.setBounds(20, 50, 60, 20);
  
      add(b0); b0.setBounds(10, 100, 100, 30);
      add(b1); b1.setBounds(110,100, 100, 30);

      b0.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent evt){
              try{
                  int i = (new Integer(t0.getText())).intValue();
                  i = (int)(1.8 * i + 32);       
                  x1.setText(i + " F");
              }catch(NumberFormatException evn){
              x1.setText("Error!! Put in a number");
              }
          }
      });

      b1.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent evt) {
              try{
                  int j = (new Integer(t0.getText())).intValue();
                  j = (int)((j - 32)/1.8);          
                  x1.setText(j + " C");
                  }catch(NumberFormatException evn){
                  x1.setText("Error!! Put in a number");
                  }
              }
          });
      }

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

3,上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
5,「電卓」プログラム。中身は自分の思うように。

GUI.java

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import java.awt.*;
import java.awt.event.*;

public class GUI extends Frame {
    Button    ba = new Button("+");
    Button    bb = new Button("-");
    Button    bc = new Button("x");
    Button    bd = new Button("/");
    Button    be = new Button("%");
    Button    bf = new Button("=");
    Button    bg = new Button("Clear");
    Button    b0 = new Button("0");
    Button    b1 = new Button("1");
    Button    b2 = new Button("2");
    Button    b3 = new Button("3");
    Button    b4 = new Button("4");
    Button    b5 = new Button("5");
    Button    b6 = new Button("6");
    Button    b7 = new Button("7");
    Button    b8 = new Button("8");
    Button    b9 = new Button("9");
    Button    b10= new Button("00");
    Label     x0 = new Label("SisokuEnzan");
    Label     x1 = new Label("Result!");
    Label     xx = new Label("");
    TextField t0 = new TextField("0");

    public GUI() {
        setLayout(null);
        add(x0); x0.setBounds(10,30,80,30);
        add(x1); x1.setBounds(90,30,80,30);
        add(t0); t0.setBounds(180,30,100,30);
        add(ba); ba.setBounds(220,230,80,30);
        add(bb); bb.setBounds(220,190,80,30);
        add(bc); bc.setBounds(220,150,80,30);
        add(bd); bd.setBounds(220,110,80,30);
        add(be); be.setBounds(220,70,80,30);
        add(bf); bf.setBounds(160,230,60,30);
        add(bg); bg.setBounds(40,70,180,30);
        add(b0); b0.setBounds(40,230,60,30);
        add(b1); b1.setBounds(40,190,60,30);
        add(b2); b2.setBounds(100,190,60,30);
        add(b3); b3.setBounds(160,190,60,30);
        add(b4); b4.setBounds(40,150,60,30);
        add(b5); b5.setBounds(100,150,60,30);
        add(b6); b6.setBounds(160,150,60,30);
        add(b7); b7.setBounds(40,110,60,30);
        add(b8); b8.setBounds(100,110,60,30);
        add(b9); b9.setBounds(160,110,60,30);
        add(b10); b10.setBounds(100,230,60,30);
        ba.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    x0.setText("  +  ");
                    x1.setText(t0.getText());
                    t0.setText("0");
                    xx.setText("a");
                }
            });
        bb.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    x0.setText("  -  ");
                    x1.setText(t0.getText());
                    t0.setText("0");
                    xx.setText("b");
                }
            });
        bc.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    x0.setText("  x  ");
                    x1.setText(t0.getText());
                    t0.setText("0");
                    xx.setText("c");
                }
            });
        bd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    x0.setText("  /  ");
                    x1.setText(t0.getText());
                    t0.setText("0");
                    xx.setText("d");
                }
            });
        be.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    x0.setText("  %  ");
                    x1.setText(t0.getText());
                    t0.setText("0");
                    xx.setText("e");
                }
            });
        bf.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(x1.getText());
                    int j = Integer.parseInt(t0.getText());
                    String S = xx.getText();
                    if(S == "a"){
                        int sum = i + j;
                        x0.setText("add:");
                        String s = Integer.toString(sum);
                        x1.setText(s);
                    }
                    else if(S == "b"){
                        int sum = i - j;
                        x0.setText("substract:");
                        String s = Integer.toString(sum);
                        x1.setText(s);}
                    else if(S == "c"){
                        int sum = i * j;
                        x0.setText("multiply:");
                        String s = Integer.toString(sum);
                        x1.setText(s);
                    }
                    else if(S == "d"){
                        int sum = i / j;
                        x0.setText("divide:");
                        String s = Integer.toString(sum);
                        x1.setText(s);
                    }
                    else if(S == "e"){
                        int sum = i % j;
                        x0.setText("surplus:");
                        String s = Integer.toString(sum);
                        x1.setText(s);
                    }
                    else {
                        String s = Integer.toString(i);
                        t0.setText(s);
                    }
                }
            });
        bg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    t0.setText("0");
                    x0.setText("KEISAN");
                    x1.setText("SHITE!");
                }
            });
        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+1;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
        b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+2;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+3;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b4.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+4;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b5.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+5;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b6.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+6;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b7.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+7;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b8.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+8;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b9.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*10+9;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
        b10.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    int i = Integer.parseInt(t0.getText());
                    int sum = i*100;
                    String s = Integer.toString(sum);
                    t0.setText(s);
                }
            });
    }
    public static void main(String[] args) {
        Frame win = new GUI();
        win.setSize(330,300);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}

感想

5の課題が特に疲れました。

参考文献