課題
1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
2.例外処理について、考察せよ。
3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
5.『電卓』プログラム。中身は自分の思うように。
1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ
import java.awt.*;
import java.awt.event.*;
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); // 部品の自動配置機能をオフにする
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へ生成・初期化
win.setSize(200, 150); // ウィンドウの大きさ
win.setVisible(true); // ディスプレイへの表示
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0); // 現在のプログラムを終了する
}
});
}
}
<<考察>>
Button ---> ユーザーがボタン境界の内側でマウスをクリックすると、ユーザー
にフィードバックを提供するようにボタンの外観が変化します。
Label(String str, int align) --->
strはラベルテキストで、alignは、ラベルを左揃え、中央揃え、右揃えのうち
どれにするかを示す定数です。
定数LEFT,CENTER,RIGHTとして位置合わせの値を設定する。
GUI上に表示される文字列で、プログラムでは変更できますが、ユーザーが変
更することはできません。
TextField(String str, int cols) --->
strはフィールドに入力されたテキストで、引数colsでは、フィールドの幅を
文字数で指定します。
TextFieldでは、1行のテキストを入力することができます。文字を入力すると、
テキストイベントが生成されます。
ENTERキーを押したときにアクションイベントを生成されます。
setLayout() ---> 要素を配置する座標を計算する必要がないので、時間の節
約にとなり、
ユーザーは間違いを起こしやすい作業を行わずにすみます。
2つ目にサイズを変更すると、要素の配置を動的に調整します。
3つ目にコンポーネントは一般的にプラットフォームによってサイズがことな
ります。
そのため、すべての環境に適した座標を一度に指定することはできません。
ここではsetLayout(null);になっているのでこれらの機能は失っている。
ActionEvent ---> ボタンを押したり、リスト項目をダブルクリックしたとき、
メニュー項目を選択したときに生成される。
setSize(); ---> 要素の数をsizeに変更する。sizeからはみ出した要素は破棄
される。
アダプタクラス ---> 特定のリスナインターフェイス内のすべてのメソッドの
空実装を提供します。
これは、そのインターフェイスで定義されるメソッドの一部だけをオーバーラ
イドしたい場合に便利です。
windowClosing ---> ユーザーがウィンドウのクローズを要求するとwindowClo
sing()メソッドが呼び出されます。
WindowEvent ---> ユーザーがウィンドウを活動化したとき、閉じたとき、非
活動化したとき、
アイコン化解除したとき、アイコン化したとき、開いたとき、終了したときに
生成される。
2.例外処理について、考察せよ。
try catch文はtryブロック文が実行されたときに発生する例外処理のク
ラスと
一致するcatchブロックの文が実行される。
もしfinallyブロックがあれば、例外が発生しなくても実行される。
書き方は次のようにやる。
try{
文
}catch(例外クラス1 ex){
文
}
finally{
文
}
例外クラスは、java.lang.Exceptionクラスのサブクラスとして
IllegalAccessException,InstantiationException,InterruptedException,
NosuchMethodException,RuntimeException等がある。
3.上述のサンプルプログラムにでてきたGUI部品を、全て使ったプログラムを作成せよ。
import java.awt.*;
import java.awt.event.*;
public class GUIbb extends Frame { //Frameクラスの継承
Button b0 = new Button("Button"); //ボタンの中身
Label x0 = new Label(" A Label ");
TextField t0 = new TextField();
Checkbox c0 = new Checkbox("Check");
List l1 = new List();
TextArea t1 = new TextArea("kakikomi");
Choice c1 = new Choice();
Choice c2 = new Choice();
public GUIbb() {
setLayout(null); //部品の自動配置機能をオフにする
add(b0); b0.setBounds(10, 110, 80, 30); //ボタンの貼付け
add(x0); x0.setBounds(10, 50, 60, 30); //表示欄の貼付け
add(t0); t0.setBounds(80, 50, 300, 30); //入力欄の大きさ
add(c0); c0.setBounds(170, 110, 60, 30);//Checkboxの大きさ
add(l1); l1.setBounds(10, 150, 140, 210);//Listの大きさ
//Listの中身//
l1.add("RAKE"); l1.add("BEVEL"); l1.add("MILDSEVEN");
l1.add("PREMIUM"); l1.add("VANTAGE"); l1.add("NEXT");
l1.add("URUMA"); l1.add("LUCKYSTRIKE"); l1.add("VIOLET");
l1.add("CAMEL"); l1.add("KENT"); l1.add("CAPTAINBLACK");
l1.add("SEVENSTAR"); l1.add("CABIN"); l1.add("COOL");
add(t1); t1.setBounds(170, 150, 220, 100);//入力欄の大きさ
add(c1); c1.setBounds(170, 260, 100, 50); //Choice1の貼付け
//Choice1の中身//
c1.add("A"); c1.add("B"); c1.add("C");
c1.add("D"); c1.add("E"); c1.add("F");
c1.add("G"); c1.add("H"); c1.add("I");
c1.add("J"); c1.add("K"); c1.add("L");
c1.add("M"); c1.add("N");
add(c2); c2.setBounds(170, 310, 100, 50);//Choice2の貼付け
//Choice2の中身//
c2.add("O"); c2.add("P"); c2.add("Q");
c2.add("R"); c2.add("S"); c2.add("T");
c2.add("U"); c2.add("V"); c2.add("W");
c2.add("X"); c2.add("Y"); c2.add("Z");
}
public static void main(String[] args) {
Frame win = new GUIbb(); //前述のオブジェクトを、変数winへ生成・
初期化
win.setSize(400, 390); //ウィンドウの大きさ
win.setVisible(true); //ディスプレイへの表示
}
}
4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
import java.awt.*;
import java.awt.event.*;
public class GUIk extends Frame { // Frameクラスの継承
Button b0 = new Button("Centigrade"); // ボタン1の中身
Button b1 = new Button("Fahrenheit"); // ボタン2の中身
Label x0 = new Label("Type a Fahrengeit");
Label x1 = new Label("Type a Centigrade");
TextField t0 = new TextField();
public GUIk() {
setLayout(null); // 部品の自動配置機能をオフにする
add(t0); t0.setBounds(10, 40, 280, 30); // 入力欄の貼付け
add(b0); b0.setBounds(90, 80, 120, 30); // ボタン1の貼付け
add(b1); b1.setBounds(90, 180, 120, 30); // ボタン2の貼付け
add(x0); x0.setBounds(10, 110, 180, 40); // 表示欄1の貼付け
add(x1); x1.setBounds(10, 210, 180, 40); // 表示欄2の貼付け
// FahrenheitからCentigrade //
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
double d1 = (new Integer(t0.getText())).doubleValue();
t0.setText("");
d1 = (d1 - 32) * 0.55;
x0.setText("Centigrade is " + d1);
}
});
// CentigradeからFahrengeit //
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
double d2 = (new Integer(t0.getText())).doubleValue();
t0.setText("");
d2 = d2 * 1.8 + 32;
x1.setText("Fahrenheit is " + d2);
}
});
}
public static void main(String[] args) {
Frame win = new GUIk(); // 前述のオブジェクトを、変数winへ生成・初期化
win.setSize(300, 300); // ウィンドウの大きさ
win.setVisible(true); // ディスプレイへの表示
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0); // 現在のプログラムを終了する
}
});
}
}
5.「電卓」プログラム。中身は自分の思うように。
import java.awt.*;
import java.awt.event.*;
public class Dentaku extends Frame {
Label x0 = new Label();
Label x1 = new Label();
TextField t0 = new TextField("0");
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 ba = new Button("+");
Button bb = new Button("-");
Button bc = new Button("*");
Button bd = new Button("/");
Button be = new Button("AC");
Button bf = new Button("=");
int wa,sa,seki,sho = 0;
public Dentaku() {
setLayout(null);
add(t0); t0.setBounds(60,100,150,30);
add(x0); x0.setBounds(60,60,100,40);
add(x1); x1.setBounds(150,60,150,40);
add(b0); b0.setBounds(50,300,50,40);
add(bf); bf.setBounds(100,300,100,40);
add(be); be.setBounds(250,150,70,190);
add(bd); bd.setBounds(200,300,50,40);
add(b1); b1.setBounds(50,250,50,40);
add(b2); b2.setBounds(100,250,50,40);
add(b3); b3.setBounds(150,250,50,40);
add(bc); bc.setBounds(200,250,50,40);
add(b4); b4.setBounds(50,200,50,40);
add(b5); b5.setBounds(100,200,50,40);
add(b6); b6.setBounds(150,200,50,40);
add(bb); bb.setBounds(200,200,50,40);
add(b7); b7.setBounds(50,150,50,40);
add(b8); b8.setBounds(100,150,50,40);
add(b9);b9.setBounds(150,150,50,40);
add(ba); ba.setBounds(200,150,50,40);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i*10; //けた上げのため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);
}
});
ba.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText( Integer.parseInt(t0.getText()) + "+");
wa = Integer.parseInt(t0.getText());
t0.setText("0");
}
});
bb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText( Integer.parseInt(t0.getText()) + "-");
sa = Integer.parseInt(t0.getText());
t0.setText("0");
}
});
bc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText( Integer.parseInt(t0.getText()) + "*");
seki = Integer.parseInt(t0.getText());
t0.setText("0");
}
});
bd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText( Integer.parseInt(t0.getText()) + "/");
sho = Integer.parseInt(t0.getText());
t0.setText("0");
}
});
be.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x0.setText(""); x1.setText("");
wa = 0; sa = 0; seki = 0; sho = 0;
t0.setText("0");
}
});
bf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(wa !=0) {
wa = wa + Integer.parseInt(t0.getText());
String s = Integer.toString(wa);
x1.setText(Integer.parseInt(t0.getText()) +
" = " + s);
t0.setText(s);
}
if(sa !=0) {
sa = sa - Integer.parseInt(t0.getText());
String s = Integer.toString(sa);
x1.setText( Integer.parseInt(t0.getText())
+" = " + s);
t0.setText(s);
}
if(seki !=0) {
seki = seki * Integer.parseInt(t0.getText());
String s = Integer.toString(seki);
x1.setText( Integer.parseInt(t0.getText()) +" = " + s);
t0.setText(s);
}
if(sho !=0) {
sho = sho / Integer.parseInt(t0.getText());
String s = Integer.toString(sho);
x1.setText( Integer.parseInt(t0.getText())
+" = " + s);
t0.setText(s);
}
}
});
}
public static void main(String[] args) {
Frame win = new Dentaku();
win.setSize(400,400); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}}