martes, 25 de octubre de 2011

Eventos y excepciones.

Estas son las posibles excepciones que se generan en mi proyecto:


import java.io.*;

public class Excepcion {
public static void main(String args[]) throws IOException  {

try {

  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

  int n1, n2, n;
  System.out.print("Da un numero: ");
  n1 = Integer.parseInt(in.readLine());
  System.out.print("Da otro numero: ");
  n2 = Integer.parseInt(in.readLine());
  System.out.print("La division = " + n1/n2);
 }

 catch (NumberFormatException nfe) {
  System.out.println("Numero invalido " + nfe.toString());
 }
 catch (ArithmeticException ae) {
  System.out.println("Division invalida " + ae.toString());
 }
}
}
Aquí se muestra como funciona:

Con respecto a los eventos tenemos los siguientes:
1. Cuando oprimimos el botón cerrar.



addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent e)
            {
               Calc.this.dispose();
               System.runFinalization();
               System.gc();
               System.exit(0);
            }
      });

2. Cuando oprimimos cualquier botón.



       jb_one.addActionListener(Calc.this);
       jb_two.addActionListener(Calc.this);
       jb_three.addActionListener(Calc.this);
       jb_four.addActionListener(Calc.this);
       jb_five.addActionListener(Calc.this);
       jb_six.addActionListener(Calc.this);
       jb_seven.addActionListener(Calc.this);
       jb_eight.addActionListener(Calc.this);
       jb_nine.addActionListener(Calc.this);
       jb_zero.addActionListener(Calc.this);
       jb_plus.addActionListener(Calc.this);
       jb_minus.addActionListener(Calc.this);
       jb_divide.addActionListener(Calc.this);
       jb_multiply.addActionListener(Calc.this);
       jb_sin.addActionListener(Calc.this);
       jb_cos.addActionListener(Calc.this);
       jb_tan.addActionListener(Calc.this);
       jb_asin.addActionListener(Calc.this);
       jb_acos.addActionListener(Calc.this);
       jb_atan.addActionListener(Calc.this);
       jb_pie.addActionListener(Calc.this);
       jb_E.addActionListener(Calc.this);
       jb_decimalpoint.addActionListener(Calc.this);
       jb_equalto.addActionListener(Calc.this);
       jb_fact.addActionListener(Calc.this);
       jb_power.addActionListener(Calc.this);
       jb_changesign.addActionListener(Calc.this);
       jb_reciporcal.addActionListener(Calc.this);
       jb_todeg.addActionListener(Calc.this);
       jb_torad.addActionListener(Calc.this);
       jb_round.addActionListener(Calc.this);
       jb_CA.addActionListener(Calc.this);
       jb_CE.addActionListener(Calc.this);

1 comentario: