Professional Documents
Culture Documents
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ARITMETICA extends JFrame
{
public JLabel T1,T2,T_RES;
public JTextField N1,N2,RES;
public JButton SUMAR,RESTAR,PROD,DIV,INV;
public ARITMETICA(){//
super("ARITMETICA EN JAVA");//Titulo
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(510,460);
T1 = new JLabel("PRIMER NUMERO ");
T2 = new JLabel("SEGUNDO NUMERO ");
T_RES = new JLabel("RESULTADO");
N1 = new JTextField(10);
N2 = new JTextField(10);
RES = new JTextField(10);
RES.setEditable(false);//define el texfiel de solo lectura
SUMAR = new JButton("SUMAR");
RESTAR = new JButton("RESTAR");
PROD = new JButton("MULTIPLICAR");
DIV = new JButton("DIVIDIR");
INV =new JButton("INVERTIR");
//FlowLayout DISTRIBUIDOR = new FlowLayout();
//this.setLayout(DISTRIBUIDOR);
add(T1);T1.setBounds(50,80,130,30);//permite colocar en el frame los
objetos
add(N1);N1.setBounds(200,80,100,30);//el setbounds permite identiicar
la columna y la fila para ubicar el objeto en el lugar que uno desea
add(T2);T2.setBounds(50,150,150,30);//(col,fila,ancho,alto)
add(N2);N2.setBounds(200,150,100,30);
add(T_RES);T_RES.setBounds(50,250,150,30);
add(RES);RES.setBounds(200,250,100,30);
add(SUMAR);SUMAR.setBounds(50,300,100,30);
add(RESTAR);RESTAR.setBounds(180,300,100,30);
add(PROD);PROD.setBounds(300,300,115,30);
add(DIV);DIV.setBounds(350,100,115,30);
add(INV);INV.setBounds(250,350,115,30);
SUMAR.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent EVENTO)
{
float NUM1,NUM2,T_RESULT;
try//controla el codigo a ejecutar
{
Página 1
ARITMETICA
NUM1= Float.parseFloat(N1.getText());
NUM2= Float.parseFloat(N2.getText());
T_RESULT= NUM1 + NUM2;
RES.setText(String.valueOf(T_RESULT));
T_RES.setText("LA SUMA ES");
}
catch(Exception E)//muestra el codigo solo si el otro codigo tiene
error
{
T_RES.setText("ERROR AL SUMAR");
}
}
}
);
RESTAR.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent EVENTO)
{
float NUM1,NUM2,T_RESULT;
try
{
NUM1= Float.parseFloat(N1.getText());
NUM2= Float.parseFloat(N2.getText());
T_RESULT= NUM1 ‐ NUM2;
RES.setText(String.valueOf(T_RESULT));
T_RES.setText("LA RESTA ES");
}
catch(Exception E)
{
T_RES.setText("ERROR AL RESTAR");
}
}
}
);
PROD.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent EVENTO)
{
float NUM1,NUM2,T_RESULT;
try
{
NUM1= Float.parseFloat(N1.getText());
NUM2= Float.parseFloat(N2.getText());
T_RESULT= NUM1 * NUM2;
RES.setText(String.valueOf(T_RESULT));
T_RES.setText("EL PRODUCTO ES");
}
catch(Exception E)
{
T_RES.setText("ERROR AL RESTAR");
Página 2
ARITMETICA
}
}
}
);
DIV.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
float a,b,c;
a=Float.parseFloat(N1.getText());
b=Float.parseFloat(N2.getText());
c=a/b;
RES.setText(""+c);
T_RES.setText("LA DIVISION ES = ");
}
}
);
INV.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String cad,res="";
cad=N1.getText();
for(int i=cad.length();i>=0;i‐‐){
res=res+cad.charAt(i);
}
RES.setText(""+res);
T_RES.setText("EL INVERTIDO ES");
}
}
);
}
public static void main(String[] ARGUMENTOS)
{
ARITMETICA MI_INTERFAZ = new ARITMETICA();
//MI_INTERFAZ.setResizable(true);
MI_INTERFAZ.setLayout(null);//el set layout null habilita el posecionar
los objets el frame (setbounds)
MI_INTERFAZ.setVisible(true);
}
}
//practica esCRIBIR UNA APLICACION EN MODO GAFICO UE PERMITE INGRESAR UN
NUMERO Y QUE REALIZE LAS SIGUIENTES ACCIONES INVERTIR UN NUMERO ,CONTAR DIGITOS
//diGITOS PARES E IMPARES, SUMAR LOS DIGITOS,MULTIPICAR LOS DIGITOS SIN
CONSIDERAR EL CERO,
//ESCRIBRI OTRA APLICACION PARA FIBO,1 2 4 7 11 16::1 2 6 24 120,
//alMACENAR EN DOS VECTORES NUMEROS ENTEROS Y SUMAR AMBOS VECTORES EN UN TERCER
VECTOR Y MOSTRAR LOS RESOLTADOS.
Página 3