Вы находитесь на странице: 1из 4

import javax.swing.*; import java.awt.*; import java.awt.event.

*; public class Calculadora extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; int primerValor = 0; int segundoValor = 0; char operador = ' '; JPanel contenedor1; JPanel contenedor2; JPanel contenerdorPrincipal; JTextField campo; JButton boton1; JButton boton2; JButton boton3; JButton boton4; JButton boton5; JButton boton6; JButton boton7; JButton boton8; JButton boton9; JButton boton0; JButton botonMas; JButton botonMenos; JButton botonPor; JButton botonEntre; JButton botonAC; JButton botonRaiz; JButton botonPotencia; JButton botonIgual; JButton botonSalir; JButton botonOff; public Calculadora(){ super("Calculardora"); contenerdorPrincipal = new JPanel(new BorderLayout()); contenedor1 = new JPanel(); contenedor1.setLayout(new BorderLayout()); campo = new JTextField(); contenedor1.add(campo, BorderLayout.NORTH); boton1 = new JButton("1"); boton2 = new JButton("2"); boton3 = new JButton("3"); boton4 = new JButton("4"); boton5 = new JButton("5"); boton6 = new JButton("6"); boton7 = new JButton("7"); boton8 = new JButton("8"); boton9 = new JButton("9"); boton0 = new JButton("0"); botonMas = new JButton("+"); botonMenos = new JButton("-"); botonPor = new JButton("*"); botonEntre = new JButton("/");

botonAC = new JButton("C"); botonRaiz = new JButton("R"); botonPotencia = new JButton("^"); botonIgual = new JButton("="); botonOff = new JButton("?"); botonSalir = new JButton("x"); boton1.addActionListener(this); boton2.addActionListener(this); boton3.addActionListener(this); boton4.addActionListener(this); boton5.addActionListener(this); boton6.addActionListener(this); boton7.addActionListener(this); boton8.addActionListener(this); boton9.addActionListener(this); boton0.addActionListener(this); botonMas.addActionListener(this); botonMenos.addActionListener(this); botonPor.addActionListener(this); botonEntre.addActionListener(this); botonRaiz.addActionListener(this); botonIgual.addActionListener(this); botonPotencia.addActionListener(this); botonAC.addActionListener(this); botonSalir.addActionListener(this); botonOff.addActionListener(this); botonIgual.addActionListener(this); contenedor2 = new JPanel(new GridLayout(4,4,2,2)); contenedor2.add(boton1); contenedor2.add(boton2); contenedor2.add(boton3); contenedor2.add(boton4); contenedor2.add(botonMas); contenedor2.add(boton5); contenedor2.add(boton6); contenedor2.add(boton7); contenedor2.add(boton8); contenedor2.add(botonMenos); contenedor2.add(boton9); contenedor2.add(boton0); contenedor2.add(botonPotencia); contenedor2.add(botonRaiz); contenedor2.add(botonEntre); contenedor2.add(botonAC); contenedor2.add(botonOff); contenedor2.add(botonSalir); contenedor2.add(botonIgual); contenedor2.add(botonPor); contenerdorPrincipal.add(contenedor1, BorderLayout.NORTH); contenerdorPrincipal.add(contenedor2, BorderLayout.SOUTH); this.setContentPane(contenerdorPrincipal); this.setSize(300, 170); int x = ((Toolkit.getDefaultToolkit().getScreenSize().width) - 3 00)/2; //calcular el ancho actual de la pantalla en pixeles

int y = ((Toolkit.getDefaultToolkit().getScreenSize().height) 170)/2; //calcular el alto actual de la pantalla en pixeles this.setLocation(x,y); this.setVisible(true); } public void actionPerformed(ActionEvent e) { //************************************************** // CDIGO DE MANEJO DE EVENTOS //************************************************** try { if (e.getSource() == boton1){ campo.setText(campo.getText() + "1"); } else if (e.getSource() == boton2){ campo.setText(campo.getText() + "2"); } else if (e.getSource() == boton3){ campo.setText(campo.getText() + "3"); } else if (e.getSource() == boton4){ campo.setText(campo.getText() + "4"); } else if (e.getSource() == boton5){ campo.setText(campo.getText()+ "5"); } else if (e.getSource() == boton6){ campo.setText(campo.getText() + "6"); } else if (e.getSource() == boton7){ campo.setText(campo.getText() + "7"); } else if (e.getSource() == boton8){ campo.setText(campo.getText() + "8"); } else if (e.getSource() == boton9){ campo.setText(campo.getText() + "9"); } else if (e.getSource() == boton0){ campo.setText(campo.getText() + "0"); } else if (e.getSource() == botonMas){ operador = botonMas.getText().charAt(0); primerValor = new Integer(campo.getText()).intValue(); campo.setText(""); } else if (e.getSource() == botonMenos){ operador = botonMenos.getText().charAt(0); primerValor = new Integer(campo.getText()).intValue(); campo.setText(""); } else if (e.getSource() == botonPor){ operador = botonPor.getText().charAt(0); primerValor = new Integer(campo.getText()).intValue(); campo.setText(""); } else if (e.getSource() == botonEntre){ operador = botonEntre.getText().charAt(0); primerValor = new Integer(campo.getText()).intValue(); campo.setText(""); } else if (e.getSource() == botonRaiz){ operador = botonRaiz.getText().charAt(0); primerValor = new Integer(campo.getText()).intValue(); campo.setText(""); } else if (e.getSource() == botonPotencia){ operador = botonPotencia.getText().charAt(0); primerValor = new Integer(campo.getText()).intValue(); campo.setText(""); } else if (e.getSource() == botonIgual){ double resultado = 0.0; if (operador != 'R') segundoValor = new Integer(campo.getText()).intValue();

switch (operador) { case '+': resultado = primerValor + segundoValor; break; case '-': resultado = primerValor - segundoValor; case '*': resultado = primerValor * segundoValor; break; case '/': resultado = primerValor / segundoValor; break; case 'R': resultado = Math.sqrt(primerValor); break; case '^': resultado = Math.pow(primerValor, segundoValor ); break; default: JOptionPane.showMessageDialog(this, "Opcion Inv alida","ADVERTENCIA",JOptionPane.ERROR_MESSAGE); break; } campo.setText(String.valueOf(resultado)); } else if (e.getSource() == botonOff){ campo.setText(""); } else if (e.getSource() == botonSalir){ dispose(); } } catch (NumberFormatException ex) { } catch (Exception ex) { } } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelC lassName()); new Calculadora(); }catch(Exception e) { e.printStackTrace(); } } }

Вам также может понравиться