Report#5

GUIプログラム{〜01/15(Mon)}

課題(講義資料を参考にすること)

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


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

偶数奇数判定プログラム(GUIaa)ソースコード

01: import java.awt.*;
02: import java.awt.event.*;

03: public class GUIaa extends Frame {
04:     Button    b0 = new Button("Even/Odd?");
05:     Label     x0 = new Label("Type a number and press...");
06:     TextField t0 = new TextField();
    
07:     public GUIaa() {
08:         setLayout(null);
09:         add(t0); t0.setBounds(10, 40, 90, 30);
10:         add(b0); b0.setBounds(110, 40, 80, 30);
11:         add(x0); x0.setBounds(10, 80, 180, 30);
12:         b0.addActionListener(new ActionListener() {
13:             public void actionPerformed(ActionEvent evt) {
14:                 int i = (new Integer(t0.getText())).intValue();
15:                 t0.setText("");
16:                 if(i % 2 == 0) {
17:                     x0.setText(i + " is Even");
18:                 } else {
19:                     x0.setText(i + " is Odd");
20:                 }
21:             }
22:         });
23:     }
24:     public static void main(String[] args) {
25:         Frame win = new GUIaa();
26:         win.setSize(200, 150); win.setVisible(true);
27:         win.addWindowListener(new WindowAdapter() {
28:             public void windowClosing(WindowEvent evt) {
29:                 System.exit(0);
30:             }
31:         });
32:     }
33: }

実行結果



偶数奇数判定プログラム(GUIaa)考察

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

例外処理のサンプルプログラム ColorRGBa のソースコード

01: import java.awt.*;
02: import java.awt.event.*;

03: public class ColorRGBa extends Frame {
04:     Button      b0 = new Button("Display");
05:     Label[]     la = new Label[]{new Label ("Red"),
06:                                  new Label ("Green"),
07:                                  new Label ("Blue")  };
08:     TextField[] ta = new TextField[]{new TextField(),
09:                                      new TextField(),
10:                                      new TextField()};
11:     Label       x0 = new Label("Input RGB values [0..255]");
    
12:     public ColorRGBa() {
13:         setLayout(null);
14:         for(int i = 0; i < la.length; i++) {
15:             add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);
16:             add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);
17:         }
18:         add(b0); b0.setBounds(10, 160, 60, 30);
19:         add(x0); x0.setBounds(10, 200, 180, 30);
20:         b0.addActionListener(new ActionListener() {
21:             public void actionPerformed(ActionEvent evt) {
22:                 try {
23:                     x0.setBackground(new Color(
24:                         (new Integer(ta[0].getText())).intValue(),
25:                         (new Integer(ta[1].getText())).intValue(),
26:                         (new Integer(ta[2].getText())).intValue()));
27:                 } catch(Exception ex) { x0.setText(ex.toString()); }
28:             }
29:         });
30:     }
31:     public static void main(String[] args) {
32:         Frame win = new ColorRGBa();
33:         win.setSize(200, 250); win.setVisible(true);
34:         win.addWindowListener(new WindowAdapter() {
35:             public void windowClosing(WindowEvent evt) {
36:                 System.exit(0);
37:             }
38:         });
39:     }
40: }

実行結果

初期


通常動作


エラー発生(例外処理)


例外処理のサンプルプログラム ColorRGBa 考察

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

上述のGUI部品を全て使ったプログラム

01: import java.awt.*;
02: import java.awt.event.*;

