1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
1. import java.awt.*; 2. import java.awt.event.*; 3. 4. public class GUIaa extends Frame { 5. Button b0 = new Button("Enter"); 6. Label x0 = new Label("数字を入力してください"); 7. TextField t0 = new TextField(); 8. 9. public GUIaa() { 10. setLayout(null); 11. add(t0); t0.setBounds(10, 40, 90, 30); 12. add(b0); b0.setBounds(110, 40, 80, 30); 13. add(x0); x0.setBounds(10, 80, 180, 30); 14. b0.addActionListener(new ActionListener() { 15. public void actionPerformed(ActionEvent evt) { 16. int i = (new Integer(t0.getText())).intValue(); 17. t0.setText(""); 18. if(i % 2 == 0) { 19. x0.setText(i + "は偶数です。"); 20. } else { 21. x0.setText(i + "は奇数です。"); 22. } 23. } 24. }); 25. } 26. public static void main(String[] args) { 27. Frame win = new GUIaa(); 28. win.setSize(200, 150); win.setVisible(true); 29. win.addWindowListener(new WindowAdapter() { 30. public void windowClosing(WindowEvent evt) { 31. System.exit(0); 32. } 33. }); 34. } 35. } |
[実行結果]
[考察]
1.GUIとは?
a、Graphical User Interfaceの頭文字を取って略したもの。
文字が並んだだけの(Character User Interface : CUI)の逆。
b、キーボード以外の操作も可能。
これにより操作方が増す。
2.プログラムの考察
a、1、2行目の記述はGUIプログラムには必要なものです。
import java.awt.*;
//GUI関係のパッケージクラス群をインポートする。
import java.awt.event.*;
//アクションリスナー(イベント処理)を使うのに必要
b、グラフィックの出力
Button b0 = new Button("Enter");
これでボタンを作成。末尾の括弧内の文字はそのままボタンの中に出力される。
Label x0 = new Label("数字を入力してください");
ラベルの作成。括弧内の文字をラベルとして出力。
TextField t0 = new TextField();
ウィンドウの作成。
c、グラフィックの配置
add(t0); t0.setBounds(10, 40, 90, 30);
t0(ボタン)の配置。末尾の括弧内は順に(ウィンドウの左上を中心にx座標、y座標、横幅、縦幅)以下同じ。
d、イベント
b0.addActionListener(new ActionListener() {
ボタンが押された時の動作クラス
public void actionPerformed(ActionEvent evt) {
int i = (new Integer(t0.getText())).intValue();
ここからは値の処理で入力された数値をint型で返し、その値をiに代入。
if(i % 2 == 0) {
x0.setText(i + "は偶数です。");
} else {
x0.setText(i + "は奇数です。");
数値判定。iが2で割り切れたら偶数それ以外は奇数と判定。
それぞれ「…は偶数です。/奇数です。」と出力。
Frame win = new GUIaa();
Frameをwinに変換し、winの作成。
win.setSize(200, 150); win.setVisible(true);
ウィンドウのサイズを定義して、画面に出力。
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
画面を閉じるとアプリの終了。
2.例外処理について、考察せよ。
例外処理とは、Javaが実行時のエラーを処理するための手段です。例外処理の基礎となるのはtryブロックであり、 例外を発生させるコードをここに記述できる。try{ //センシティブなコード } catch (Exception1Type e1){ //例外タイプ1を処理する } catch (Exception1Type e1){ //例外タイプ2を処理する } … finally { //tryブロックが終わる前に実行すべきコード } |
3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
import java.awt.*; import java.awt.event.*; public class R7_2 extends Frame { Button b0 = new Button("GO!"); Label x0 = new Label("今回の授業はどうでしたか?"); Label x1 = new Label("名前を入力してください。"); Label x2 = new Label("性別を入力してください。"); Label x3 = new Label("レポート7は終わりましたか?"); Label x4 = new Label("プログラミング2の感想を書いてください。"); Label x5 = new Label("★☆授業評価☆★"); TextField t0 = new TextField(); Checkbox c0 = new Checkbox("はい(^_^)"); Checkbox d0 = new Checkbox("いいえ(-_-)"); List l1 = new List(); TextArea t1 = new TextArea(""); Choice c1 = new Choice(); public R7_2() { setLayout(null); add(b0); b0.setBounds(300, 500, 80, 30); add(x0); x0.setBounds(10, 100, 90, 30); add(x1); x1.setBounds(10, 30, 90, 30); add(x2); x2.setBounds(200, 30, 90, 30); add(x3); x3.setBounds(10, 170, 90, 30); add(x4); x4.setBounds(10, 250, 90, 30); add(x5); x5.setBounds(10, 500, 90, 40); add(t0); t0.setBounds(10, 50, 150, 25); add(c0); c0.setBounds(10, 200, 90, 30); add(d0); d0.setBounds(90, 200, 90, 30); add(l1); l1.setBounds(200, 50, 65, 40); l1.add("男♂"); l1.add("女♀"); add(t1); t1.setBounds(10, 270, 420, 200); add(c1); c1.setBounds(10, 120, 120, 30); c1.add("超、難しい"); c1.add("ちと難しい"); c1.add("普通"); c1.add("わりと簡単"); c1.add("超、簡単"); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { x5.setText("ありがとうございました。"); } }); } public static void main(String[] args) { Frame win = new R7_2(); win.setSize(450, 550); win.setVisible(true); } } |
[実行結果]
4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
1. import java.awt.*; 2. import java.awt.event.*; 3. 4. public class henkan extends Frame{ 5. 6. Button b0 = new Button("華氏↓"); 7. Button b1 = new Button("摂氏↓"); 8. Label x0 = new Label("変換前"); 9. Label x1 = new Label("変換後"); 10. 11. TextField t0 = new TextField(); 12. TextField t1 = new TextField(); 13. 14. public henkan(){ 15. setLayout(null); 16. add(t0); t0.setBounds(90,60,50,20); 17. add(b0); b0.setBounds(65,85,50,20); 18. add(x0); x0.setBounds(40,60,50,20); 19. add(b1); b1.setBounds(115,85,50,20); 20. add(t1); t1.setBounds(90,110,50,20); 21. add(x1); x1.setBounds(40,110,50,20); 22. 23. b0.addActionListener(new ActionListener() { 24. public void actionPerformed(ActionEvent evt) { 25. try{ 26. int i = (new Integer(t0.getText())).intValue(); 27. t1.setText("" + (i*1.8+32)); 28. }catch(Exception ex){ 29. t1.setText("error"); 30. } 31. } 32. }); 33. 34. b1.addActionListener(new ActionListener() { 35. public void actionPerformed(ActionEvent evt) { 36. try{ 37. int i = (new Integer(t0.getText())).intValue(); 38. t1.setText("" + (i-32)/1.8); 39. }catch(Exception ex){ 40. t1.setText("error"); 41. } 42. } 43. }); 44. } 45. 46. public static void main(String[] args) { 47. Frame win = new henkan(); 48. win.setSize(230, 200); win.setVisible(true); 49. win.addWindowListener(new WindowAdapter() { 50. public void windowClosing(WindowEvent evt) { 51. System.exit(0); 52. } 53. }); 54. } 55. } |
[実行結果]
5.「電卓」プログラム。中身は自分の思うように。
import java.awt.*; import java.awt.event.*; public class calculater 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 calculater(){ 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 calculater(); win.setSize(230, 200); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
[実行結果]
6.感想
今回もかなり苦労させられました…テスト期間の前に仕上げておきたかったので急いでやったわりには自信作です (冬休みに仕上げとけば…)。でもなんとか仕上がってよかったです。この調子で8も頑張ります!7.参考文献・HP
・独習Java第2版 『ジョゼフ・オニール著』