1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124 | import javax.swing.*;import java.awt.event.*; // agregamos esto tambien para el boton.public class Operar extends JFrame implements ActionListener { private JTextField operando1, operando2, resultado; private JButton restar, sumar, multiplicar, dividir, cerrar, acerca; private JLabel titulo, texto1, texto2, result; public Operar(){ setLayout(null); //text fields operando1 = new JTextField(); operando1.setBounds(100, 100, 100, 20); add(operando1); operando2 = new JTextField(); operando2.setBounds(100, 130, 100, 20); add(operando2); resultado = new JTextField(); resultado.setBounds(100, 160, 100, 20); add(resultado); //buttons restar = new JButton("Restar"); restar.setBounds(220, 100, 100, 50); add(restar); restar.addActionListener(this); sumar = new JButton("Sumar"); sumar.setBounds(220, 160, 100, 50); add(sumar); sumar.addActionListener(this); multiplicar = new JButton("Multiplicar"); multiplicar.setBounds(220, 220, 100, 50); add(multiplicar); multiplicar.addActionListener(this); dividir = new JButton("Dividir"); dividir.setBounds(220, 280, 100, 50); add(dividir); dividir.addActionListener(this); cerrar = new JButton("Salir"); cerrar.setBounds(100, 200, 100, 50); add(cerrar); cerrar.addActionListener(this); acerca = new JButton("About"); acerca.setBounds(100, 260, 100, 50); add(acerca); acerca.addActionListener(this); //labels titulo = new JLabel("Calculadora francves v1.0"); titulo.setBounds(130, 40, 200, 30); add(titulo); texto1 = new JLabel("Primer Valor: "); texto1.setBounds(10, 95, 200, 30); add(texto1); texto2 = new JLabel("Segundo Valor: "); texto2.setBounds(10, 125, 200, 30); add(texto2); result = new JLabel("Resultado: "); result.setBounds(10, 155, 200, 30); add(result); } public void actionPerformed(ActionEvent e) { if(e.getSource() == restar){ String oper1=operando1.getText(); String oper2=operando2.getText(); int num1=Integer.parseInt(oper1); int num2=Integer.parseInt(oper2); int resul=num1-num2; String total=String.valueOf(resul); resultado.setText(total); } if(e.getSource() == sumar){ String oper1=operando1.getText(); String oper2=operando2.getText(); int num1=Integer.parseInt(oper1); int num2=Integer.parseInt(oper2); int resul=num1+num2; String total=String.valueOf(resul); resultado.setText(total); } if(e.getSource() == multiplicar){ String oper1=operando1.getText(); String oper2=operando2.getText(); int num1=Integer.parseInt(oper1); int num2=Integer.parseInt(oper2); int resul=num1*num2; String total=String.valueOf(resul); resultado.setText(total); } if(e.getSource() == dividir){ String oper1=operando1.getText(); String oper2=operando2.getText(); int num1=Integer.parseInt(oper1); int num2=Integer.parseInt(oper2); int resul=num1/num2; String total=String.valueOf(resul); resultado.setText(total); } if(e.getSource() == cerrar){ System.exit(0); } if(e.getSource() == acerca){ JOptionPane.showMessageDialog(null, "Calculadora francves v1.0 \n Sigueme en twitter @francves"); } } public static void main(String[] ar){ Operar ope = new Operar(); ope.setBounds(10, 10, 400, 400); ope.setResizable(false); ope.setVisible(true); }} |