☆講義資料「GUIプログラム」をもとに、以下の課題を行え。 1. 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。 2. 例外処理について考察せよ。 3. 上述のサンプルプログラムに出てきたGUI部品を全て使ったプログラムを作成せよ。 4. 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。 5. 「電卓」プログラム。中身は自分の思うように。
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とは a. Graphical User Interfaceの頭文字を取って略したもの。 文字が並んだだけの(Character User Interface : CUI)の逆。 b. キーボード以外の操作も可能。 これにより操作方が増す。 プログラムについて a. 1、2行目の記述はGUIプログラムには必要なものです。 import java.awt.*; //GUI関係のパッケージクラス群をインポートする。 import java.awt.event.*; //アクションリスナー(イベント処理)を使うのに必要 b. グラフィックの出力 Button b0 = new Button("Enter"); これでボタンを作成。末尾の括弧内の文字はそのままボタンの中に出力される。 Label x0 = new Label("数字を入力してください"); ラベルの作成。括弧内の文字をラベルとして出力。 TextField t0 = new TextField(); ウィンドウの作成。 c. グラフィックの配置 add(t0); t0.setBounds(10, 40, 90, 30); t0(ボタン)の配置。末尾の括弧内は順に(ウィンドウの左上を中心にx座標、y座標、横幅、縦幅)以下同じ。 d. イベント 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.applet.Applet; import java.awt.*; public class ALLGUI extends Applet { Button button = new Button("Button"); Label label = new Label("Sample Components"); Label label2 = new Label("Your PC's OS"); Label label3 = new Label("Programing Language"); Label label4 = new Label("Music Genre"); TextField textfield = new TextField("TextField"); Checkbox checkbox0 = new Checkbox("Win"); Checkbox checkbox1 = new Checkbox("Mac"); List list = new List(); TextArea textarea = new TextArea("TextArea"); Choice choice = new Choice(); public void init() { setLayout(null); add(label); label.setBounds(10 ,10 ,300,30 ); add(label2); label2.setBounds(170,110,100,20 ); add(label3); label3.setBounds(170,160,100,20 ); add(label4); label4.setBounds(10 ,90 ,100,20 ); add(textfield); textfield.setBounds(10 ,50 ,300,30 ); add(list); list.setBounds(10 ,110,150,100); add(checkbox0); checkbox0.setBounds(170,130,60 ,30 ); add(checkbox1); checkbox1.setBounds(240,130,60 ,30 ); add(choice); choice.setBounds(170,180,150,30 ); add(button); button.setBounds(10 ,220,300,30 ); add(textarea); textarea.setBounds(10 ,260,300,50 ); list.add("Ballad"); list.add("Blues/R&B"); list.add("Jazz"); list.add("Hip Hop"); list.add("Classic Rock"); list.add("Classical"); list.add("Country"); list.add("Dance"); list.add("Folk"); list.add("J-pop"); list.add("Rock"); list.add("Trance"); list.add("Enka"); choice.add("C"); choice.add("Java"); } } |
なかなか形にするのが難しかったが、それなりのものはできたと思う。 今度はもっとアンケートに近い形にしてみたい。
import java.awt.*; import java.awt.event.*; public class Temp extends Frame { Button b0 = new Button("華氏F→摂氏C"); Button b1 = new Button("摂氏C→華氏F"); Label x0 = new Label("温度を入力してください"); TextField t0 = new TextField(); public Temp() { setLayout(null); add(t0); t0.setBounds(10, 30, 50, 20); add(b0); b0.setBounds(80, 30, 120, 30); add(b1); b1.setBounds(80, 60, 120, 30); add(x0); x0.setBounds(10, 100, 180, 60); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ float i = (new Float(t0.getText())).floatValue(); t0.setText(""); float ond =(new Float((i-32)*5/9)).floatValue(); x0.setText("華氏"+i+"\n摂氏"+ond); }catch(NumberFormatException e){ x0.setText("数字を入力してください"); } } }); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ float i = (new Float(t0.getText())).floatValue(); t0.setText(""); float ond =(new Float((i*9/5)+32)).floatValue(); x0.setText("華氏"+ond+"\n摂氏"+i); }catch(NumberFormatException e){ x0.setText("数字を入力してください"); } } }); } public static void main(String[] args) { Frame win = new Temp(); win.setSize(200, 150); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
F(華氏温度)→C(摂氏温度)への変換計算方法 C(摂氏温度) = (指定した数 - 32) * 5 / 9 C(摂氏温度)→F(華氏温度)への変換計算方法 F(華氏温度) = (指定した数 * 9 / 5) + 32
import java.awt.*; import java.awt.event.*; public class Cal extends Frame{ Button b0 = new Button("+"); Button b1 = new Button("−"); Button b2 = new Button("×"); Button b3 = new Button("÷"); TextField t0 = new TextField(); TextField t1 = new TextField(); TextField t2 = new TextField(); TextField t3 = new TextField(); Label x0 = new Label("="); public Cal(){ setLayout(null); add(t0); t0.setBounds(20,60,40,20); add(t1); t1.setBounds(80,60,40,20); add(t2); t2.setBounds(140,60,40,20); add(t3); t3.setBounds(60,60,20,20); add(b0); b0.setBounds(30,90,30,20); add(b1); b1.setBounds(65,90,30,20); add(b2); b2.setBounds(100,90,30,20); add(b3); b3.setBounds(135,90,30,20); add(x0); x0.setBounds(120,60,20,20); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("+"); t2.setText("" + (a+b)); }catch(Exception ex){ t1.setText("error"); } } }); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("−"); t2.setText("" + (a-b)); }catch(Exception ex){ t1.setText("error"); } } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("×"); t2.setText("" + (a*b)); }catch(Exception ex){ t1.setText("error"); } } }); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int a = (new Integer(t0.getText())).intValue(); int b = (new Integer(t1.getText())).intValue(); t3.setText("÷"); t2.setText("" + (a/b)); }catch(Exception ex){ t1.setText("error"); } } }); } public static void main(String[] args) { Frame win = new Cal(); win.setSize(230, 200); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
☆考察
とても簡単な四則演算しかできない計算プログラムにしてみた。 割った時の余りのことは考慮に入れていないので「3割る4」の場合は答えが0となる。
なかなかややこしかったが完成してみると面白いなと思った。 次の課題はもう少し難しくしてみよう。ひとつ前のページに戻る