You are on page 1of 3

ARITMETICA.

java 06/08/2018 17:28

1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 public class ARITMETICA extends JFrame 
5 {
6 public JLabel T1,T2,T_RES;
7 public JTextField N1,N2,RES;
8 public JButton SUMAR,RESTAR,PROD,DIV,INV;
9
10 public ARITMETICA(){//
11 super("ARITMETICA EN JAVA");//Titulo
12 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13 setSize(510,460);
14
15 T1 = new JLabel("PRIMER NUMERO  ");
16 T2 = new JLabel("SEGUNDO NUMERO ");
17 T_RES = new JLabel("RESULTADO");
18
19 N1 = new JTextField(10);
20 N2 = new JTextField(10);
21 RES = new JTextField(10);
22 RES.setEditable(false);//define el texfiel de solo lectura
23
24 SUMAR = new JButton("SUMAR");
25 RESTAR = new JButton("RESTAR");
26 PROD = new JButton("MULTIPLICAR");
27 DIV = new JButton("DIVIDIR");
28 INV =new JButton("INVERTIR");
29 //FlowLayout DISTRIBUIDOR = new FlowLayout();
30 //this.setLayout(DISTRIBUIDOR);
31
32 add(T1);T1.setBounds(50,80,130,30);//permite colocar en el frame los objetos
33 add(N1);N1.setBounds(200,80,100,30);//el setbounds permite identiicar la columna y la fila par
34 add(T2);T2.setBounds(50,150,150,30);//(col,fila,ancho,alto)
35 add(N2);N2.setBounds(200,150,100,30);
36 add(T_RES);T_RES.setBounds(50,250,150,30);
37 add(RES);RES.setBounds(200,250,100,30);
38 add(SUMAR);SUMAR.setBounds(50,300,100,30);
39 add(RESTAR);RESTAR.setBounds(180,300,100,30);
40 add(PROD);PROD.setBounds(300,300,115,30);
41 add(DIV);DIV.setBounds(350,100,115,30);
42 add(INV);INV.setBounds(250,350,115,30); 
43
44
45 SUMAR.addActionListener( new ActionListener(){
46 public void  actionPerformed(ActionEvent EVENTO)
47 {
48 float NUM1,NUM2,T_RESULT;
49 try//controla el codigo a ejecutar
50 {
51 NUM1= Float.parseFloat(N1.getText());
52 NUM2= Float.parseFloat(N2.getText());
53 T_RESULT= NUM1 + NUM2;
54
55 RES.setText(String.valueOf(T_RESULT));
56 T_RES.setText("LA SUMA ES");
57 }
58 catch(Exception E)//muestra el codigo solo si el otro codigo tiene error
59 {
60 T_RES.setText("ERROR AL SUMAR");
61 }
62 }
63 }
64 );
65
66 RESTAR.addActionListener( new ActionListener(){
67 public void  actionPerformed(ActionEvent EVENTO)
68 {

Page 1 of 3
ARITMETICA.java 06/08/2018 17:28

69 float NUM1,NUM2,T_RESULT;
70 try
71 {
72 NUM1= Float.parseFloat(N1.getText());
73 NUM2= Float.parseFloat(N2.getText());
74 T_RESULT= NUM1 ‐ NUM2;
75
76 RES.setText(String.valueOf(T_RESULT));
77 T_RES.setText("LA RESTA ES");
78 }
79 catch(Exception E)
80 {
81 T_RES.setText("ERROR AL RESTAR");
82 }
83 }
84 }
85 );
86 PROD.addActionListener( new ActionListener(){
87 public void  actionPerformed(ActionEvent EVENTO)
88 {
89 float NUM1,NUM2,T_RESULT;
90 try
91 {
92 NUM1= Float.parseFloat(N1.getText());
93 NUM2= Float.parseFloat(N2.getText());
94 T_RESULT= NUM1 * NUM2;
95
96 RES.setText(String.valueOf(T_RESULT));
97 T_RES.setText("EL PRODUCTO ES");
98 }
99 catch(Exception E)
100 {
101 T_RES.setText("ERROR AL RESTAR");
102 }
103 }
104 }
105 );
106 DIV.addActionListener(new ActionListener(){
107 public void actionPerformed(ActionEvent e){
108 float a,b,c;
109 a=Float.parseFloat(N1.getText());
110 b=Float.parseFloat(N2.getText());
111 c=a/b;
112 RES.setText(""+c);
113 T_RES.setText("LA DIVISION ES = ");
114 }
115 }
116 );
117 INV.addActionListener(new ActionListener(){
118 public void actionPerformed(ActionEvent e){
119 String cad,res="";
120 cad=N1.getText();
121 for(int i=cad.length();i>=0;i‐‐){
122 res=res+cad.charAt(i);
123 }
124 RES.setText(""+res);
125 T_RES.setText("EL INVERTIDO ES");
126 }
127 }
128
129 );
130 }
131 public static void main(String[] ARGUMENTOS)
132 {
133 ARITMETICA MI_INTERFAZ = new ARITMETICA();
134 //MI_INTERFAZ.setResizable(true);
135 MI_INTERFAZ.setLayout(null);//el set layout null habilita el posecionar los objets el frame (s
136 MI_INTERFAZ.setVisible(true);

Page 2 of 3
ARITMETICA.java 06/08/2018 17:28

137 }
138 }
139 //practica  esCRIBIR UNA APLICACION EN MODO GAFICO UE PERMITE INGRESAR UN NUMERO Y QUE REALIZE LAS SIG
140 //diGITOS PARES E IMPARES, SUMAR LOS DIGITOS,MULTIPICAR LOS DIGITOS SIN CONSIDERAR EL CERO, 
141 //ESCRIBRI OTRA APLICACION PARA FIBO,1 2 4 7 11 16::1 2 6 24 120,
142 //alMACENAR EN DOS VECTORES NUMEROS ENTEROS Y SUMAR AMBOS VECTORES EN UN TERCER VECTOR Y MOSTRAR LOS R

Page 3 of 3

You might also like