1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。 2.例外処理について、考察せよ。 3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。 4.摂氏から華氏、華氏から摂氏への温度換算が出来るプログラムを作成せよ。 5.『電卓』プログラム。中身は自分の思うように。
1.偶数判定プログラム(GUIaa)
import java.awt.*; import java.awt.event.*; //GUI関係のパッケージクラス群をインポート public class GUIaa extends Frame { //Frameクラスの継承 //↓インスタンス変数としてボタン、表示欄、入力欄を生成・初期化 Button b0 = new Button("Even/Odd?"); Label x0 = new Label("Type a number and press..."); TextField t0 = new TextField(); public GUIaa() { //コンストラクタでウィンドウの初期設定 setLayout(null); //部品の自動配置機能をoffにする add(t0); t0.setBounds(10, 40, 90, 30); //入力欄の貼り付け(左上隅のx/y座標、幅、高さ) add(b0); b0.setBounds(110, 40, 110, 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(); //変数iを設定 t0.setText(""); if(i % 2 == 0) { //iが2で割り切れるなら x0.setText(i + " is Even"); //"i is Even"と表示 } else { //それ以外なら x0.setText(i + " is Odd"); //"i is Odd"と表示 } } }); } public static void main(String[] args) { Frame win = new GUIaa(); win.setSize(230, 150); win.setVisible(true); //ウィンドウのサイズディスプレイへ表示 win.addWindowListener(new WindowAdapter() { //ウィンドウを public void windowClosing(WindowEvent evt) { 閉じる(終了する)動作 System.exit(0); } }); } } [実行結果] [nw0311:~/java] j03011% javac GUIaa.java [nw0311:~/java] j03011% java GUIaa |
GUI(グラフィカルユーザーインターフェイス) GUIにはさまざまなコンポーネントを入れることができます。 ボタン、チェック ボックス、チョイス、リスト、スクロールバー、テキストエリア、 テキスト フィールド、メニュー などがその例である。 イベント処理 GUIの世界はイベントがあって何か処理をするという世界である。 JAVAでイベ ントを処理するということは、3つのクラスが必要になる。 1、イベントを認識するクラス 2、イベントに関するデータが格納されるクラス 3、イベントが起きた時にどのような処理をするかを記述したクラスListenerインターフェイス
Listenerの種類 | 内容 |
ActionListener | アクションイベントを受け取るためのリスナイ ンタフェイス |
AdjustmentListener | 調整イベントを受け取るためのリスナイン タフェイス |
AWTEventListener | ComponentやMenuComponent、またはそれらの サブクラスのインスタンスであるオブジェクトにディスパッチされるイベ ントの通知を受信するためのリスナインタフェイス |
ComponentListener | コンポーネントイベントを受け取るためのリ スナインタフェイス |
MouseListener | コンポーネント上の「関連する」マウスイベント (プレス、リリース、クリック、コンポーネントへの出入り)を受け取る ためのリスナインターフェイス |
KeyListener | このリスナは、キーボードイベント(キーストロー ク)を受け取るためのインタフェイス |
WindowListener | ウィンドウイベントを受け取るためのリスナイ ンタフェイス |
WindowListenerのメソッド | 内容 |
windowActivated(WindowEvent evt) | ウィンドウがユーザのアク ティブウィンドウに設定されたときに呼び出される。 |
windowClosed(WindowEvent evt) | ウィンドウに対するdisposeの 呼び出しの結果として、ウィンドウがクローズされた時に呼び出される。 |
windowClosing(WindowEvent evt) | ユーザーが、ウィンドウのシ ステムメニューでウィンドウを閉じようとした時に呼び出される。 |
windowDeactivated(WindowEvent evt) | ウィンドウがユーザのア クティブウィンドウではなくなった時に呼び出される。 |
windowDeiconified(WindowEvent evt) | ウィンドウが最小化され た状態から通常の状態に変更された時に呼び出される。 |
windowconified(WindowEvent evt) | ウィンドウが通常の状態から 最小化された状態に変更された時に呼び出される。 |
windowOpened(WindowEvent evt) | ウィンドウが最初に可視になっ た時に呼び出される。 |
2.例外処理について、考察せよ
import java.awt.*; import java.awt.event.*; //GUI関係のパッケージクラス群をインポート public class ColorRGBa extends Frame { //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); //部品の自動配置機能をoffにする 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, 80, 30); //ボタン欄の張り付け add(x0); x0.setBounds(10, 200, 220, 40); //表示欄の貼り付け b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { //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(240, 280); win.setVisible(true); //ウィンドウのサイズディスプレイへ表示 win.addWindowListener(new WindowAdapter() { //ウィンドウを閉じる動作 public void windowClosing(WindowEvent evt) { System.exit(0); //ウィンドウを閉じるとシステム終了 } }); } } [実行結果] [nw0311:~/java] j03011% javac ColorRGBa.java [nw0311:~/java] j03011% java ColorRGBa |
Javaでは、プログラム実行時のエラー(例外)が発生した場合に「例外処理」という機能で処理する。 try { // 例外を検出する文をここに書く } catch (例外のタイプ1 変数名) { // 例外のタイプ1が発生したとき処理する文をここに書く } catch (例外のタイプ2 変数名) { // 例外のタイプ2が発生したとき処理する文をここに書く } ・ ・ ・ catch (例外のタイプn 変数名) { // 例外のタイプnが発生したとき処理する文をここに書く } finally { // 例外発生の有無にかかわらず必ず実行する文をここに書く }
4.摂氏から華氏、華氏から摂氏への温度換算が出来るプログラムを作成せよ。
import java.awt.*; import java.awt.event.*; public class GUICF extends Frame{ Button b0 = new Button("C to F"); Button b1 = new Button("F to C"); Label x0 = new Label("Result"); Label x1 = new Label(""); TextField t0 = new TextField(""); public GUICF(){ setLayout(null); add(x0); x0.setBounds(90, 50, 50, 10); add(x1); x1.setBounds(140, 50, 100, 10); add(t0); t0.setBounds(20, 50, 60, 20); add(b0); b0.setBounds(10, 100, 100, 30); add(b1); b1.setBounds(110,100, 100, 30); b0.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ try{ int i = (new Integer(t0.getText())).intValue(); i = (int)(1.8 * i + 32); //C(摂氏)からF(華氏)に計算 x1.setText(i + " F"); }catch(NumberFormatException evn){ x1.setText("Error!! Put in a number"); } } }); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { try{ int j = (new Integer(t0.getText())).intValue(); j = (int)((j - 32)/1.8); //F(華氏)からC(摂氏)に計算 x1.setText(j + " C"); }catch(NumberFormatException evn){ x1.setText("Error!! Put in a number"); } } }); } public static void main(String[] args){ Frame win = new GUICF(); win.setSize(210,200); win.setVisible(true); win.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } } [実行結果] [nw0311:~/java] j03011% javac GUICF.java [nw0311:~/java] j03011% java GUICF |
3&5.GUI部品を、全て使ったプログラムを『電卓』プログラムで作成。
import java.awt.*; import java.awt.event.*; public class GUI 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("Clear"); 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"); Button b10= new Button("00"); Label x0 = new Label("SisokuEnzan"); Label x1 = new Label("Result!"); Label xx = new Label(""); TextField t0 = new TextField("0"); public GUI() { 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(220,230,80,30); add(bb); bb.setBounds(220,190,80,30); add(bc); bc.setBounds(220,150,80,30); add(bd); bd.setBounds(220,110,80,30); add(be); be.setBounds(220,70,80,30); add(bf); bf.setBounds(160,230,60,30); add(bg); bg.setBounds(40,70,180,30); 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); add(b10); b10.setBounds(100,230,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); } }); b10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int i = Integer.parseInt(t0.getText()); int sum = i*100; String s = Integer.toString(sum); t0.setText(s); } }); } public static void main(String[] args) { Frame win = new GUI(); win.setSize(330,300); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } [実行結果] [nw0311:~/java] j03011% javac GUI.java [nw0311:~/java] j03011% java GUI |
なんだか、とても楽しかったです。 でも、なんだかとても疲れました。 この苦労が私の血となり肉となる日 が来たらいいなと思います。 [参考資料] Javaへの道
Pratical Programming Java JavaJavaしてんじゃねぇよ