report5
GUIプログラム:講義資料を参考に、以下の課題を行え。
1. 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。 2. 例外処理について、考察せよ。 3. 上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。 4. 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。。 5. 「電卓」プログラム。中身は自分の思うように。
課題1
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); } }); } } |
実行結果
課題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
import java.awt.*; import java.awt.event.*; public class report5_3 extends Frame { Button b0 = new Button("完了"); Label x0 = new Label("アンケート"); Label x1 = new Label("性別"); Label x2 = new Label("年齢"); Label x3 = new Label("職業"); Label x4 = new Label("お住まい"); Label x5 = new Label("・今日100円拾った"); Label x6 = new Label("・ガッペむかつく!"); Label x7 = new Label("・そこの角、右に曲がってな!"); Label x8 = new Label("・メモ欄"); Label x9 = new Label(""); TextField t0 = new TextField(); TextField t2= new TextField(); Checkbox c0 = new Checkbox("うん"); Checkbox d0 = new Checkbox("いや!1000円だし"); Checkbox c2 = new Checkbox("(?_?)"); Checkbox d2 = new Checkbox("へへっ"); Checkbox c3 = new Checkbox("あ、うん。"); Checkbox d3 = new Checkbox("左じゃなくて?"); List l1 = new List(); TextArea t1 = new TextArea(""); Choice c1 = new Choice(); public report5_3() { setLayout(null); add(b0); b0.setBounds(230, 530, 80, 30); add(x0); x0.setBounds(10, 20, 90, 30); add(x1); x1.setBounds(10, 35, 90, 30); add(x2); x2.setBounds(200, 35, 90, 30); add(x3); x3.setBounds(10, 100, 90, 30); add(x4); x4.setBounds(10, 150, 90, 30); add(x5); x5.setBounds(10, 210, 120, 30); add(x6); x6.setBounds(10, 260, 120, 30); add(x7); x7.setBounds(10, 310, 200, 30); add(x8); x8.setBounds(10, 360, 90, 30); add(x9); x9.setBounds(10,600,250,30); add(t0); t0.setBounds(10, 120, 180, 25); add(t2); t2.setBounds(200, 55, 50, 25); add(c0); c0.setBounds(10, 230, 90, 30); add(d0); d0.setBounds(90, 230, 150, 30); add(c2); c2.setBounds(10, 280, 90, 30); add(d2); d2.setBounds(90, 280, 90, 30); add(c3); c3.setBounds(10, 330, 90, 30); add(d3); d3.setBounds(90, 330, 150, 30); add(l1); l1.setBounds(10, 60, 65, 40); l1.add("男性"); l1.add("女性"); add(t1); t1.setBounds(10, 380, 300, 150); add(c1); c1.setBounds(10, 165, 120, 30); c1.add("北海道"); c1.add("アメリカ"); c1.add("渋谷"); c1.add("エジプト"); c1.add("西の方"); c1.add("大城さん家の隣"); c1.add("黄河周辺"); c1.add("3つ目の信号の角"); c1.add("沖縄"); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { x9.setText("どうでもいい情報ありがとう!"); } }); } public static void main(String[] args) { Frame win = new report5_3(); win.setSize(350, 570); win.setVisible(true); } } |
実行結果
課題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); } }); } } |
実行結果
(摂氏:℃ 華氏:F)
摂氏と華氏の関係 ℃=(F−32)/1.8 F=32+℃×1.8 |
課題5
import java.awt.*; import java.awt.event.*; public class report5_5 extends Frame{ Button b0 = new Button("+"); Button b1 = new Button("−"); Button b2 = new Button("×"); Button b3 = new Button("÷"); TextField t0 = new TextField(); TextField t1 = new TextField(); TextField t2 = new TextField(); TextField t3 = new TextField(); Label x0 = new Label("="); public report5_5(){ setLayout(null); add(t0); t0.setBounds(20,60,40,20); add(t1); t1.setBounds(80,60,40,20); add(t2); t2.setBounds(140,60,40,20); add(t3); t3.setBounds(60,60,20,20); add(b0); b0.setBounds(30,90,30,20); add(b1); b1.setBounds(65,90,30,20); add(b2); b2.setBounds(100,90,30,20); add(b3); b3.setBounds(135,90,30,20); add(x0); x0.setBounds(120,60,20,20); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("+"); t2.setText("" + (a+b)); }catch(Exception ex){ t1.setText("error"); } } }); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("−"); t2.setText("" + (a-b)); }catch(Exception ex){ t1.setText("error"); } } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("×"); t2.setText("" + (a*b)); }catch(Exception ex){ t1.setText("error"); } } }); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("÷"); t2.setText("" + (a/b)); }catch(Exception ex){ t1.setText("error"); } } }); } public static void main(String[] args) { Frame win = new report5_5(); win.setSize(230, 200); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
実行結果
感想
今回の課題はとても大変でした。残り1つなので頑張りたいです。