Report#7


~課題~
講義資料GUIプログラムをもとに、以下の課題を行え。
1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
2.例外処理について、考察せよ。
3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
5.「電卓」プログラム。中身は自分の思うように。


1.偶数奇数判定プログラム(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);
            }
        });
    }
}

実行結果

考察

4行目〜7行目
Button,Label,Textfieldを生成,初期化している。

9行目~13行目
Button,Lable,TextFieldのサイズと位置の設定。

14行目~25行目
Buttonが押されたときのイベント処理。
TextFieldに入力された文字を整数型に変換して偶数奇数判定をしている。

27行目
前述のオブジェクトを、変数winへ生成・初期化

28行目
ウィンドウの大きさの設定・表示

29行目~33行目
ウィンドウを閉じるときのイベント処理。 System.exit(0);で終了する。





2.例外処理について、考察せよ。
~ソースコード~
try{
   //tryブロック
}
catch (ExceptionType1 param1) {
   //例外処理ブロック
}
catch (ExceptionType2 param2) {
   //例外処理ブロック
}
       ・
    ・
     ・
catch (ExceptionTypeN paramN) {
   //例外処理ブロック
}
finally {
   //finallyブロック
}

Javaでは例外処理をする場合try文を用いる。

tryブロックには例外が発生する可能性の処理を、catchには例外時の処理を書く。

finallyブロックはtryブロック、catchブロックどちらが実行されても必ず実行されるようになっている。
finallyブロックは省略する事ができる。





3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
~ソースコード~
import java.awt.*;
import java.awt.event.*;

public class buhin extends Frame {
    Button    b0 = new Button("Green");
    Label     x0 = new Label("TextField");
    Label     x1 = new Label("Checkbox");
    Label     x2 = new Label("List");
    Label     x3 = new Label("TextArea");
    Label     x4 = new Label("choice");
    TextField t0 = new TextField();
    Checkbox  ch0 = new Checkbox("check1");
    Checkbox  ch1 = new Checkbox("check2");
    List      l1 = new List();
    TextArea  t1 = new TextArea("some text ...");
    Choice    c1 = new Choice();
    
    public buhin() {
        setLayout(null);
        add(b0); b0.setBounds(10, 40, 60, 30);
        add(x0); x0.setBounds(10, 80, 60, 30);
        add(x1); x1.setBounds(10, 120,60, 30);
        add(x2); x2.setBounds(180,100, 60, 30);
        add(x3); x3.setBounds(240,40, 60, 30);
        add(x4); x4.setBounds(10,200, 60, 30);
        add(t0); t0.setBounds(80, 80, 60, 30);
        add(ch0); ch0.setBounds(80, 120, 65, 30);
        add(ch1); ch1.setBounds(80, 160, 65, 30);
        add(l1); l1.setBounds(180, 120, 80, 100);
        l1.add("List0"); l1.add("List1"); l1.add("List2");
        l1.add("List3"); l1.add("List4");
        add(t1); t1.setBounds(300, 40, 100, 200);
        add(c1); c1.setBounds(10, 230, 60, 30);
        c1.add("whit");c1.add("Red"); c1.add("Green"); c1.add("Blue");
        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    t0.setBackground(new Color(0,255,0));
                }
            });
    }
    public static void main(String[] args) {
        Frame win = new buhin();
        win.setSize(500, 300);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}

実行結果




4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
~ソースコード~
import java.awt.*;
import java.awt.event.*;

public class ondo extends Frame{

    Button     b0 = new Button("C to F");
    Button     b1 = new Button("F to C");
    Label      x0 = new Label("temperature conversion");
    TextField  t0 = new TextField();

    public ondo(){
        setLayout(null);
        add(t0); t0.setBounds(90,60,50,20);
        add(b0); b0.setBounds(65,85,50,20);
        add(b1); b1.setBounds(115,85,50,20);
        add(x0); x0.setBounds(60,145,160,20);

        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try{
                        int i = (new Integer(t0.getText())).intValue();
                        i = (int)Math.rint(1.8*i+32);
                        x0.setText(t0.getText() +"C to " + i+"F");
                    }catch(Exception ex){
                        x0.setText("error");
                    }
                }
            });

        b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try{
                        int i = (new Integer(t0.getText())).intValue();
                        i = (int)Math.rint((i-32)/1.8);
                        x0.setText(t0.getText() +"F to "+ i+"C");
                    }catch(Exception ex){
                        x0.setText("error");
                    }
                }
            });
    }

    public static void main(String[] args) {
        Frame win = new ondo();
        win.setSize(230, 200); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}

