レポート#6
(12/)(Sun) 035717B 金城満久
- 課題
-
- 偶数奇数判定プログラム(GUIaa)をタイプし,その動作を考察せよ.
- 例外処理について,考察せよ.
- 上述のサンプルプログラムに出てきたGUI部品を,全て使ったプログラムを作成せよ.
- 摂氏から華氏,華氏から摂氏への温度換算ができるプログラムを作成せよ.
- 「電卓」プログラム.中身は自分の思うように.
- 実行プログラム&実行結果
-
(GUI部品を全て使ったプログラム)
[Mitsuhisa-KINJO:~/GUI] j03017% cat myGUI.java
import java.awt.*;
import java.awt.event.*;
public class myGUI extends Frame {
Button b0 = new Button("Cancel COLOR");
Label x0 = new Label("Let's enjoy!!");
TextField t0 = new TextField();
Checkbox c0 = new Checkbox("What's your sex?",true);
CheckboxGroup cc = new CheckboxGroup();
Checkbox ca = new Checkbox("male",cc,true);
Checkbox cb = new Checkbox("female",cc,false);
List l1 = new List();
TextArea t1 = new TextArea("f");
Choice c1 = new Choice();
public myGUI() {
setLayout(null);
add(b0); b0.setBounds(120,170,140,30);
add(x0); x0.setBounds(10,30,60,30);
add(t0); t0.setBounds(130,30,80,30);
add(c0); c0.setBounds(10,210,150,30);
add(ca); ca.setBounds(10,240,60,30);
add(cb); cb.setBounds(80,240,65,30);
add(l1); l1.setBounds(10,70,110,80);
add(t1); t1.setBounds(130,70,120,90);
add(c1); c1.setBounds(10,170,50,30);
l1.add("FRESHMAN");
l1.add("SOPHOMORE");
l1.add("JUNIOR");
l1.add("SENIOR");
c1.add("RED");
c1.add("BLUE");
c1.add("GREEN");
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Color C0 = new Color(255,255,255);
setBackground(C0);
}
});
c1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
if(c1.getSelectedItem().equals("RED")) {
Color C1 = new Color(255,0,0);
setBackground(C1);}
else if(c1.getSelectedItem().equals("BLUE")) {
Color C2 = new Color(0,0,255);
setBackground(C2);}
else {
Color C3 = new Color(0,255,0);
setBackground(C3);}
}
});
}
public static void main(String[] args) {
Frame win = new myGUI();
win.setSize(300,300);
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
(実行結果1)
(実行結果2) (実行結果3)
(摂氏・華氏変換プログラム)
[Mitsuhisa-KINJO:~/GUI] j03017% cat sessikasi.java
import java.awt.*;
import java.awt.event.*;
public class sessikasi extends Frame {
Button b0 = new Button("Fahrenheit");
Button b1 = new Button("Centigrade");
Label x0 = new Label("Type a number");
TextField t0 = new TextField();
public sessikasi() {
setLayout(null);
add(x0); x0.setBounds(10,40,100,30);
add(t0); t0.setBounds(120,40,90,30);
add(b0); b0.setBounds(10,80,100,30);
add(b1); b1.setBounds(120,80,100,30);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = (new Integer(t0.getText())).intValue();
t0.setText("");
int sum = i * 9 / 5 + 32;
x0.setText( sum + " F ");
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = (new Integer(t0.getText())).intValue();
t0.setText("");
int sum = (i - 32) * 5 / 9;
x0.setText( sum + " 'C ");
}
});
}
public static void main(String[] args) {
Frame win = new sessikasi();
win.setSize(250,250); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
(実行結果1)
(実行結果2) (実行結果3)
(電卓プログラム)
[Mitsuhisa-KINJO:~/GUI] j03017% cat dentaku.java
import java.awt.*;
import java.awt.event.*;
public class dentaku extends Frame {
Button ba = new Button("+");
Button bb = new Button("-");
Button bc = new Button("x");
Button bd = new Button("/");
Button be = new Button("%");
Button bf = new Button("=");
Button bg = new Button("AC");
Button b0 = new Button("0");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Label x0 = new Label("KEISAN");
Label x1 = new Label("SHITE!");
Label xx = new Label("");
TextField t0 = new TextField("0");
public dentaku() {
setLayout(null);
add(x0); x0.setBounds(10,30,80,30);
add(x1); x1.setBounds(90,30,80,30);
add(t0); t0.setBounds(180,30,100,30);
add(ba); ba.setBounds(10,70,40,30);
add(bb); bb.setBounds(50,70,40,30);
add(bc); bc.setBounds(90,70,40,30);
add(bd); bd.setBounds(130,70,40,30);
add(be); be.setBounds(170,70,40,30);
add(bf); bf.setBounds(100,230,120,30);
add(bg); bg.setBounds(220,110,80,150);
add(b0); b0.setBounds(40,230,60,30);
add(b1); b1.setBounds(40,190,60,30);
add(b2); b2.setBounds(100,190,60,30);
add(b3); b3.setBounds(160,190,60,30);
add(b4); b4.setBounds(40,150,60,30);
add(b5); b5.setBounds(100,150,60,30);
add(b6); b6.setBounds(160,150,60,30);
add(b7); b7.setBounds(40,110,60,30);
add(b8); b8.setBounds(100,110,60,30);
add(b9); b9.setBounds(160,110,60,30);
ba.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText(" + ");
x1.setText(t0.getText());
t0.setText("0");
xx.setText("a");
}
});
bb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText(" - ");
x1.setText(t0.getText());
t0.setText("0");
xx.setText("b");
}
});
bc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText(" x ");
x1.setText(t0.getText());
t0.setText("0");
xx.setText("c");
}
});
bd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText(" / ");
x1.setText(t0.getText());
t0.setText("0");
xx.setText("d");
}
});
be.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText(" % ");
x1.setText(t0.getText());
t0.setText("0");
xx.setText("e");
}
});
bf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(x1.getText());
int j = Integer.parseInt(t0.getText());
String S = xx.getText();
if(S == "a"){
int sum = i + j;
x0.setText("add:");
String s = Integer.toString(sum);
x1.setText(s);
}
else if(S == "b"){
int sum = i - j;
x0.setText("substract:");
String s = Integer.toString(sum);
x1.setText(s);
}
else if(S == "c"){
int sum = i * j;
x0.setText("multiply:");
String s = Integer.toString(sum);
x1.setText(s);
}
else if(S == "d"){
int sum = i / j;
x0.setText("divide:");
String s = Integer.toString(sum);
x1.setText(s);
}
else if(S == "e"){
int sum = i % j;
x0.setText("surplus:");
String s = Integer.toString(sum);
x1.setText(s);
}
else {
String s = Integer.toString(i);
t0.setText(s);
}
}
});
bg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
t0.setText("0");
x0.setText("KEISAN");
x1.setText("SHITE!");
}
});
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+1;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+2;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+3;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+4;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+5;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+6;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+7;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+8;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10+9;
String s = Integer.toString(sum);
t0.setText(s);
}
});
}
public static void main(String[] args) {
Frame win = new dentaku();
win.setSize(330,300);
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
(実行結果1)
(実行結果2) (実行結果3)
- 考察
-
-
この奇数偶数判定プログラムでは,TextFieldに数字を入力し,ボタンを押
すと,ボタンに組み込んでいたアクションリスナーがそのイベントが発生した
ことを受け取り,actionPerformed()内で次の処理を行います.
まず,t0.getText()でTextFieldの数字を返しますが,それはString値なの
で,intValueでint値として得たのをiに代入します.そして,TextFieldには
最初のなにも数字のない状態に設定します.最後にif〜else文で奇数か偶数か
を判断してLabelに設定します.
最後に,ウィンドウを閉じるというイベントが発生したときは,System.exi
t(0);を呼び出してこのプログラムを終了します.
-
この三原色ColorRGBaプログラムでは,TextFieldに0から255までの数字を
いれてボタンを押すと,"Input RGB Value [0..255]"と設定されているLabel
の背景が設定されたRGB値の色に設定されます.もし,TextFieldになにも入力
しない,または数字以外を入力した場合,エラーメッセージを"Input 〜"と設
定されたLabelに表示されます.
プログラムの中身では,Label[],TextField[]の配置を設定するときに,for
文でi
このGUI部品を使ったプログラムで,チェックボックス1つとラジオボタン
というものを作ってみました.まず,CheckboxGroupインスタンスを作成しま
す.このCheckboxGroupは,Checkboxにラジオボタンの性格を与え,複数のコ
ンポーネントをグループとして扱うための機能を提供します.そして,Check
boxインスタンスを作成する際に,表示テキスト,CheckboxGroupインスタンス
,選択状態の3つの引数に指定します.これで,このCheckboxは指定のCheckb
oxGroupによってラジオボタンのグループとして管理されるようになります.
このプログラムの一番の目玉は,(というよりこれだけなんですが...)Ch
oiceで色を設定すると背景が変わるというものです.このプログラムの中身で
は,「アイテムリスナー」というものを使いました.これは,コンポーネント
の状態が変更された際に発生するもので,アイテムイベントの処理は,ItemLi
stenerというものを使って行います.イベント処理で,RED,Blueに変換した場
合をif〜else文で分けていますが,ここで,[c1.getSelectedItem().equals("
〜")]としていますが,int値の場合,[a==b]としますが,String値の場合,[a.
equals(b)]つまりaとbが等しい,というふうにします.色を設定するとき,[C
olor(赤,緑,青)]とする以外で,[Color.red]というようなやりかたもあります.
このやりかたでは,用意されている色は(black,blue,cyan,darkGray,gray,gree
n,lightGray,magenta,orange,pink,red,White,yellow)です.また,ボタンで
設定した色を取り消すというようにしましたが,実際は色を白にかえただけで
す.
TextFieldとTextAreaの違いはみてわかるのですが,TextFieldの場合は1行
のテキストを入力するもので,長いテキストを編集したりする場合にTextAr
eaを使います.
-
最初,華氏を絶対温度のケルビンのやつと思っていたので,ちょっとまえに
友達に教えられてわかりましたが,物理でならっていません.ネットで調べた
公式は,[摂氏(C)=(華氏(F)−32)*5/9]というものでしたが,正直始めてみま
した.プログラムの中身は,TextFieldに数字を入力しボタンを押したらアク
ションリスナーより,"Centigrade"ボタンなら上の公式をつかって計算した値
をLabelに表示し,TextFieldを何もない状態に戻し,"Fahrenheit"ボタンの場
合,逆の公式で計算したい値をLabelに表示し,TextFieldを何もない状態にし
ます.
-
電卓プログラムは,加算,差算,乗算,除算,剰余の計算ができるプログラ
ムを作りました.
0から9までのボタンでは,TextFieldに入力された数字を返し,それに10
をかけて,それにそれぞれのボタンの数字を足してTextFieldに表示します.
(+,-,x,/,%)のボタンでは,押すと,そのボタンの記号がLabel1に,TextFie
ldの数字がText2に表示されます.ここで,[Label xx]としたものは,一応,
目印という形で作りました.そして,"="ボタンを押すと,目印によりどのボ
タンを押したか判断して,Label2,TextFieldの数字を計算します.
元に戻すときには,"AC"ボタンを押せば始めの状態になります.
-
- 反省・感想
-
今まで普通に使っていたでんたくが,今回の電卓のプログラムを作るうえ
で,その中身の仕組みが少しわかったような感じがして,すごいものなんだ
と実感してしまいました.いろいろなソフトやネットでみかけるcheckboxや
ボタンなどの仕組みも理解できてかなり大変だったけれど,おもしろかった
です.
- 参考文献
-
Java&Cocoaプログラミングバイブル:掌田津耶乃 著
<ホーム>