- 学籍番号:055704J
- 氏 名 :伊志嶺拓人
課題
- 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
- 例外処理について、考察せよ。
- 上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
- 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
- 「電卓」プログラム。中身は自分の思うように。
●プログラム
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);
}
});
}
}
●実行結果
●考察
GUIとは?
Graphical User Interfaceの頭文字を取って略したもの。
文字が並んだだけの(Character User Interface : CUI)の逆。
キーボード以外の操作も可能。
これにより操作方が増す
・Button・
ユーザーがボタン境界の内側でマウスをクリックすると、
ユーザーにフィードバックを提供するようにボタンの外観が変化する
・Label(String str, int align)・
strはラベルテキストで、alignは、ラベルを左揃え、中央揃え、右揃えのうちど
れにするかを示す定数。
定数LEFT,CENTER,RIGHTとして位置合わせの値を設定する。
GUI上に表示される文字列で、プログラムでは変更できますが、ユーザーが変更
することはできない
・TextField(String str, int cols)・
strはフィールドに入力されたテキストで、引数colsでは、フィールドの幅を字数で指定する。
TextFieldでは、1行のテキストを入力することができます。文字を入力すると、テキストイベントが生成される。
ENTERキーを押したときにアクションイベントを生成される。
・setLayout()・
要素を配置する座標を計算する必要がないので、時間の節約にとなり、ユーザーは間違いを起こしやすい作業を行わずにすむ。
2つ目にサイズを変更すると、要素の配置を動的に調整する。
3つ目にコンポーネントは一般的にプラットフォームによってサイズがことなる。
そのため、すべての環境に適した座標を一度に指定することはできない。
ここではsetLayout(null);になっているのでこれらの機能は失っている。
・ActionEvent・
ボタンを押したり、リスト項目をダブルクリックしたとき、メニュー項目を選択したときに生成される。
・int i = (new Integer(t0.getText())).intValue();・
t0.getText()でt0のテキストを読み込みIntegerで生成後のオブジェクトを不変なものとする。
・setSize();・
要素の数をsizeに変更する。sizeからはみ出した要素は破棄される。
・windowClosing・
ユーザーがウィンドウのクローズを要求するとwindowClosing()メソッドが呼び出される。
・WindowEvent・
ユーザーがウィンドウを活動化したとき、閉じたとき、非活動化したとき、
アイコン化解除したとき、アイコン化したとき、開いたとき、終了したときに生成される。
●グラフィックの出力
Button b0 = new Button("Enter");
これでボタンを作成。末尾の括弧内の文字はそのままボタンの中に出力される。
Label x0 = new Label("数字を入力してください");
ラベルの作成。括弧内の文字をラベルとして出力。
TextField t0 = new TextField();
ウィンドウの作成。
●グラフィックの配置
add(t0); t0.setBounds(10, 40, 90, 30);
t0(ボタン)の配置。末尾の括弧内は順に(ウィンドウの左上を中心にx座標、
y座標、横幅、縦幅)以下同じ。
●イベント
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);
画面を閉じるとアプリの終了。
例外処理とは、Javaが実行時のエラーを処理するための手段です。例外処理の基
礎となるのはtryブロックであり、 例外を発生させるコードをここに記述できる。
try文について。
tryブロック内で例外が発生した場合は、tryブロックの次に記述されている
catchブロックに対応する 例外処理が書かれてるかを検索します。 しかし、対
応する例外処理が書かれていなければそのtryブロックを囲っているさらに上の
tryブロックに書かれているctachブロックを検索します。 つまり、例外処理が
起きたメソッドを呼び出している呼び出し元のtry〜catch ブロックなどを探し
ます。
try{
//センシティブなコード
}
catch (Exception1Type e1){
//例外タイプ1を処理する
}
catch (Exception1Type e1){
//例外タイプ2を処理する
}
…
finally {
//tryブロックが終わる前に実行すべきコード
}
●プログラム
import java.awt.*;
import java.awt.event.*;
public class repo7_3 extends Frame {
Button b0 = new Button("送信");
Label x0 = new Label("iPod nano アンケート調査");
Label x1 = new Label("性別");
Label x2 = new Label("年齢");
Label x3 = new Label("職業");
Label x4 = new Label("お住まい");
Label x5 = new Label("・デザインが良い");
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("Yes!(^_^)");
Checkbox d0 = new Checkbox("No!(-_-)");
Checkbox c2 = new Checkbox("Yes!(^_^)");
Checkbox d2 = new Checkbox("No!(-_-)");
Checkbox c3 = new Checkbox("Yes!(^_^)");
Checkbox d3 = new Checkbox("No!(-_-)");
List l1 = new List();
TextArea t1 = new TextArea("");
Choice c1 = new Choice();
public repo7_3() {
setLayout(null);
add(b0); b0.setBounds(330, 600, 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, 90, 30);
add(x6); x6.setBounds(10, 260, 90, 30);
add(x7); x7.setBounds(10, 310, 90, 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, 90, 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, 90, 30);
add(l1); l1.setBounds(10, 60, 65, 40);
l1.add("男♂"); l1.add("女♀");
add(t1); t1.setBounds(10, 380, 420, 200);
add(c1); c1.setBounds(10, 165, 120, 30);
c1.add("北海道"); c1.add("東北"); c1.add("関東");
c1.add("東海"); c1.add("関西"); c1.add("中国"); c1.add("四国");
c1.add("九州"); c1.add("沖縄");
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x9.setText("ありがとうございました。");
}
});
}
public static void main(String[] args) {
Frame win = new repo7_3();
win.setSize(450, 640);
win.setVisible(true);
}
}
●実行結果
●考察
アンケート風に作ってみました。
・List
List l1 = new List();
はリストを表示するもので
add(l1); l1.setBounds(10, 60, 65, 40);
l1.add("男♂"); l1.add("女♀");
で表示する位置とリストの内容を決めます。
・Checkbox
Checkbox c0 = new Checkbox("Yes!(^_^)");
Checkbox d0 = new Checkbox("No!(-_-)");
はチェックボックスを表示してチェックを付ける事が出来ます。
・TextArea
TextArea t1 = new TextArea("");
記入欄を表示して、数行の記入が出来ます。
・Choice
Choice c1 = new Choice();
選択肢の中から一つを選ぶ事が出来ます。
add(c1); c1.setBounds(10, 165, 120, 30);
c1.add("北海道"); c1.add("東北"); c1.add("関東");
c1.add("東海"); c1.add("関西"); c1.add("中国"); c1.add("四国");
c1.add("九州"); c1.add("沖縄");
という風にして、位置や大きさ、選択肢を決める
●プログラム
import java.awt.*;
import java.awt.event.*;
public class henkan extends Frame{
Button b0 = new Button("華氏↓");
Button b1 = new Button("摂氏↓");
Label x0 = new Label("変換前");
Label x1 = new Label("変換後");
Label x2 = new Label("数字をいれてね");
TextField t0 = new TextField();
TextField t1 = new TextField();
public henkan(){
setLayout(null);
add(t0); t0.setBounds(90,60,50,20);
add(b0); b0.setBounds(65,85,50,20);
add(x0); x0.setBounds(40,60,50,20);
add(b1); b1.setBounds(115,85,50,20);
add(t1); t1.setBounds(90,110,50,20);
add(x1); x1.setBounds(40,110,50,20);
add(x2); x2.setBounds(60,145,160,20);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int i = (new Integer(t0.getText())).intValue();
i = (int)Math.rint(1.8*i+32);
t1.setText("" + i);
x2.setText("摂氏" + t0.getText() + "は華氏" + i
);
}catch(Exception ex){
t1.setText("error");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int i = (new Integer(t0.getText())).intValue();
i = (int)Math.rint((i-32)/1.8);
t1.setText("" + i);
x2.setText("華氏"+ t0.getText() +"は摂氏"+ i);
}catch(Exception ex){
t1.setText("error");
}
}
});
}
public static void main(String[] args) {
Frame win = new henkan();
win.setSize(230, 200); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
●実行結果
●考察
ボタン華氏↓を押した場合
b0.addActionListener(new ActionListener(){}の部分が実行されて、
入力された数字に1.8かけて32足した数字が下に表示されます。
ボタン摂氏↓を押した場合
b1.addActionListener(new ActionListener(){}の部分が実行されて、
入力された数字から32引いて1.8で割った数字が下に表示されます。
数字以外のものが入力されるなどの例外が起こった場合どちらのボタン
を押してもerrorと表示されます。
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
でウィンドウを閉じた場合にシステムを終了させます。
import java.awt.*;
import java.awt.event.*;
public class keisan extends Frame{
Button b0 = new Button("+"); // ボタン生成
Button b1 = new Button("−");
Button b2 = new Button("×");
Button b3 = new Button("÷");
Button b4 = new Button("1");
Button b5 = new Button("2");
Button b6 = new Button("3");
Button b7 = new Button("4");
Button b8 = new Button("5");
Button b9 = new Button("6");
Button b10= new Button("7");
Button b11= new Button("8");
Button b12= new Button("9");
Button b13= new Button("0");
Button b14= new Button("c");
Button b15= new Button("=");
Label x0 = new Label("演算子を2回連続で押さないでね");
TextField t0 = new TextField(""+0); // テキストフィールド生成
LayoutManager lmg = new GridLayout(4,4);// パネルの行列指定(4行4列)
Panel pnl = new Panel(lmg); // パネル生成
int enzan,total,k,tt;
boolean q = false;
boolean t = false;
boolean x = false;
public keisan(){
setLayout(null);
pnl.add(b10); pnl.add(b11); pnl.add(b12); pnl.add(b0); // ボタンをパネルに配置
pnl.add(b7 ); pnl.add(b8 ); pnl.add(b9 ); pnl.add(b1);
pnl.add(b4 ); pnl.add(b5 ); pnl.add(b6 ); pnl.add(b2);
pnl.add(b13); pnl.add(b14); pnl.add(b15); pnl.add(b3);
add(t0); t0.setBounds(20,35,180,30); // テキストフィールド配置
add(pnl); pnl.setBounds(20,100,180,190); // パネル配置
add(x0); x0.setBounds(5,300,120,20);
b4.addActionListener(new ActionListener() { //ボタン(1)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q==false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+1));}
else{
x = false;
t0.setText(""+ 1);
q = false;}
}catch(Exception ex){
t0.setText(""+ 1);}
}
});
b5.addActionListener(new ActionListener() { //ボタン(2)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+2));}
else{
x = false;
t0.setText(""+ 2);
q = false;}
}catch(Exception ex){
t0.setText(""+ 2);}
}
});
b6.addActionListener(new ActionListener() { //ボタン(3)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+3));}
else{
x = false;
t0.setText(""+ 3);
q=false;}
}catch(Exception ex){
t0.setText(""+ 3);}
}
});
b7.addActionListener(new ActionListener() { //ボタン(4)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+4));}
else{
x = false;
t0.setText(""+ 4);
q = false;}
}catch(Exception ex){
t0.setText(""+ 4);}
}
});
b8.addActionListener(new ActionListener() { ////ボタン(5)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+5));}
else{
x = false;
t0.setText(""+ 5);
q = false;}
}catch(Exception ex){
t0.setText(""+ 5);}
}
});
b9.addActionListener(new ActionListener() { //ボタン(6)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+6));}
else{
x = false;
t0.setText(""+ 6);
q = false;}
}catch(Exception ex){
t0.setText(""+ 6);}
}
});
b10.addActionListener(new ActionListener() { //ボタン(7)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+7));}
else{
x = false;
t0.setText(""+ 7);
q = false;}
}catch(Exception ex){
t0.setText(""+ 7);}
}
});
b11.addActionListener(new ActionListener() { //ボタン(8)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new
Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+8));}
else{
x = false;
t0.setText(""+ 8);
q = false;}
}catch(Exception ex){
t0.setText(""+ 8);}
}
});
b12.addActionListener(new ActionListener() { //ボタン(9)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
try{
if(q == false){
int i = (new
Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+9));}
else{
x = false;
t0.setText(""+ 9);
q = false;}
}catch(Exception ex){
t0.setText(""+ 9);}
}
});
b13.addActionListener(new ActionListener() { //ボタン(0)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
if(q == false){
int i = (new
Integer(t0.getText())).intValue();
i = i*10;
t0.setText("" + (i+0));}
else{
x = false;
t0.setText(""+ 0);
q = false;}
}
});
b14.addActionListener(new ActionListener() { //ボタン(c)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
t0.setText("" + 0);
}
});
b0.addActionListener(new ActionListener() { //ボタン(+)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
int b = (new Integer(t0.getText())).intValue();
if(t == true){
switch (enzan){
case 1:
total = total+b;
break;
case 2:
total = total-b;
break;
case 3:
total = total*b;
break;
case 4:
total = total/b;
break;
}
t0.setText(""+ total);
enzan = 1;
q = true;
}
else{
int a = (new Integer(t0.getText())).intValue();
total = a;
enzan = 1;
q = true;
t = true;
}
}
});
b1.addActionListener(new ActionListener() { //ボタン(-)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
int b = (new Integer(t0.getText())).intValue();
if(t == true){
switch (enzan){
case 1:
total = total+b;
break;
case 2:
total = total-b;
break;
case 3:
total = total*b;
break;
case 4:
total = total/b;
break;
}
t0.setText(""+ total);
enzan = 2;
q = true;
}
else{
int a = (new Integer(t0.getText())).intValue();
total = a;
enzan = 2;
q = true;
t = true;
}
}
});
b2.addActionListener(new ActionListener() { //ボタン(×)を押したとのイベント
public void actionPerformed(ActionEvent evt) {
int b = (new Integer(t0.getText())).intValue();
if(t == true){
switch (enzan){
case 1:
total = total+b;
break;
case 2:
total = total-b;
break;
case 3:
total = total*b;
break;
case 4:
total = total/b;
break;
}
t0.setText(""+ total);
enzan = 3;
q = true;
}
else{
int a = (new Integer(t0.getText())).intValue();
total = a;
enzan = 3;
q = true;
t = true;
}
}
});
b3.addActionListener(new ActionListener() { //ボタン(÷を押したときのイベント
public void actionPerformed(ActionEvent evt) {
int b = (new Integer(t0.getText())).intValue();
if(t == true){
switch (enzan){
case 1:
total = total+b;
break;
case 2:
total = total-b;
break;
case 3:
total = total*b;
break;
case 4:
total = total/b;
break;
}
t0.setText(""+ total);
enzan = 4;
q = true;
}
else{
int a = (new Integer(t0.getText())).intValue();
total = a;
enzan = 4;
q = true;
t = true;
}
}
});
b15.addActionListener(new ActionListener() { //ボタン(=)を押したときのイベント
public void actionPerformed(ActionEvent evt) {
if(x == false){
int b = (new Integer(t0.getText())).intValue();
k = b;
switch (enzan){
case 0:
break;
case 1:
total = total+b;
break;
case 2:
total = total-b;
break;
case 3:
total = total*b;
break;
case 4:
total = total/b;
break;
}
t0.setText(""+ total);
tt =total;
total = 0;
q = true;
t = false;
x = true;
}else{
switch (enzan){
case 1:
tt = tt+k;
break;
case 2:
tt = tt-k;
break;
case 3:
tt = tt*k;
break;
case 4:
tt = tt/k;
break;
}
t0.setText(""+ tt);
}
}
});
}
public static void main(String[] args) {
Frame win = new keisan();
win.setSize(220, 320); win.setVisible(true);
win.setTitle("電卓");
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
●実行結果
●考察
数字や演算子の配置はPanelを使ってます。これは行と列の数を指定するとそこに
並べてくれます。わざわざボタンをひとつずつ配置しないでいいので便利です。
qのtrue/falseを使って演算子を押した後の数字の入力なのか、前の入力なのか
判断しています。
trueなら演算子を押した後なので、表示されている数字を消して新しく数字をいれます。
falseなら表示されている数字を10倍して数字を足します。
tのtrue/falseを使って最初に押された演算子なのか(例:1+3の+)二回目以
降に押された演算子(例:6+1−2 のー)判断します。
trueなら2回目以降に押されているのでその前の演算結果を表示します。
例えば、6+1−2をした場合、ーを押した時点で6+1の計算結果7を
表示します。
xのtrue/falseを使って最初に押された=なのか2回目以降に押された=なのか判断します。
xがtrueなら2回目以降に押されているので一回目に行われた演算をさらに繰り返します。
例えば1+2の後に=を押すと3が表示されます。ここで再び=を押すと
さらに+2されて5が表示されます。
変数enzanを使うことにより=を押した時に、どの演算をするかを判断しています。
演算子のボタンを押した時にenzanに数字を入れて、=を押した時にswitch文で
どの演算を行えばいいか判断します。
例えば、+を押すとenzanに1が入ります
=を押した時にenzanには1が入っているので、switch文のcase 1:の加算部分を行います
俺の電卓の性能
良いとこ
・cを押す事で表示されている数字を消せる。
・=連打で演算を繰り返す事が出来る。
・演算を組み合わせて計算出来る。例1+2-3+4みたいな
悪いとこ
・オバーフローしても知らんぷり
・演算子のボタンを二回以上続けて押すとバグリます!
反省・感想
とことん頑張りました。
特に電卓は自分のすべてをつぎ込みました。
無駄な部分が多いですが試行錯誤してやっと出来たので大満足です
ゲームを作る気力はありませんがReport8も頑張ります。
・参考文献・URL
独習Java 第3版