03: public class allsample extends Frame {
04:     Button    b0 = new Button("self-introduction complete");
05:     Label     x0 = new Label("please enter your name.");
06:     Label     x1 = new Label("Birth");
07:     Label     x2 = new Label("Gender");
08:     TextField t0 = new TextField();
09:     TextField t1 = new TextField();
10:     Checkbox  c0 = new Checkbox("Is not the mistake found?");
11:     List      l1 = new List();
12:     TextArea  te1 = new TextArea("This is self-introduction program.");
13:     Choice    c1 = new Choice();

14:     public allsample() {
15:         setLayout(null);
16:         add(b0); b0.setBounds(10, 220, 200, 30);
17:         add(t1); t1.setBounds(10, 250, 250, 100);
18:         add(x0); x0.setBounds(10, 90, 200, 30);
19:         add(t0); t0.setBounds(10, 110, 160, 30);
20:         add(x1); x1.setBounds(10,150,60,10);
21:         add(x2); x2.setBounds(190,150,60,10);
22:         add(c0); c0.setBounds(10, 190, 200, 30);
23:         add(l1); l1.setBounds(10, 170, 100, 20);
24:         l1.add("January"); l1.add("February"); l1.add("March");
25:         l1.add("April"); l1.add("May"); l1.add("June"); l1.add("July");
26:         l1.add("August"); l1.add("September"); l1.add("October");
27:         l1.add("November"); l1.add("December");
28:         add(te1); te1.setBounds(10, 40, 240, 40);
29:         add(c1); c1.setBounds(190, 170, 90, 30);
30:         c1.add("male"); c1.add("female");
31:         b0.addActionListener(new ActionListener() {
32:                 public void actionPerformed(ActionEvent evt) {
33:                     try{
34:                         String answear = t0.getText();
35:                         t1.setText("This is " + answear + "'s easy introduction.");
36:                     }catch(Exception ex){
37:                         t1.setText("error");
38:                     }
39:                 }
40:             });
41:     }
42:     public static void main(String[] args) {
43:         Frame win = new allsample();
44:         win.setSize(300, 400);
45:         win.setVisible(true);
46:         win.addWindowListener(new WindowAdapter() {
47:                 public void windowClosing(WindowEvent evt) {
48:                     System.exit(0);
49:                 }
50:             });
51:     }
52: }

実行結果


考察

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

摂氏華氏変換プログラムソースコード

01: import java.awt.*;
02: import java.awt.event.*;

03: public class GUIc extends Frame {
04:     Button    b0 = new Button("C to F");
05:     Button    b1 = new Button("F to C");
06:     Label     x0 = new Label("temperature conversion");
07:     TextField t0 = new TextField();
08:
09:     public GUIc() {
10:         setLayout(null);
11:         add(t0); t0.setBounds(10, 40, 80, 30);
12:         add(b1); b0.setBounds(100, 80, 80, 30);
13:         add(b0); b1.setBounds(10, 80, 80, 30);
14:         add(x0); x0.setBounds(10, 120, 180, 30);
15:         b0.addActionListener(new ActionListener() {
16:                 public void actionPerformed(ActionEvent evt) {
17:                     try {
18:                         int i = (new Integer(t0.getText())).intValue();
19:                         {
20:                             x0.setText(i*1.8+32+"F");
21:                         }
22:                     }catch(Exception ex) {
23:                         x0.setText("Only the integer input");
24:                     }
25:                 }
26:             });
27:         b1.addActionListener(new ActionListener() {
28:                 public void actionPerformed(ActionEvent evt) {
29:                     try{
30:                         int i = (new Integer(t0.getText())).intValue();
31:                         {
32:                             x0.setText((i-32)/1.8+"C");
33:                         }
34:                     }catch(Exception ex){
35:                         x0.setText("Only the integer input");
36:                     }
37:                 }
38:             });
39:     }

40:     public static void main(String[] args) {
41:         Frame win = new GUIc();
42:         win.setSize(200, 160);
43:         win.setVisible(true);
44:         win.addWindowListener(new WindowAdapter() {
45:                 public void windowClosing(WindowEvent evt) {
46:                     System.exit(0);
47:                 }
48:             });
49:     }
50: }

実行結果

初期


F(華氏)からC(摂氏)へ


C(摂氏)からF(華氏)へ


例外処理時(エラー時)


摂氏華氏変換プログラム解説

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

「電卓」プログラムのソースコード

01:  import java.awt.*;
02:  import java.awt.event.*;

