[administrator@ib03 ~/Javaworks]% cat GUIaa.java 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, 100, 30); add(x0); x0.setBounds(10, 80, 180, 30); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ int i = Integer.parseInt(t0.getText()); t0.setText(""); if(i % 2 == 0) { x0.setText(i + " is Even"); } else { x0.setText(i + " is Odd"); } }catch(NumberFormatException e){ x0.setText("Error! prease input number"); t0.setText(""); } } }); } public static void main(String[] args) { Frame win = new GUIaa(); win.setSize(220, 150); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
実行結果は次の通り。
|
setText( i + "");
try{ //以下のブロックが例外処理の対象 int i = Integer.parseInt(t0.getText()); t0.setText(""); if(i % 2 == 0) { x0.setText(i + " is Even"); } else { x0.setText(i + " is Odd"); } }catch(NumberFormatException e){ x0.setText("Error! prease input number"); t0.setText(""); } //catch以下より、例外発生時に行う処理の記述このコードでは、catch節が一つしか無いが、例外の種類が複数ある場合は、try以下のブロックに複数のcatch文を続けて記述することが出来ます。そうすることで、1つ目のcatch文で示された種類の例外で無いときは別のcatch文へ、というように、複数の例外時それぞれに別々の処理を行うことが出来ます。
catch(NumberFormatException e)は、NumberformatExceptionクラス型の例外が、try以下のブロックで発生した時に、そのオブジェクトeを生成して、catch以下の処理を行うことを示しています。この例外クラスは全て、Exceptionというクラスのサブクラスなので、
catch(Exception e)とすれば、全ての例外を捕えることができます。
[administrator@ib03 ~/Javaworks]% cat GUId.java import java.awt.*; import java.awt.event.*; public class GUId extends 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]"); Checkbox c0 = new Checkbox("Check"); List l1 = new List(); TextArea t1 = new TextArea("some text ..."); Choice c1 = new Choice(); public GUId() { setLayout(null); 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, 60, 30); add(x0); x0.setBounds(200,30, 180, 30); add(c0); c0.setBounds(200,50 , 60, 30); add(l1); l1.setBounds(200,100 , 80, 100); l1.add("Hello!"); l1.add("How are you?"); add(t1); t1.setBounds(300,300, 80, 100); add(c1); c1.setBounds(300,120 , 60, 30); c1.add("Red"); c1.add("Green"); c1.add("Blue"); b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { 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) { GUId win = new GUId(); win.setSize(400, 450); win.setVisible(true); win.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } } |
実行結果(こんなしょっぱいのですいません..) |
[administrator@ib03 ~/Javaworks]% cat FtoC.java import java.awt.*; import java.awt.event.*; public class FtoC extends Frame { Button b0 = new Button("FtoC"); Button b1 = new Button("CtoF"); Label x0 = new Label("temperature conversion"); Label x1 = new Label(":Input number"); TextField t0 = new TextField(); public FtoC() { setLayout(null); add(t0); t0.setBounds(10, 40, 80, 30); add(x1); x1.setBounds(100,40, 200,20); add(b0); b0.setBounds(10, 80, 60, 30); add(b1); b1.setBounds(100,80, 60,30); b0.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { try{ int c = Integer.parseInt(t0.getText()); int f = (c*9/5) + 32; String s = Integer.toString(f); t0.setText(s); x1.setText("Converted :CtoF"); }catch(NumberFormatException ex1){ x1.setText("Error! Prease input number"); t0.setText(""); } } }); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { try{ int f = Integer.parseInt(t0.getText()); int c = (f-32)*5/9; String s = Integer.toString(c); t0.setText(s); x1.setText("Converted :FtoC"); }catch(NumberFormatException ex2){ x1.setText("Error! Prease input number"); t0.setText(""); } } }); add(x0); x0.setBounds(10, 120, 180, 30); } public static void main(String[] args) { Frame win = new FtoC(); win.setSize(300, 160); win.setVisible(true); win.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent ext){ System.exit(0); } }); } } |
実行結果 |