import java.awt.*; import java.awt.event.*; public class repo7_1 extends Frame { Button b0 = new Button("Even/Odd?"); Label x0 = new Label("Type a number and press..."); TextField t0 = new TextField(); public repo7_1() { 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 repo7_1(); win.setSize(200, 150); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
○実行結果
テキストボックスに入力された文字列を受け取り、それが偶数か奇数か
を判定するプログラム。
テキストボックス、ラベル、ボタンの三つのGUIオブジェクトを使用してい
る。
これらのオブジェクトは、
・ボタンが押された時
・テキストボックスに文字が入力された時
・マウスが重なった時
●GUIについて
簡単に言えばアイコンやボタンなどを使ってコンピュータを操作する方法であり、
絵やアイコンなどで表すため初心者にもわかりやすいといった利点がある。
●ユーザーインターフェースにはGUIの他にCUIがあり、 GUIがグラフィック
ベースであるのに対し、
CUIは文字ベースのUIとなっている。
●GUIプログラムはイベントを受けとる事で処理を行う。
●java.awt.*とjava.awt.event.*パッケージについて
java.awt.* : ユーザインターフェースの作成と、グラフィックスとイメージのペイントの為のクラス、インターフェースが含まれている。
AWT(Abstract Window Toolkit)のクラス群であり、ウィンドウオブジェクトの生成、GUIコンポーネントの生成などができる。
java.awt.event.* : イベント処理のクラスとインターフェースが含まれている。
●Listenerについて
Listenerはイベントが発生したときにそのイベントを受け取り、イベント処理
を行うメソッドを呼び出すための インターフェースである。Listenerにはマウスに関するものやウィンドウに関するものなど様々な種類がある。
●addActionListenerとaddWindowListenerについて
これらのadd〜Listenerメソッドはイベントを発生させる部品のインスタンスに
Listenerを組み込むような動作を行う。
つまり、このメソッドを使う事により、Listenerとその部品との連結のような
ことを行うわけである。
2,問題(2):例外処理
プログラム
public class Repo7test06 { public static void main(String[] args) { int a = 5; int b = 8; int c = 0; try{ System.out.println("a/b=" + a/b); System.out.println("b/c=" + b/c); } catch(Exception err){ System.out.println("エラーです"); } finally{ System.out.println("計算終了"); } } }
a/b=0 エラーです 計算終了
try{ //ここに例外が発生しそうな処理を書く } catch(例外クラス1 変数){ //例外処理1を書く } catch(例外クラス2 変数){ //例外処理2を書く } finally{ //共通処理を書く }例外が発生しなかった場合は共通処理が実行される。
例外クラス | 内容 |
ArrayIndexOutOfBoundsException | 定義済みの配列を超えたとき |
ClassNotFoundException | クラスが見つからないとき |
FileNotFoundException | ファイルが見つからないとき |
IllegalArgumentException | 引数が不正なとき |
NumberFormatException | 数値フォーマットが異なるとき |
NullPointerException | nullオブジェクトの不正な使用を示すとき |
import java.awt.*; import java.awt.event.*; public class Rep7B extends Frame{ Button b0 = new Button("OK"); Label x0 = new Label("あなたの名前は"); Label x1 = new Label("住所"); Label x2 = new Label("都道府県"); Label x3 = new Label("市町村"); Label x4 = new Label("質問です。該当するものにチェックして下 さい。"); Label x5 = new Label("みんなに何かメッセージがあれば書いて下 さい。"); Label x6 = new Label("Thank you!!"); TextField t0 = new TextField(); TextArea t1 = new TextArea("some text ..."); Choice c0 = new Choice(); Checkbox c1 = new Checkbox("毎年年賀状を書いている。"); Checkbox c2 = new Checkbox("近頃年賀状を貰った覚えがない。"); Checkbox c3 = new Checkbox("郵便受けに年賀状の束を見つけると、自 分宛のものがないか必ず探す? 。"); List l1 = new List(); public Rep7B() { setLayout(null); add(b0); b0.setBounds(100, 600, 40, 20); add(x0); x0.setBounds(30, 40, 100, 20); add(x1); x1.setBounds(30, 80, 40, 20); add(x2); x2.setBounds(40, 110, 80, 20); add(x3); x3.setBounds(210, 110, 100, 20); add(x4); x4.setBounds(30, 240, 120, 20); add(x5); x5.setBounds(30, 380, 430, 20); add(x6); x6.setBounds(30, 640, 430, 20); add(t0); t0.setBounds(140, 40, 100, 20); add(t1); t1.setBounds(50, 420, 350, 150); add(c0); c0.setBounds(105, 110, 80, 20); c0.add("沖縄県"); c0.add("その他"); add(c1); c1.setBounds(60, 270, 300, 20); add(c2); c2.setBounds(60, 300, 300, 20); add(c3); c3.setBounds(60, 330, 430, 20); add(l1); l1.setBounds(280, 110, 70, 120); l1.add("北谷町"); l1.add("北中城村"); l1.add(" 宜野湾市"); l1.add("中城村"); l1.add("浦添市"); l1.add("那 覇市"); l1.add("西原町"); l1.add("沖縄市"); l1.add("与 那原町"); l1.add("その他"); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { x6.setText("何で毎年家族で俺にだけ年賀状が来ないんだろ う?"); } }); } public static void main(String[] args) { Frame win = new Rep7B(); win.setSize(600, 900); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
import java.awt.*; import java.awt.event.*; public class GUIcc extends Frame{ Button b0 = new Button("摂氏→華氏"); Button b1 = new Button("華氏→摂氏"); Label x0 = new Label("変換のボタンを押して下さい"); Label x1 = new Label("→"); TextField[] t0 = new TextField[]{new TextField(), new TextField()}; public GUIcc(){ setLayout(null); add(x0); x0.setBounds(10, 30, 100, 30); add(t0[0]); t0[0].setBounds(10, 70, 70, 30); add(t0[1]); t0[1].setBounds(100, 70, 70, 30); add(x1); x1.setBounds(80, 70, 20, 30); add(b0); b0.setBounds(5, 110, 160, 30); add(b1); b1.setBounds(5, 150, 160, 30); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ t0[1].setText("" +((new Float(t0[0].getText()).floatValue()*1.8+32))); }catch(Exception ex){t0[1].setText(ex.toString());} } }); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ t0[1].setText("" +Math.round(((new Float(t0[0].getText()).floatValue()*(0.555)-(17.777))))); }catch(Exception ex){t0[1].setText(ex.toString());} } }); } public static void main(String[] args){ Frame win = new GUIcc(); win.setSize(250, 200); win.setVisible(true); win.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); } }
import java.awt.*; import java.awt.event.*; public class Rep7D 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 i = 0, y = 0; public Rep7D() { setLayout(null); add(b0); b0.setBounds(50,300,50,40); add(bf); bf.setBounds(100,300,100,40); 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); add(be); be.setBounds(250,150,70,190); add(t0); t0.setBounds(60,100,150,30); add(x0); x0.setBounds(60,60,100,40); add(x1); x1.setBounds(150,60,150,40); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 1; String s1 = Integer.toString(i); t0.setText(s1); } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 2; String s1 = Integer.toString(i); t0.setText(s1); } }); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 3; String s1 = Integer.toString(i); t0.setText(s1); } }); b4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 4; String s1 = Integer.toString(i); t0.setText(s1); } }); b5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 5; String s1 = Integer.toString(i); t0.setText(s1); } }); b6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 6; String s1 = Integer.toString(i); t0.setText(s1); } }); b7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 7; String s1 = Integer.toString(i); t0.setText(s1); } }); b8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 8; String s1 = Integer.toString(i); t0.setText(s1); } }); b9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 9; String s1 = Integer.toString(i); t0.setText(s1); } }); ba.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = i * 10 + 9; String s1 = Integer.toString(i); t0.setText(s1); } }); ba.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { y = y + i; String s2 = Integer.toString(y); x0.setText(s2 + "+"); t0.setText("0"); i = 0; bf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { y = y + i; String s2 = Integer.toString(y); x0.setText(s2); t0.setText("0"); } }); } }); bb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (y == 0) { y = i - y; } else { y = y - i; } String s2 = Integer.toString(y); x0.setText(s2 + "-"); t0.setText("0"); i = 0; bf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { y = y - i; String s2 = Integer.toString(y); x0.setText(s2); t0.setText("0"); } }); } }); bc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (y == 0) { y = 1 * i; } else { y = y * i; } String s2 = Integer.toString(y); x0.setText(s2 + "*"); t0.setText("0"); i = 0; bf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { y = y * i; String s2 = Integer.toString(y); x0.setText(s2); t0.setText("0"); } }); } }); bd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (y == 0) { y = i / 1; } else { y = y / i; } String s2 = Integer.toString(y); x0.setText(s2 + "/"); t0.setText("0"); i = 0; bf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { y = y / i; String s2 = Integer.toString(y); x0.setText(s2); t0.setText("0"); } }); } }); be.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { i = 0; y = 0; String s2 = Integer.toString(y); x0.setText(s2); t0.setText("0"); } }); } public static void main(String[] args) { Frame win = new Rep7D(); win.setSize(500,500); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } }
・ i = 0; ・ ・ i = i * 10 + 1; ・
public void actionPerformed(ActionEvent evt) { if (y == 0) { y = i / 1; } else { y = y / i; } String s2 = Integer.toString(y); x0.setText(s2 + "/"); t0.setText("0"); i = 0;