miércoles, 24 de octubre de 2012

VENTANA DE OPERACION DE POTENCIAS CON JFRAME

  • EJERCICIO
necesito una ventana de operacion de potencias q tenga los siguientes espacios, 1 para la base, otro para el exponente otro para el resultado y tres botones 1 para calcular, otro para limpiar y otro para salir.
  • RESULTADO 01 : CON PURO COD. FUENTE



  • RESULTADO 02 : CON LA INTERFAZ DE NETBEANS 7.2




  • COD. FUENTE DEL RESULTADO 01

/**
 *
 *  E-Mail : shamirdhc31@gmail.com
 *  Blog   : http://javadhc.blogspot.com
 *
 */

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Exponente extends JFrame implements ActionListener
{
    //-- CREAMOS LOS OBJETOS QUE UTLIZAREMOS
    
    public JButton btnCalcular = new JButton("CALCULAR");
    public JButton btnLimpiar = new JButton("LIMPIAR");
    public JButton btnSalir = new JButton("SALIR");
    
    public JLabel lblBase = new JLabel("BASE              : ");
    public JLabel lblExponente = new JLabel("EXPONENTE : ");
    public JLabel lblResultado = new JLabel("RESULTADO : ");
    
    public JTextField txtBase = new JTextField(20);
    public JTextField txtExponente = new JTextField(20);
    public JTextField txtResultado = new JTextField(20);
    
    public JPanel pnlMiPanel = new JPanel();
    
    public Exponente() 
    {
        //-- CONFIGURAMOS LA VENTANA JFRAME
        
        super("POTENCIA");
        this.setSize(400,270);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        
        //-- LE AGREGAMOS EL GESTIONADOR DE EVENTOS
        
        this.btnCalcular.addActionListener(this);    
        this.btnLimpiar.addActionListener(this);
        this.btnSalir.addActionListener(this);
        
        //-- LE AÑADIMOS UN DISTRIBUIDOR DE OBJETOS AL PANEL
        
        FlowLayout DISTRIBUIDOR = new FlowLayout(FlowLayout.LEFT,30,30);
        this.pnlMiPanel.setLayout(DISTRIBUIDOR);
        
        //-- PERZONALIZAMOS LOS OBJETOS
        
        this.pnlMiPanel.setBackground(Color.LIGHT_GRAY);
        
        this.txtResultado.setEditable(false);
        
        //-- LE AGREGAMOS LOS OBJETOS A LA VENTANA
        
        this.pnlMiPanel.add(this.lblBase);
        this.pnlMiPanel.add(this.txtBase);
        this.pnlMiPanel.add(this.lblExponente);
        this.pnlMiPanel.add(this.txtExponente);
        this.pnlMiPanel.add(this.lblResultado);
        this.pnlMiPanel.add(this.txtResultado);
        this.pnlMiPanel.add(this.btnLimpiar);
        this.pnlMiPanel.add(this.btnCalcular);
        this.pnlMiPanel.add(this.btnSalir);
        
        this.add(this.pnlMiPanel);
        
    }
    
    @Override
    public void actionPerformed(ActionEvent AE) 
    {
        //-- CON "getSource()" VEMOS EN QUE BOTON SE HIZO CLICK
        
        if(AE.getSource() == this.btnCalcular)
        {
            try
            {
                double BASE,EXP,RES;
                
                BASE = Double.parseDouble(this.txtBase.getText());
                EXP  = Double.parseDouble(this.txtExponente.getText());
              
                RES = Math.pow(BASE,EXP);
                
                this.txtResultado.setText(String.valueOf(RES));
            }
            catch(Exception E)
            {
                JOptionPane.showMessageDialog(null,"ERROR VUELVA A INGRESAR LOS DATOS");
                this.txtBase.setText("");
                this.txtExponente.setText("");
                this.txtResultado.setText("");
            }
        }
        else if(AE.getSource() == this.btnLimpiar)
        {
            this.txtBase.setText("");
            this.txtExponente.setText("");
            this.txtResultado.setText("");
        }
        else if(AE.getSource() == this.btnSalir)
        {
            System.exit(0);
        }        
    }
    
    public static void main(String[] ARGUMENTOS)
    {
        //-- CREAMOS E INICIALIZAMOS LA VENTANA
        
        Exponente Exp = new Exponente();
        Exp.setVisible(true);
    }
}


DALE CLICK EN LA IMAGEN PARA BAJARTE EL PROYECTO CON LOS 2 EJERCICIOS

IMPORTANTE : "TODOS LOS CODIGOS INDICADOS AQUI SON ESCRITOS POR MI PERSONA, ASI QUE CUALQUIER DUDA O EJERCICIO QUE NO PUEDAN RESOLVER, NO DUDEN EN MANDARME UN E-MAIL A MI CORREO"
shamirdhc31@gmail.com

0 comentarios: