1. 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
1: import java.awt.*;
2: import java.awt.event.*;
3:
4: public class GUIaa extends Frame {
5: Button b0 = new Button("Even/Odd?");
6: Label x0 = new Label("Type a number and press...");
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 + " is Even");
20: } else {
21: x0.setText(i + " is Odd");
22: }
23: }
24: });
25: }
26: public static void main(String[] args) {
27: Frame win = new GUIaa();
28: win.setSize(200, 150);
29: win.setVisible(true);
30: win.addWindowListener(new WindowAdapter() {
31: public void windowClosing(WindowEvent evt) {
32: System.exit(0);
33: }
34: });
35: }
36: }
・実行結果
考察
・プログラムの流れ
4: public class GUIaa extends Frame {
Frameクラスを拡張してGUIaaクラスを宣言する。
5: Button b0 = new Button("Even/Odd?");
Buttonオブジェクトを作成。ここで作成したButtonには、Even/odd?と表示させる。
6: Label x0 = new Label("Type a number and press...");
Labelオブジェクトを作成。ここで作成したLabelには、Type a number and press...と表示させる。
7: TextField t0 = new TextField();
TextFieldオブジェクトを作成。
9: public GUIaa() {
GUIaaコンストラクタを宣言。
10: setLayout(null);
このコンテナのレイアウトマネージャのディフォルト設定の解除。
11: add(t0); t0.setBounds(10, 40, 90, 30);
コンテナにt0を追加し、t0を返す。setBoundsは、指定位置に指定サイズのコンポーネントを設定する。
setBounds(int x,int y,int w,int h)で宣言し、xにはx座標をyにはy座標、wには幅、hには高さの値を設定する。
12: add(b0); b0.setBounds(100, 40, 100, 30);
コンテナにb0を追加し、b0を返す。setBoundsは、指定位置に指定サイズのコンポーネントを設定する。
setBounds(int x,int y,int w,int h)で宣言し、xにはx座標をyにはy座標、wには幅、hには高さの値を設定する。
13: add(x0); x0.setBounds(10, 80, 180, 30);
コンテナにx0を追加し、x0を返す。setBoundsは、指定位置に指定サイズのコンポーネントを設定する。
setBounds(int x,int y,int w,int h)で宣言し、xにはx座標をyにはy座標、wには幅、hには高さの値を設定する。
14: b0.addActionListener(new ActionListener() {
アクションイベントを受け取るリスナーの追加。
15: public void actionPerformed(ActionEvent evt) {
アクションイベントの参照。
16: int i = (new Integer(t0.getText())).intValue();
t0に入力された文字をint型にキャストし、iに代入する。
getText()は、textに入力された文字を読み取る。
intValue()は、現在のオブジェクトの値をint型で返す。
17: t0.setText("");
0tに空白を表示。setText()は、テキストの書き込み。("")なので、空白になる。
18: if(i % 2 == 0) {
19: x0.setText(i + " is Even");
iを2で割り、その余りの有無で偶数か奇数かを調べる。
余りが0だと、偶数なのでiと"is Even"を表示する。
20: } else {
21: x0.setText(i + " is Odd");
22: }
余りの値が0意外なら、奇数になるのでiと"is Odd"を表示する。
26: public static void main(String[] args) {
mainメソッドの宣言。mainメソッドが、実行開始位置である。
27: Frame win = new GUIaa();
winという新しいフレームを作成する。
28: win.setSize(200, 150);
29: win.setVisible(true);
新しく作ったフレームのサイズをsetSizeで定義する。幅200、高さ150に定義する。
setVisible()は、コンポーネントの表示・非表示を設定する。
30: win.addWindowListener(new WindowAdapter() {
ウィンドウイベントを受け取るためにオブジェクトを登録する。
31: public void windowClosing(WindowEvent evt) {
32: System.exit(0);
ウィンドウのクローズを要求したときの処理。System.exit(0)で、ウィンドウが閉じられる。
2. 例外処理について、考察せよ。
1: import java.awt.*;
2: import java.awt.event.*;
3:
4: public class ColorRGBa extends Frame {
5: Button b0 = new Button("Display");
6: Label[] la = new Label[]{new Label ("Red"),
7: new Label ("Green"),
8: new Label ("Blue") };
9: TextField[] ta = new TextField[]{new TextField(),
10: new TextField(),
11: new TextField()};
12: Label x0 = new Label("Input RGB values [0..255]");
13:
14: public ColorRGBa() {
15: setLayout(null);
16: for(int i = 0; i < la.length; i++) {
17: add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);
18: add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);
19: }
20: add(b0); b0.setBounds(10, 160, 80, 30);
21: add(x0); x0.setBounds(10, 200, 180, 30);
22: b0.addActionListener(new ActionListener() {
23: public void actionPerformed(ActionEvent evt) {
24: try {
25: x0.setBackground(new Color(
26: (new Integer(ta[0].getText())).intValue(),
27: (new Integer(ta[1].getText())).intValue(),
28: (new Integer(ta[2].getText())).intValue()));
29: } catch(Exception ex) { x0.setText(ex.toString()); }
30: }
31: });
32: }
33: public static void main(String[] args) {
34: Frame win = new ColorRGBa();
35: win.setSize(200, 250);
36: win.setVisible(true);
37: win.addWindowListener(new WindowAdapter() {
38: public void windowClosing(WindowEvent evt) {
39: System.exit(0);
40: }
41: });
42: }
43: }
・実行結果
考察
・プログラムについて
4: public class ColorRGBa extends Frame {
Frameクラスを拡張してColorRGBaクラスを宣言。
5: Button b0 = new Button("Display");
新しくButtonオブジェクトを作成。b0というButtonには、Displayと表示させる。
6: Label[] la = new Label[]{new Label ("Red"),
7: new Label ("Green"),
8: new Label ("Blue") };
新しくLabelオブジェクトを作成。laというLabelは配列で宣言されていて、それぞれ"Red"・"Green"・"Blue"と表示させる。
9: TextField[] ta = new TextField[]{new TextField(),
10: new TextField(),
11: new TextField()};
新しくTextFieldオブジェクトを作成。taというTextFieldは、配列で宣言されている。
12: Label x0 = new Label("Input RGB values [0..255]");
新しくLabelオブジェクトを作成。x0というLabelには、"Input RGB values [0...255]"と表示させる。
14: public ColorRGBa() {
ColorRGBaコンストラクタを宣言。
15: setLayout(null);
このコンテナのレイアウトマネージャのディフォルト設定の解除。
16: for(int i = 0; i < la.length; i++) {
17: add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);
18: add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);
19: }
iが0から始まり、1ずつ増やしlaの長さより小さい間、処理を続ける。laは配列なので、lengthで配列の長さを得る。
コンテナにla[i]を追加する。setBoundsは追加されたla[i]を、指定位置に指定サイズのコンポーネントを設定する。
コンテナにta[i]を追加する。setBoundsは追加されたta[i]を、指定位置に指定サイズのコンポーネントを設定する。
20: add(b0); b0.setBounds(10, 160, 80, 30);
コンテナにb0を追加する。setBoundsは追加されたb0を、指定位置に指定サイズのコンポーネントを設定する。
21: add(x0); x0.setBounds(10, 200, 180, 30);
コンテナにx0を追加する。setBoundsは追加されたx0を、指定位置に指定サイズのコンポーネントを設定する。
22: b0.addActionListener(new ActionListener() {
アクションイベントを受け取るリスナーの追加。
23: public void actionPerformed(ActionEvent evt) {
アクションイベントの参照。
24: try {
例外処理の開始。
25: x0.setBackground(new Color(
26: (new Integer(ta[0].getText())).intValue(),
27: (new Integer(ta[1].getText())).intValue(),
28: (new Integer(ta[2].getText())).intValue()));
setBackgroundは、背景色の設定である。Color(int r,int g,int b)は、r(赤)g(緑)b(青)の各成分を0~255の範囲で与えRGB色を作る。
int rの部分が、(new Integer(ta[0].getText())).intValue()なので、ta[0]の値をint型に変換し、代入する。赤の成分
int gの部分が、(new Integer(ta[1].getText())).intValue()なので、ta[1]の値をint型に変換し、代入する。緑の成分
int bの部分が、(new Integer(ta[2].getText())).intValue()なので、ta[2]の値をint型に変換し、代入する。青の成分
29: } catch(Exception ex) { x0.setText(ex.toString()); }
Exceptionは、正しく例外処理を行って、動作が継続することを期待するときになげられる。例外が起きたとき、その例外の内容をx0に表示する。
33: public static void main(String[] args) {
mainメソッドの宣言。mainメソッドが、実行開始位置である。
34: Frame win = new ColorRGBa();
winという新しいフレームを作成。
35: win.setSize(200, 250);
36: win.setVisible(true);
新しく作ったフレームのサイズをsetSizeで定義する。幅200、高さ150に定義する。
setVisible()は、コンポーネントの表示・非表示を設定する。
37: win.addWindowListener(new WindowAdapter() {
ウィンドウイベントを受け取るためにオブジェクトを登録する。
38: public void windowClosing(WindowEvent evt) {
39: System.exit(0);
ウィンドウのクローズを要求したときの処理。System.exit(0)で、ウィンドウが閉じられる。
5. 「電卓」プログラム。中身は自分の思うように。
1: import java.applet.*;
2: import java.awt.*;
3: import java.awt.event.*;
4:
5: public class Cal extends Applet implements ActionListener {
6:
7: Label lab;
8: Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
9: int x0,y0,w,h;
10: long acc,num;
11: String op,s;
12:
13: public void init() {
14:
15: // レイアウトマネージャのディフォルト設定を解除。
16: this.setLayout(null);
17: // パラメータ。
18: x0 = 20; // 電卓の左上隅のx座標
19: y0 = 20; // 電卓の左上隅のy座標
20: w = 50; // 数字キーボタンの幅
21: h = 30; // 数字キーボタンの高さ
22:
23: // ラベルの作成。
24: lab = new Label("",Label.RIGHT);
25: lab.setBackground(Color.lightGray); // ラベルの背景色を設定
26: lab.setForeground(Color.black); // ラベルの前景色を設定
27:
28: // ラベルの配置。
29: lab.setBounds(x0,y0,4*w,h);
30: this.add(lab);
31:
32: // 数字キー、演算キー、=キー、クリアキーの作成。
33: b0 = new Button("0");
34: b1 = new Button("1");
35: b2 = new Button("2");
36: b3 = new Button("3");
37: b4 = new Button("4");
38: b5 = new Button("5");
39: b6 = new Button("6");
40: b7 = new Button("7");
41: b8 = new Button("8");
42: b9 = new Button("9");
43: b10 = new Button("÷");
44: b11 = new Button("×");
45: b12 = new Button("−");
46: b13 = new Button("+");
47: b14 = new Button("C");
48: b15 = new Button("=");
49:
50: // 数字キー、演算キー、=キー、クリアキーの配置。
51: b7.setBounds(x0,y0+h,w,h);
52: b4.setBounds(x0,y0+2*h,w,h);
53: b1.setBounds(x0,y0+3*h,w,h);
54: b14.setBounds(x0,y0+4*h,w,h);
55: b8.setBounds(x0+w,y0+h,w,h);
56: b5.setBounds(x0+w,y0+2*h,w,h);
57: b2.setBounds(x0+w,y0+3*h,w,h);
58: b0.setBounds(x0+w,y0+4*h,w,h);
59: b9.setBounds(x0+2*w,y0+h,w,h);
60: b6.setBounds(x0+2*w,y0+2*h,w,h);
61: b3.setBounds(x0+2*w,y0+3*h,w,h);
62: b15.setBounds(x0+2*w,y0+4*h,w,h);
63: b10.setBounds(x0+3*w,y0+h,w,h);
64: b11.setBounds(x0+3*w,y0+2*h,w,h);
65: b12.setBounds(x0+3*w,y0+3*h,w,h);
66: b13.setBounds(x0+3*w,y0+4*h,w,h);
67:
68: this.add(b0);
69: this.add(b1);
70: this.add(b2);
71: this.add(b3);
72: this.add(b4);
73: this.add(b5);
74: this.add(b6);
75: this.add(b7);
76: this.add(b8);
77: this.add(b9);
78: this.add(b10);
79: this.add(b11);
80: this.add(b12);
81: this.add(b13);
82: this.add(b14);
83: this.add(b15);
84:
85: // =キーの設定。
86: b15.setBackground(Color.white); // =キーの背景色を設定。
87: b15.setForeground(Color.blue); // =キーの前景色を設定。
88:
89: // クリアキーの設定。
90: b14.setBackground(Color.white); // クリアキーの背景色を設定。
91: b14.setForeground(Color.red); // クリアキーの前景色を設定。
92:
93: // イベントリスナの登録。
94: b0.addActionListener(this);
95: b1.addActionListener(this);
96: b2.addActionListener(this);
97: b3.addActionListener(this);
98: b4.addActionListener(this);
99: b5.addActionListener(this);
100: b6.addActionListener(this);
101: b7.addActionListener(this);
102: b8.addActionListener(this);
103: b9.addActionListener(this);
104: b10.addActionListener(this);
105: b11.addActionListener(this);
106: b12.addActionListener(this);
107: b13.addActionListener(this);
108: b14.addActionListener(this);
109: b15.addActionListener(this);
110:
111: // 演算機能の初期設定。
112: num = 0;
113: acc = 0;
114:
115: }
116:
117: // ActionListenerインターフェースのメソッド。
118: public void actionPerformed(ActionEvent e) {
119: if(e.getSource() == b0) {
120: num = num * 10 + 0; s = "" + num;
121: }
122:
123: if(e.getSource() == b1) {
124: num = num * 10 + 1; s = "" + num;
125: }
126:
127: if(e.getSource() == b2) {
128: num = num * 10 + 2; s = "" + num;
129: }
130:
131: if(e.getSource() == b3) {
132: num = num * 10 + 3; s = "" + num;
133: }
134:
135: if(e.getSource() == b4) {
136: num = num * 10 + 4; s = "" + num;
137: }
138:
139: if(e.getSource() == b5) {
140: num = num * 10 + 5; s = "" + num;
141: }
142:
143: if(e.getSource() == b6) {
144: num = num * 10 + 6; s = "" + num;
145: }
146:
147: if(e.getSource() == b7) {
148: num = num * 10 + 7; s = "" + num;
149: }
150:
151: if(e.getSource() == b8) {
152: num = num * 10 + 8; s = "" + num;
153: }
154:
155: if(e.getSource() == b9) {
156: num = num * 10 + 9; s = "" + num;
157: }
158:
159: if(e.getSource() == b10) {
160: acc = num; num = 0; s = "" + acc; op = "÷";
161: }
162:
163: if(e.getSource() == b11) {
164: acc = num; num = 0; s = "" + acc; op = "×";
165: }
166:
167: if(e.getSource() == b12) {
168: acc = num; num = 0; s = "" + acc; op = "ー";
169: }
170:
171: if(e.getSource() == b13) {
172: acc = num; num = 0; s = "" + acc; op = "+";
173: }
174:
175: if(e.getSource() == b15) {
176: if(op.equals("+")) {
177: acc = acc + num;
178: }
179: if(op.equals("− ")) {
180: acc = acc - num;
181: }
182: if(op.equals("÷")) {
183: acc = acc / num;
184: }
185: if(op.equals("×")) {
186: acc = acc * num;
187: }
188:
189: s = "" + acc; num = acc; op = "";
190:
191: }
192:
193: if(e.getSource() == b14) {
194: acc = 0; num = 0; s = "0";
195: }
196:
197: lab.setText(s);
198:
199: }
200:
201: }
・実行結果
考察
・プログラムについて