課題7
- 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
- 例外処理について、考察せよ。
- 上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
- 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。。
- 「電卓」プログラム。中身は自分の思うように。
http://www.pc-view.net/Help/ 参考文献
偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
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);
}
});
}
}
考察です
import java.awt.*;
import java.awt.event.*;
import java.awt.*;はAbstract Window Toolkitの略です
アプレットとアプリケーションのGUIを作成する事ができるパッケージです
import java.awt.event.*;はアクションリスナーやウィンドウリスナー等の
イベントを処理するときに使います。
Button b0 = new Button("Even/Odd?");
Label x0 = new Label("Type a number and press...");
TextField t0 = new TextField();
GUIのコンポーネントを使ってオブジェクトを作ります。
ButtonやLabelやTextFieldがそれにあたります。
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);
setLayoutはコンポーネントを配置する事ができます。
ここではコンポーネントの位置を自分で指定しているため、
setLayoutにnullを送ります。
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");
}
}
});
# b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
はボタンを押した後の処理を行います。
int i = (new Integer(t0.getText())).intValue();
はTextField(一行の文字列を入力する事ができる)から
入力された文字列をint 型の i に代入します
さらにiが2で割り切れるときはis Even、そうでないときはis Oddと表示されます
public static void main(String[] args) {
Frame win = new GUIaa();
win.setSize(200, 150); win.setVisible(true);
メイン関数です。GUIaaをフレームのオブジェクト化します。
フレームはウィンドウに関するメソッドを持っています。
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
ウィンドウが閉じられたとき、アプリケーションを終了します
例外処理について、考察せよ。
import java.awt.*;
import java.awt.event.*;
public class ColorRGBa extends Frame {
Button b0 = new Button("Display");
Label[] la = new Label[]{new Label ("Red"),
new Label ("Green"),
new Label ("Blue") };
TextField[] ta = new TextField[]{new TextField(),
new TextField(),
new TextField()};
Label x0 = new Label("Input RGB values [0..255]");
public ColorRGBa() {
setLayout(null);
for(int i = 0; i < la.length; i++) {
add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);
add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);
}
add(b0); b0.setBounds(10, 160, 60, 30);
add(x0); x0.setBounds(10, 200, 180, 30);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
x0.setBackground(new Color(
(new Integer(ta[0].getText())).intValue(),
(new Integer(ta[1].getText())).intValue(),
(new Integer(ta[2].getText())).intValue()));
} catch(Exception ex) { x0.setText(ex.toString()); }
}
});
}
public static void main(String[] args) {
Frame win = new ColorRGBa();
win.setSize(200, 250); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
Label[] la = new Label[]{new Label ("Red"),
new Label ("Green"),
new Label ("Blue") };
TextField[] ta = new TextField[]{new TextField(),
new TextField(),
new TextField()};
Labelのオブジェクトの配列を3つ作り、TextFieldも同様に作っています。
try {
x0.setBackground(new Color(
(new Integer(ta[0].getText())).intValue(),
(new Integer(ta[1].getText())).intValue(),
(new Integer(ta[2].getText())).intValue()));
} catch(Exception ex) { x0.setText(ex.toString()); }
try以下の中括弧に処理を書いて、その途中で例外がおこったときに
catch以下の中括弧の処理が行われます。
例外の内容はcatch()のカッコ内で指定します。この場合はException exとなります。
例外とは
例外とはプログラムの実行中に発生した問題を通知するために、
実行時に生成されるオブジェクトの事です。例外が発生する典型的な
ケースは、整数を0で除算した、ファイルが見つからなかった、数値の形式が
不正だった、等の場合です。
try{
処理内容
}
catch(例外1){
例外1が起きたときの処理
}
catch(例外2){
例外2が起きたときの処理
}
finally{
処理内容
}
tryで処理をしているときに問題が発生した場合、上から順にcatchブロックが
検索されます。起きた問題と例外が合致するとcatch処理が行われます。
finallyは必ず最後に行われる処理です。try処理やcatch処理が終わると行われます。
上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ
import java.awt.*;
import java.awt.event.*;
public class Matome extends Frame{
//コンポーネントの生成
Choice co = new Choice();
Checkbox[] ce = new Checkbox[]{new Checkbox("man"),
new Checkbox("woman") };
Button b0 = new Button("表示");
Label[] x0 = new Label[]{new Label("あなたの性別"),
new Label("あなたの好きな色は?"),
new Label("学年")};
TextArea ta = new TextArea();
List li = new List();
//コンストラクタの設定
public Matome() {
setLayout(null);
//コンポーネントの絶対位置を指定する
add(b0); b0.setBounds(10, 290, 90, 30);
add(x0[0]); x0[0].setBounds(10, 30, 80, 30);
add(x0[1]); x0[1].setBounds(10, 100, 90, 30);
add(x0[2]); x0[2].setBounds(10, 220, 60, 30);
add(ce[0]); ce[0].setBounds(10, 50, 80, 30);
add(ce[1]); ce[1].setBounds(70, 50, 80, 30);
add(co); co.setBounds(10, 240, 20, 30);
co.add("1"); co.add("2"); co.add("3"); co.add("4");
add(li); li.setBounds(10, 120, 120, 100);
li.add("緑"); li.add("オレンジ"); li.add("青");
li.add("白"); li.add("赤");
add(ta); ta.setBounds(220, 30, 200, 200);
//ボタンが押されたときの処理
b0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
//テキストエリアに表示する文
if(ce[0].getState())
ta.append("性別は "+ ce[0].getLabel() + "?n");
if(ce[1].getState())
ta.append("性別は "+ ce[1].getLabel() + "?n");
ta.append("好きな色は "+li.getSelectedItem()+"?n");
ta.append("学年は "+co.getSelectedItem()+"?n");
}
});
}
public static void main(String[] args) {
Frame win = new Matome();
win.setSize(500,400); win.setVisible(true);
//ウィンドウが閉じられたときの設定
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
解説
-
import java.awt.*;
import java.awt.event.*;
インポートしてる部分です
Choice co = new Choice();
Checkbox[] ce = new Checkbox[]{new Checkbox("man"),
new Checkbox("woman") };
Button b0 = new Button("表示");
Label[] x0 = new Label[]{new Label("あなたの性別"),
new Label("あなたの好きな色は?"),
new Label("学年")};
TextArea ta = new TextArea();
List li = new List();
チョイス、チェックボックス、ボタンなどのコンポーネントを生成します.
public Matome() {
setLayout(null);
コンストラクタの設定をします。レイアウトは自分で指定するのでnull.
add(b0); b0.setBounds(10, 290, 90, 30);
add(x0[0]); x0[0].setBounds(10, 30, 80, 30);
add(x0[1]); x0[1].setBounds(10, 100, 90, 30);
add(x0[2]); x0[2].setBounds(10, 220, 60, 30);
add(ce[0]); ce[0].setBounds(10, 50, 80, 30);
add(ce[1]); ce[1].setBounds(70, 50, 80, 30);
add(co); co.setBounds(10, 240, 20, 30);
co.add("1"); co.add("2"); co.add("3"); co.add("4");
add(li); li.setBounds(10, 120, 120, 100);
li.add("緑"); li.add("オレンジ"); li.add("青");
li.add("白"); li.add("赤");
add(ta); ta.setBounds(220, 30, 200, 200);
コンポーネントを実装しています。コンポーネントの絶対位置も決めます。
リストとチョイスの要素もここで決めます
if(ce[0].getState())
ta.append("性別は "+ ce[0].getLabel() + "n");
if(ce[1].getState())
ta.append("性別は "+ ce[1].getLabel() + "n");
ta.append("好きな色は "+li.getSelectedItem()+"n");
ta.append("学年は "+co.getSelectedItem()+"n");
ボタンが押されたときの処理です。
テキストエリアは入力するときや出力する時に使えます
出力結果
摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。。
import java.awt.*;
import java.awt.event.*;
public class Henka extends Frame{
Label[] l0 = new Label[]{new Label("入力フィールド"),
new Label("出力フィールド")};
Choice co = new Choice();
TextField[] tx = new TextField[]{new TextField(""),
new TextField("")};
Button bt = new Button("変換");
public Henka() {
setLayout(null);
add(l0[0]); l0[0].setBounds(240, 40,40,40);
add(l0[1]); l0[1].setBounds(240, 140,40,40);
add(co); co.setBounds(110, 70, 120, 30);
co.add("摂氏→華氏"); co.add("華氏→摂氏");
add(tx[0]); tx[0].setBounds(30, 40, 200, 10);
add(tx[1]); tx[1].setBounds(30, 140, 200, 10);
add(bt); bt.setBounds(30, 70, 60, 30);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(co.getSelectedIndex()==1){//華氏→摂氏
//tx[0]を整数型に変換
String str = tx[0].getText();
double tx0 = Double.parseDouble(str);
//華氏を摂氏に直す
int tx0kai = (int) ((tx0 - 32) / 1.8);
tx[1].setText("摂氏は "+tx0kai+" です");
}
else{//摂氏→華氏
//tx[0]を整数型に変換
String str = tx[0].getText();
double tx0 = Double.parseDouble(str);
//摂氏を華氏に直す
int tx0kai = (int)(tx0 * 1.8 + 32);
tx[1].setText("華氏は "+tx0kai+" です");
}
}
});
}
public static void main(String[] args) {
Frame win = new Henka();
win.setSize(400,400); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
解説
Label[] l0 = new Label[]{new Label("入力フィールド"),
new Label("出力フィールド")};
Choice co = new Choice();
TextField[] tx = new TextField[]{new TextField(""),
new TextField("")};
Button bt = new Button("変換");
ラベルを2つとチョイスと入力用のテキストフィールド、そして
出力用のテキストフィールドを生成します
if(co.getSelectedIndex()==1){//華氏→摂氏
//tx[0]を整数型に変換
String str = tx[0].getText();
double tx0 = Double.parseDouble(str);
//華氏を摂氏に直す
int tx0kai = (int) ((tx0 - 32) / 1.8);
tx[1].setText("摂氏は "+tx0kai+" です");
ボタンが押された後の処理についてです。
チョイスで「華氏→摂氏」が選択されたときにボタンが押されると、
この処理に移ります。入力された値を
ダブル型で計算した後にint型に代入しました。
else{//摂氏→華氏
//tx[0]を整数型に変換
String str = tx[0].getText();
double tx0 = Double.parseDouble(str);
//摂氏を華氏に直す
int tx0kai = (int)(tx0 * 1.8 + 32);
tx[1].setText("華氏は "+tx0kai+" です");
チョイスの要素は2つなので、摂氏→華氏の処理はここに移ります。
ここもダブル型からイント型に代入しました。
実行画面
「電卓」プログラム。中身は自分の思うように。
import java.awt.*;
import java.awt.event.*;
public class Dentaku extends Frame implements ActionListener{
static int kazu1 = 0;
static int kazu2 = 0;
static int hugou = 0;
Button[] b0 = new Button[]{
new Button("0"),new Button("1"),new Button("2"),
new Button("3"),new Button("4"),new Button("5"),
new Button("6"),new Button("7"),new Button("8"),
new Button("9")
};
Button[] b1 = new Button[]{
new Button("+"),new Button("-"),new Button("*"),
new Button("/"),new Button("C"),new Button("=")
};
TextField tf = new TextField("");
public Dentaku() {
setLayout(null);
for(int i=0; i<10;i++) add(b0[i]);
for(int i=0; i<6;i++) add(b1[i]);
add(tf);
int wi = 40;//幅
int hi = 50;//高さ
int ki = 250;
tf.setBounds(0,hi,wi*5,hi);
b0[0].setBounds(0,ki,wi,hi);
b0[1].setBounds(0,ki-hi,wi,hi);
b0[2].setBounds(wi,ki-hi,wi,hi);
b0[3].setBounds(wi+wi,ki-hi,wi,hi);
b0[4].setBounds(0,ki-hi*2,wi,hi);
b0[5].setBounds(wi,ki-hi*2,wi,hi);
b0[6].setBounds(wi+wi,ki-hi*2,wi,hi);
b0[7].setBounds(0,ki-hi*3,wi,hi);
b0[8].setBounds(wi,ki-hi*3,wi,hi);
b0[9].setBounds(wi+wi,ki-hi*3,wi,hi);
b1[0].setBounds(wi+wi+wi,ki,wi,hi);
b1[1].setBounds(wi+wi+wi,ki-hi,wi,hi);
b1[2].setBounds(wi+wi+wi,ki-hi*2,wi,hi);
b1[3].setBounds(wi+wi+wi,ki-hi*3,wi,hi);
b1[4].setBounds(wi,ki,wi,hi);
b1[5].setBounds(wi+wi+wi+wi,ki-hi*3,wi,hi*4);
for(int i=0;i<10;i++)
b0[i].addActionListener(this);
for(int i=0;i<6;i++)
b1[i].addActionListener(this);
}
public void actionPerformed (ActionEvent e) {
try{
//数字が押されたときの処理
String num1 = e.getActionCommand();
kazu1 = kazu1 * 10 + Integer.parseInt(num1);
//入力された数字を表示
tf.setText(""+kazu1);
}
catch(NumberFormatException nu){
if (e.getSource() == b1[0]){
if(kazu1 != 0){
keisan();
hugou = 1;//hugou に+を代入
}else{
kazu2 = kazu1;
kazu1 = 0;
}
}
else if (e.getSource() == b1[1]){
if(kazu1 != 0) {
keisan();
hugou = 2;//hugouに-を代入
}else{
kazu2 = kazu1;
kazu1 = 0;
}
}
else if (e.getSource() == b1[2]){
if(kazu1 != 0){
keisan();
hugou = 3;//hugouに*を代入
}else{
kazu2 = kazu1;
kazu1 = 0;
}
}
else if (e.getSource() == b1[3]){
if(kazu1 != 0){
keisan();
hugou = 4;//hugouに/を代入
}else{
kazu2 = kazu1;
kazu1 = 0;
}
}
else if (e.getSource() == b1[4]){
kazu1 = 0; kazu2 = 0; tf.setText(""+0);
}
else if (e.getSource() == b1[5]){
keisan();
}
tf.setText(""+kazu2);
}
}
public void keisan(){
switch(hugou) {
case 0:
break;
case 1:
kazu2 = kazu2 + kazu1;
kazu1 = 0;
break;
case 2:
kazu2 = kazu2 - kazu1;
kazu1 = 0;
break;
case 3:
kazu2 = kazu2 * kazu1;
kazu1 = 0;
break;
case 4:
kazu2 = kazu2 / kazu1;
kazu1 = 0;
break;
}
}
public static void main(String[] args) {
Frame win = new Dentaku();
win.setSize(300,300); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
説明
public void actionPerformed (ActionEvent e) {
try{
//数字が押されたときの処理
String num1 = e.getActionCommand();
kazu1 = kazu1 * 10 + Integer.parseInt(num1);
//入力された数字を表示
tf.setText(""+kazu1);
}
catch(NumberFormatException nu){
ボタンが押されたときの処理です。
とりあえずボタンから帰ってきた文字列を数字に変換してみます。
無事変換できたら数字として扱い、エラーがでたら符号として利用します
全体のプログラムとしては
変数kazu1とkazu2とhugouが重要でした
kazu1は直前の数を保存します、kazu2は今までの数の蓄積です、
hugouは直前の符号を保存します。
kazu1は入力された数字が直接保存されます。
kazu2はkazu1が上書きされないようにkazu1を蓄積します。hugouはそのときの
計算で使いました。
実行画面
感想:相変わらず大変だった(汗)