実行結果




5.「電卓」プログラム。中身は自分の思うように。
~ソースコード~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class culc2 extends JApplet implements ActionListener{
   JLabel l1;
   JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,bp,bx;
   JPanel p1;
   double s1,s2;
   int fl1,fl2,fl3;

   public void init(){
      s1=0;
      s2=0;
      fl1=0;
      fl3=0;
      l1=new JLabel("0");
      l1.setHorizontalAlignment(SwingConstants.RIGHT);
      b0=new JButton("0");
      b1=new JButton("1");
      b2=new JButton("2");
      b3=new JButton("3");
      b4=new JButton("4");
      b5=new JButton("5");
      b6=new JButton("6");
      b7=new JButton("7");
      b8=new JButton("8");
      b9=new JButton("9");
      ba=new JButton("+");
      bb=new JButton("-");
      bc=new JButton("*");
      bd=new JButton("/");
      bp=new JButton("=");
      bx=new JButton("C");
      b0.addActionListener(this);
      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);
      b6.addActionListener(this);
      b7.addActionListener(this);
      b8.addActionListener(this);
      b9.addActionListener(this);
      ba.addActionListener(this);
      bb.addActionListener(this);
      bc.addActionListener(this);
      bd.addActionListener(this);
      bp.addActionListener(this);
      bx.addActionListener(this);
      p1=new JPanel();
      p1.setLayout(new GridLayout(4,4));
      p1.add(b7);
      p1.add(b8);
      p1.add(b9);
      p1.add(bd);
      p1.add(b4);
      p1.add(b5);
      p1.add(b6);
      p1.add(bc);
      p1.add(b1);
      p1.add(b2);
      p1.add(b3);
      p1.add(bb);
      p1.add(b0);
      p1.add(bp);
      p1.add(bx);
      p1.add(ba);
      Container c=getContentPane();
      c.setLayout(new BorderLayout());
      c.add("Center",p1);
      c.add("North",l1);
   }

   public void actionPerformed(ActionEvent e){
      fl2=0;
      if(e.getSource()==b0){
         s1=s1*10;
      }
      else if(e.getSource()==b1){
         s1=s1*10+1;
      }
      else if(e.getSource()==b2){
         s1=s1*10+2;
      }
      else if(e.getSource()==b3){
         s1=s1*10+3;
      }
      else if(e.getSource()==b4){
         s1=s1*10+4;
      }
      else if(e.getSource()==b5){
         s1=s1*10+5;
      }
      else if(e.getSource()==b6){
         s1=s1*10+6;
      }
      else if(e.getSource()==b7){
         s1=s1*10+7;
      }
      else if(e.getSource()==b8){
         s1=s1*10+8;
      }
      else if(e.getSource()==b9){
         s1=s1*10+9;
      }
      else if(e.getSource()==bx){
         s2=0;
         s1=0;
         fl1=0;
         fl3=0;
      }
      else if(e.getSource()==ba){
         keisan();
         fl1=1;
         fl2=1;
         fl3=1;
         s1=0;
      }
      else if(e.getSource()==bb){
         keisan();
         fl1=2;
         fl2=1;
         fl3=1;
         s1=0;
      }
      else if(e.getSource()==bc){
         keisan();
         fl1=3;
         fl2=1;
         fl3=1;
         s1=0;
      }
      else if(e.getSource()==bd){
         keisan();
         fl1=4;
         fl2=1;
         fl3=1;
         s1=0;
      }
      else if(e.getSource()==bp){
         keisan();
         s1=0;
         fl2=1;
      }

      if(fl2==0){
         l1.setText(Double.toString(s1));
      }
      else{
         l1.setText(Double.toString(s2));
      }
   }

   public void keisan(){
      if(fl3==0){
         s2=s1;
      }
      else if(fl1==1){
         s2=s2+s1;
      }
      else if(fl1==2){
         s2=s2-s1;
      }
      else if(fl1==3){
         s2=s2*s1;
      }
      else if(fl1==4){
         s2=s2/s1;
      }
   }

}

実行結果


戻る