☆課題☆
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 or Odd?");
Label x0 = new Label("Type an integral number");
TextField t0 = new TextField();
public GUIaa() {
setLayout(null);
add(b0); b0.setBounds(120, 40, 110, 20);
add(x0); x0.setBounds(10, 80, 180, 20);
add(t0); t0.setBounds(10, 40, 100, 20);
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(240, 120);
win.setVisible(true);
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
}
1.実行結果
☟数字を入力
☟ボタンを押す
1.プログラムの考察
awtパッケージのインポート。
そこからさらにイベント処理のパッケージをインポートしている。
Frameクラスを継承したGUIaaクラスというものの中にButtonとLabelとTextFieldを作っている
(Frameクラスではメニューバーを提供している)
Button、Label、TextFieldの大きさの指定。(x座標、y座標、幅、高さ)左上隅が原点。
Buttonを押したときのイベント処理。
iをTextFieldから読み込んでもしiを2で割った余りが0ならLabelにiの数字と"is Even"を出力。
その他なら"is Odd"になる。
main関数。
GUIaaで使うフレームの設定。
setSizeでウインドウの大きさ。
setVisibleでディスプレイの表示をするか。
最後にウインドウを閉じたときに、プログラムを終了するようにしている。
2.例外処理
発生しそうな例外の種類に合わせた処理をすることで、バグ、処理をしないなどの問題を起こさせないようにするもの。
try{}で例外が発生しそうな場所を囲い、この範囲で例外が発生するとcatch(){}の()で例外の内容別に種類分けして{}で処理する。
そしてそれらが終了するとfinally{}が実行され最終処理をする。
☟偶数奇数プログラムに例外処理を加えると
☟実行結果
☟ボタンを押す
3.GUI部品を色々使ったプログラム
import java.awt.*;
import java.awt.event.*;
public class GUI extends Frame{
Button b0 = new Button("判定");
TextField t0 = new TextField();
List li = new List();
Choice ch0 = new Choice();
Choice ch1 = new Choice();
Label l0 = new Label("あなたの気分は何色ですか?");
Label l1 = new Label("体調はどうですか?");
Label l2 = new Label("貯金はいくらありますか?");
Checkbox c0 = new Checkbox("なぞのcheck欄");
TextArea ta0 = new TextArea("TextArea");
public GUI(){
setLayout(null);
add(l0);l0.setBounds(10,60,200,30);
add(ch0);ch0.setBounds(205,60,100,30);
add(c0); c0.setBounds(10,200,150,30);
add(ta0);ta0.setBounds(150,200,250,100);
ch0.add("RED");ch0.add("BLUE");ch0.add("YELLOW");
ch0.add("GREEN");ch0.add("PURPLE");ch0.add("BLACK");
add(l1);l1.setBounds(10,90,150,30);
add(l2);l2.setBounds(10,30,160,30);
add(ch1);ch1.setBounds(205,90,100,30);
ch1.add("絶好調");ch1.add("好調");ch1.add("普通");ch1.add("不調");
ch1.add("絶不調");ch1.add("その他");
add(t0);t0.setBounds(205,30,100,20);
add(b0);b0.setBounds(210,120,100,30);
b0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
ta0.setText("ps3を買いましょう。\nそして島原君にあげましょう");
}
});
}
public static void main(String[] args) {
Frame win = new GUI();
win.setSize(400,300);
win.setTitle("行動判定プログラム");
win.setVisible(true);
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
}
3.実行結果
☟質問に従って入力
☟判定ボタンを押す
3.考察
上述のサンプルプログラムに出てくるGUI部品とは下記の7つ。
- Button これを押してイベント処理を行った。
- Label 文章を貼付けることができる。
- TextField 1行の文章を入力(出力)するための領域を確保する。
- Checkbox check欄を作る事ができる。
- List 選択事項を選ぶ際に使う。スクロール機能付き。
- TextArea 複数行の文章を入力(出力)するための領域を確保する。
- Choice 選択事項を選ぶ際に使う。スクロール機能なし。
4.摂氏華氏変換プログラム
import java.awt.*;
import java.awt.event.*;
public class sekka extends Frame{
Button b0 = new Button("摂氏 → 華氏");
Button b1 = new Button("華氏 → 摂氏");
TextField t0 = new TextField();
TextField t1 = new TextField();
Label l0 = new Label("↓");
Label l1 = new Label("↓");
public sekka(){
setLayout(null);
add(t0);t0.setBounds(50,30,100,20);
add(t1);t1.setBounds(50,130,100,20);
add(b0);b0.setBounds(0,80,100,20);
add(b1);b1.setBounds(100,80,100,20);
add(l0);l0.setBounds(93,60,100,20);
add(l1);l1.setBounds(93,110,100,20);
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 sekka();
win.setSize(200, 180);
win.setVisible(true);
win.setTitle("変換機");
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});}
}
4.実行結果
☟数字を入力
☟ボタンを押す
☟数字以外が入力されたとき
4.1のプログラムだけでは考察しきれていない場所の考察
摂氏から華氏の間にはF(華氏)=1.8*+C(摂氏)+32という関係がある。
よってこうやって計算の部分をi*1.8+32とすればよい。
Button(b0)を押すと数字が摂氏から華氏に計算される。
華氏から摂氏は逆から計算する。
この部分は例外が出るかもしれないので両方に例外処理を入れておく。
新しくsetTitleでTitleをつけた。
5.電卓プログラム
import java.awt.*;
import java.awt.event.*;
public class calculator extends Frame {
Button b0 = new Button("+");
Button b1 = new Button("-");
Button b2 = new Button("*");
Button b3 = new Button("/");
Button b4 = new Button("%");
Label l0 = new Label("計算結果欄");
Label l1 = new Label("Type two integral numbers");
TextField t0 = new TextField();
TextField t1 = new TextField();
TextField t2 = new TextField();
public calculator() {
setLayout(null);
add(t0); t0.setBounds(90, 70, 100, 25);
add(t1); t1.setBounds(90, 200, 100, 25);
add(t2); t2.setBounds(45, 245, 210, 30);
add(b0); b0.setBounds(75, 100, 60, 30);
add(b1); b1.setBounds(150, 100, 60, 30);
add(b2); b2.setBounds(75, 135, 60, 30);
add(b3); b3.setBounds(150, 135, 60, 30);
add(b4); b4.setBounds(90, 170, 100, 25);
add(l0); l0.setBounds(105, 225, 100, 50);
add(l1); l1.setBounds(50, 40, 200, 30);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
int i = (new Integer(t0.getText())).intValue();
int j = (new Integer(t1.getText())).intValue();
l0.setText(" 計算結果");
t2.setText("" + (i+j) + "");
}catch(NumberFormatException ne) {
l0.setText(" error");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
int i = (new Integer(t0.getText())).intValue();
int j = (new Integer(t1.getText())).intValue();
l0.setText(" 計算結果");
t2.setText("" + (i-j) + "");
}catch(Exception ne) {
l0.setText(" error");
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
int i = (new Integer(t0.getText())).intValue();
int j = (new Integer(t1.getText())).intValue();
l0.setText(" 計算結果");
t2.setText("" + i*j + "");
}catch(Exception ne){
l0.setText(" error");
}
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
int i = (new Integer(t0.getText())).intValue();
int j = (new Integer(t1.getText())).intValue();
l0.setText(" 計算結果");
t2.setText("" + i/j + "");
}catch(Exception ne) {
l0.setText(" error");
}
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
int i = (new Integer(t0.getText())).intValue();
int j = (new Integer(t1.getText())).intValue();
l0.setText(" 計算結果");
t2.setText("" + (i%j) + "");
}catch(Exception ne) {
l0.setText(" error");
}
}
});
}
public static void main(String[] args) {
Frame win = new calculator();
win.setSize(280, 280);
win.setTitle("calculator");
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
5.実行結果
☟数字を入力
☟+ボタンを押す
☟-ボタンを押す
☟*ボタンを押す
☟/ボタンを押す
☟%ボタンを押す
☟片方の数字を入力しないでボタンを押す
☟半角英数を入力してボタンを押す
5.電卓プログラムの考察
4則演算に余り演算を加えた簡易電卓プログラム。
量が増えただけのプログラムなので1〜4の考察で全て賄える内容。
感想
今回から画像を使ってみた。
使うと考察もしやすいし、なにより見やすい。なぜいままでこの便利さ?に気付かなかったんだろう。
だから実行結果の部分の画像加工は楽しくてしょうがなかった。
過去の自分に気付いて欲しかった。この楽しさに。