• 1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せ よ。
  • ソース

    01 import java.awt.*; 02 import java.awt.event.*; 03 04 public class GUIaa extends Frame { 05 Button b0 = new Button("Even/Odd?"); 06 Label x0 = new Label("Type a number and press..."); 07 TextField t0 = new TextField(); 08 09 public GUIaa() { 10 setLayout(null); 11 add(t0); t0.setBounds(10, 40, 90, 30); 12 add(b0); b0.setBounds(110, 40, 100, 30); 13 add(x0); x0.setBounds(10, 80, 180, 30); 14 b0.addActionListener(new ActionListener() { 15 public void actionPerformed(ActionEvent evt) { 16 int i = Integer.parseInt(t0.getText()); 17 18 t0.setText(""); 19 if(i % 2 == 0) { 20 x0.setText(i + " is Even"); 21 } else { 22 x0.setText(i + "is Odd"); 23 } 24 } 25 }); 26 } 27 public static void main(String[] args) { 28 Frame win = new GUIaa(); 29 win.setSize(200, 150); win.setVisible(true); 30 win.addWindowListener(new WindowAdapter() { 31 public void windowClosing(WindowEvent evt) { 32 System.exit(0); 33 } 34 }); 35 } 36 }

    実行結果

    rep7-1 rep7-2rep7-3
    02:01java.awt.event」までなのでそれより下は 「java.awt.event.*」で指定して上げないといけない
    04:Frameクラスの継承。ウィンドウを表すクラスはFrameクラスを使う。
    07:インスタンス変数として入力欄を生成・初期化。
    09:コンストラクタでウィンドウの初期設定。
    10:部品の自動配置機能をoffにする
    18:偶数奇数の判定。値を2で割って余りが0なら偶数/Even、それ以外は奇数/Odd。 結果をラベルに表示する。
    30:ウィンドウを閉じる時のイベントを無名クラスで定義する。

  • 2.例外処理について、考察せよ。
  •     try {
                例外が発生しそうな処理
        } catch (例外1クラス 変数) {
                例外処理1
        } catch (例外2クラス 変数) {
                例外処理2
        } finally {
                共通処理
        }
    

    例外が発生しなかった場合、共通処理が実行される。
    例外1が発生した場合、対応する例外処理1が実行され、最後に共通処理が実行 される。
    例外2が発生した場合、対応する例外処理2が実行され、最後に共通処理が実行 される。


    ソース

    import java.awt.*;
        import java.awt.event.*;
    
        public class TryCatch extends Frame {
            Button    b0 = new Button("Even/Odd?");
            Label     x0 = new Label("Type a number and press...");
            TextField t0 = new TextField();
    
            public TryCatch() {
                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) {
                        try{
                            int i = (new Integer(t0.getText())).intValue();
                            t0.setText("");
                            if(i % 2 == 0) {
                                x0.setText(i + " is Even");
                            } else {
                                x0.setText(i + " is Odd");
                            }
                        }catch(NumberFormatException ee){
                            x0.setText("error");}
                    }
                });
            }
    
            public static void main(String[] args) {
                Frame win = new TryCatch();
                win.setSize(200, 150); win.setVisible(true);
                win.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent evt) {
                        System.exit(0);
                    }
                });
            }
        }
    
    

    実行結果

    rep7-4 数字以外が入力された場合例外処理errorを表示。
    例外はオブジェクトとして扱われる。 例外が発生すると、その種類に応じたクラス型のオブジェクトが生成される。 例外処理とは、このオブジェクトを捕まえて、処理すること。

  • 3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプ ログラムを作成せよ。
  • ソース

    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("使っているパソコン"); Label label3 = new Label("好きな言語"); Label label4 = new Label("好きな音楽"); 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("ポップス"); list.add("ロック"); list.add("ジャズ"); list.add("ヒップホップ"); list.add("RB"); list.add("ハードロック"); list.add("へヴィーメタル"); list.add("ブルース"); list.add("パンク"); choice.add("C"); choice.add("Java"); } }

    実行結果
    rep7-5

    考察

     ActionEventはダブルクリックで決定した時に投げられる。
      ItemEventはクリックで選択、もしくは選択解除時に投げられる。
      クリックすると四角い窓にチェックが入り、既入時は消される。
      仕様可能イベントはItemEvent。getStateChange() メソッドでオン・オフを判別。
      Listコンポネートを生成すると長方形のスペースができ、その
      中に項目を置く。
    

  • 4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを 作成せよ。
  • ソース

    import java.awt.*; import java.awt.event.*; public class CtoF extends Frame { Button but = new Button("変換"); Label lab = new Label("温度を入力して下さい"); TextField txt = new TextField(); Checkbox cb0 = new Checkbox("華氏→摂氏"); Checkbox cb1 = new Checkbox("摂氏→華氏"); CheckboxGroup cbG = new CheckboxGroup(); public CtoF() { setLayout(null); add(txt); txt.setBounds(10, 40, 90, 30); add(but); but.setBounds(110, 40, 80, 30); add(lab); lab.setBounds(10, 90, 180, 20); cb0.setCheckboxGroup(cbG); cb1.setCheckboxGroup(cbG); cb0.setState(true); add(cb0); cb0.setBounds(10, 105, 100, 15); add(cb1); cb1.setBounds(10, 120, 100, 15); but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String str; int i; try{ double f = Double.parseDouble(txt.getText()); if(cb0.getState() == true){ str = Double.toString((f-32.0)*5.0/9.0); i = str.indexOf('.'); //System.out.println(str); lab.setText(f+"F = " +str.concat("000").substring(0,i+3)+"℃"); }else{ str = Double.toString((f*9.0/5.0)+32); i = str.indexOf('.'); //System.out.println(str); lab.setText(f+"。?= " +str.concat("000").substring(0,i+3)+"F"); } }catch(Exception e){ lab.setText("数値のみを入力して下さい(半角)"); } txt.requestFocus(); txt.setText(""); } }); } public static void main(String[] args) { Frame win = new CtoF(); win.setSize(200, 150); win.setVisible(true); win.setTitle("摂氏華氏変換"); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }

    実行結果

    rep7-6rep7-8

    考察

    奇数偶数判定プログラムと似たような処理。こっちが、難しい。

  • 5.「電卓」プログラム。中身は自分の思うように。
  • ソース

    import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Keisannki extends Applet{ long temp = 0; long temp2 = 0; String x = "="; boolean input = false; boolean input2 = false; int count = 0; TextField t0 = new TextField("0"); Label l0 = new Label(""); 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 bC = new Button("C"); Button bAC= new Button("AC"); Button ba = new Button("+"); Button bb = new Button("-"); Button bc = new Button("×"); Button bd = new Button("÷"); Button be = new Button("="); class Buttonevent implements ActionListener {/*数字ボタンイベン ト用リスナ*/ public void actionPerformed(ActionEvent ae){ if(input == true){/*lv1 符号入力有り(符号入力あ り、初期化必要)*/ if(input2 == true){/*lv2 符号入力による 初期化判定(初期化済み)*/ if(count == 0){/*lv3 既入力判定 (初入力)*/ if(ae.getActionCommand() != "0"){/*lv4 0入力判定(0入力でない)*/ t0.setText(ae.getActionCommand()); count++; }else{/*lv4 0入力判定(0 入力)*/ t0.setText(ae.getActionCommand()); } }else{/*lv3 既入力判定(既入力) */ t0.setText(t0.getText() + ae.getActionCommand()); count++; } }else{/*lv2 符号入力による初期化判定(初 期化前)*/ if(ae.getActionCommand() != "0"){/*lv3 0入力判定(0入力でない)*/ t0.setText(ae.getActionCommand()); count++; }else{/*lv3 0入力判定(0入力)*/ t0.setText(ae.getActionCommand()); } input2 = true; } }else{/*lv1 符号入力判定(符号入力無し、初期化不 必要)*/ if(count == 0){/*lv2 既入力判定(初入力) */ if(ae.getActionCommand() != "0"){/*lv3 0入力判定(0入力でない)*/ t0.setText(ae.getActionCommand()); count++; }else{/*lv3 0入力判定(0入力)*/ t0.setText("0"); } }else{/*lv2 既入力判定(既入力)*/ t0.setText(t0.getText() + ae.getActionCommand()); count++; } } } } class Equalevent implements ActionListener {/*演算イベント用リス ナ*/ public void actionPerformed(ActionEvent ae){ long temp3; try{/*エラー発生箇所*/ if(x != "="){/*lv1 最低1回の符号入力の有 無判定(有り)*/ if(input != true){/*lv2 符号変更 判定(変更無し)*/ temp = Long.parseLong(t0.getText()); }else{/*lv2 符号変更判定(変更有 り)*/ temp2 = Long.parseLong(t0.getText()); } if(x == "+"){/*lv2 演算条件群*/ t0.setText(Long.toString(temp+temp2)); }else{ if(x == "-"){ t0.setText(Long.toString(temp-temp2)); }else{ if(x == "*"){ t0.setText(Long.toString(temp*temp2)); }else{ if(x == "/"){ t0.setText(Long.toString(temp/temp2)); }}}} if(Long.parseLong(t0.getText()) == 0){/*結果が0の時を例外として処理*/ count = 0; } input = false; input2 = false; } }catch(Exception ex){/*エラー処理*/ l0.setText("E"); t0.setText("0"); count = 0; temp = 0; temp2 = 0; x = "="; input = false; input2 = false; } } } class Xevent implements ActionListener {/*符号変更イベント用リス ナ*/ public void actionPerformed(ActionEvent ae){ try{/*エラー発生箇所*/ temp = Long.parseLong(t0.getText()); x = ae.getActionCommand(); input = true; count = 0; l0.setText(ae.getActionCommand()); }catch(Exception ex){/*エラー処理*/ l0.setText("E"); t0.setText("0"); count = 0; temp = 0; temp2 = 0; x = "="; input = false; input2 = false; } } } public void init() {/*コンストラクタ(レイアウト、イベント設定) */ setLayout(null);/*レイアウトマネージャのカット*/ add(t0); t0.setBounds(50, 40, 150, 20); /*以下、レイアウ トの決め打ち*/ t0.setEditable(false); add(l0); l0.setBounds(20, 40, 30, 20); add(b0); b0.setBounds(10, 240, 40, 30); add(b1); b1.setBounds(10, 200, 40, 30); add(b4); b4.setBounds(10, 160, 40, 30); add(b7); b7.setBounds(10, 120, 40, 30); add(b2); b2.setBounds(60, 200, 40, 30); add(b5); b5.setBounds(60, 160, 40, 30); add(b8); b8.setBounds(60, 120, 40, 30); add(b3); b3.setBounds(110, 200, 40, 30); add(b6); b6.setBounds(110, 160, 40, 30); add(b9); b9.setBounds(110, 120, 40, 30); add(be); be.setBounds(60, 240, 90, 30); add(bC); bC.setBounds(110, 80, 40, 30); add(bAC); bAC.setBounds(10, 80,90 ,30); add(ba); ba.setBounds(170, 120, 40, 30); add(bb); bb.setBounds(170, 160, 40, 30); add(bc); bc.setBounds(170, 200, 40, 30); add(bd); bd.setBounds(170, 240, 40, 30); b0.addActionListener(new Buttonevent());/*以下、イベント のリスナを登録*/ b1.addActionListener(new Buttonevent()); b2.addActionListener(new Buttonevent()); b3.addActionListener(new Buttonevent()); b4.addActionListener(new Buttonevent()); b5.addActionListener(new Buttonevent()); b6.addActionListener(new Buttonevent()); b7.addActionListener(new Buttonevent()); b8.addActionListener(new Buttonevent()); b9.addActionListener(new Buttonevent()); bC.addActionListener(new ActionListener(){/*Clearボタン (無名クラスリスナ)*/ public void actionPerformed(ActionEvent ae){ t0.setText("0"); count = 0; } }); bAC.addActionListener(new ActionListener(){/*AllClearボ タン(無名クラスリスナ)*/ public void actionPerformed(ActionEvent ae){ t0.setText("0"); l0.setText(""); count = 0; temp = 0; temp2 = 0; x = "="; input = false; input2 = false; } }); be.addActionListener(new Equalevent()); ba.addActionListener(new Xevent()); bb.addActionListener(new Xevent()); bc.addActionListener(new Xevent()); bd.addActionListener(new Xevent()); } public static void main(String[] args) { Frame win = new GUIaa(); win.setSize(300, 410); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }

    実行結果

    rep7-7

    考察

    整数四則演算ができる。それだけになった。
    これが自分の理解の限界?のようです。
    
    

  • 感想
  • 疲れた。。難しいっすよマジ。。頭いてえ。。頑張りマッス!!