03:  public class Dentaku extends Frame {
04:      TextField t0 = new TextField();
05:      TextField t1 = new TextField();
06:      TextField t2 = new TextField();
07:      Label     x0 = new Label("X");
08:      Label     x1 = new Label("Y");
09:      Label     x2 = new Label("answer");
10:      Button    a0 = new Button("+");
11:      Button    a1 = new Button("-");
12:      Button    a2 = new Button("x");
13:      Button    a3 = new Button("/");
14:      Button    a4 = new Button("%");
15:      Button    a5 = new Button("AC");

16:      public Dentaku() {
17:          setLayout(null);
18:          add(x0); x0.setBounds(10, 40, 20, 10);
19:          add(t0); t0.setBounds(30, 40, 80, 30);
20:          add(x1); x1.setBounds(140, 40, 20, 10);
21:          add(t1); t1.setBounds(160, 40, 80, 30);
22:          add(a0); a0.setBounds(30, 80, 80, 30);
23:          add(a1); a1.setBounds(110, 80, 80, 30);
24:          add(a2); a2.setBounds(30, 100, 80, 30);
25:          add(a3); a3.setBounds(110, 100, 80, 30);
26:          add(a4); a4.setBounds(30, 120, 80, 30);
27:          add(a5); a5.setBounds(110, 120, 80, 30);
28:          add(x2); x2.setBounds(30, 140, 180, 30);
29:          a0.addActionListener(new ActionListener() {
30:                  public void actionPerformed(ActionEvent evt) {
31:                      try {
32:                          int i = (new Integer(t0.getText())).intValue();
33:                          int h = (new Integer(t1.getText())).intValue();
34:                          {
35:                              x2.setText(""+ (i+h) +"");
36:                          }
37:                      }catch(Exception ex) {
38:                          x2.setText("Only the integer input");
39:                      }
40:                  }
41:              });
42:          a1.addActionListener(new ActionListener() {
43:                  public void actionPerformed(ActionEvent evt) {
44:                      try {
45:                          int i = (new Integer(t0.getText())).intValue();
46:                          int h = (new Integer(t1.getText())).intValue();
47:                          {
48:                              x2.setText("" + (i-h) + "");
49:                          }
50:                      }catch(Exception ex) {
51:                          x2.setText("Only the integer input");
52:                      }
53:                  }
54:              });
55:          a2.addActionListener(new ActionListener() {
56:                  public void actionPerformed(ActionEvent evt) {
57:                      try {
58:                          int i = (new Integer(t0.getText())).intValue();
59:                          int h = (new Integer(t1.getText())).intValue();
60:                          {
61:                              x2.setText("" + i*h + "");
62:                          }
63:                      }catch(Exception ex) {
64:                          x2.setText("Only the integer input");
65:                      }
66:                  }
67:              });
68:          a3.addActionListener(new ActionListener() {
69:                  public void actionPerformed(ActionEvent evt) {
70:                      try {
71:                          int i = (new Integer(t0.getText())).intValue();
72:                          int h = (new Integer(t1.getText())).intValue();
73:                          {
74:                              x2.setText("" + i/h + "");
75:                          }
76:                      }catch(Exception ex) {
77:                          x2.setText("Only the integer input");
78:                      }
79:                  }
80:              });
81:          a4.addActionListener(new ActionListener() {
82:                  public void actionPerformed(ActionEvent evt) {
83:                      try {
84:                          int i = (new Integer(t0.getText())).intValue();
85:                          int h = (new Integer(t1.getText())).intValue();
86:                          {
87:                              x2.setText("" + i%h + "");
88:                          }
89:                      }catch(Exception ex) {
90:                          x2.setText("Only the integer input");
91:                      }
92:                  }
93:              });
94:          a5.addActionListener(new ActionListener() {
95:                  public void actionPerformed(ActionEvent evt) {
96:                      t0.setText("0");
97:                      t1.setText("0");
98:                      x2.setText("0");
99:                  }
100:             });
101:     }
102:     public static void main(String[] args) {
103:         Frame win = new Dentaku();
104:         win.setSize(250, 200); win.setVisible(true);
105:         win.addWindowListener(new WindowAdapter() {
106:                 public void windowClosing(WindowEvent evt) {
107:                     System.exit(0);
108:                 }
109:             });
110:     }
111: }

実行結果

初期


足し算


引き算


かけ算


割り算


余り算


クリア


例外処理時(エラー時)


「電卓」プログラムの考察

反省・感想


戻る。