Calculadora en java
¿Me puede mandar alguien el código de una calculadora (da igual lo simple que sea) hecha con el Jbuilder?. GRACIAS
Eu sou do Brasil e gostaria de receber o código de uma calculadora feita em JAVA, por favor se você tiver me mande.
//calculadora.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class calculadora extends JFrame implements ActionListener{
Clipboard clipbd = getToolkit().getSystemClipboard();//portapapeles
Container contenedor;
FlowLayout miFlow;
JMenuBar bar;
JMenu menuEdicion,menuVer,menuAyuda,menulog;
JMenuItem itemcopiar, itempegar, itemestandar, itemcientifica,
itemndigitos,itemtemas,itemacerca,itemcos,itemsen,itemtan,itemarcosen,
itemarcocosen,itemarcotan,itemlogn;
public double n1,n2,res;
public String op1,op2,op3,memoria,msg,clipString;
public int men;
private JTextField texto_pantalla;
private JTextField txtmres;
private JButton btce;
private JButton btmc;
private JButton btmr;
private JButton btms;
private JButton btmult;
private JButton btporc;
private JButton btmmas;
private JButton btsqrt;
private JButton btunox;
private JButton btsumar;
private JButton btmasom;
private JButton btrestar;
private JButton btdividir;
private JButton btlimpiar;
private JButton btretroceso;
private JButton btcoma;
private JButton btigual;
private JButton bt0;
private JButton bt1;
private JButton bt2;
private JButton bt3;
private JButton bt4;
private JButton bt5;
private JButton bt6;
private JButton bt7;
private JButton bt8;
private JButton bt9;
public calculadora(){
Container c=getContentPane();
ActionEventHandler evento =new ActionEventHandler();
c.setLayout(new FlowLayout());
texto_pantalla = new JTextField(25);
texto_pantalla.setEditable( false );
texto_pantalla.setBackground(Color.white);
texto_pantalla.setForeground (Color.black);
texto_pantalla.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
texto_pantalla.setBorder(new javax.swing.border.MatteBorder(new java.awt.Insets(1, 1, 1, 1), new java.awt.Color(0, 0, 255)));
c.add( texto_pantalla );
txtmres=new JTextField(3);
txtmres.setBackground(java.awt.Color.lightGray);
txtmres.setForeground (Color.red);
txtmres.setHorizontalAlignment(javax.swing.JTextField.CENTER);
txtmres.setEditable(false);
c.add(txtmres);
btretroceso=new JButton("Retroceso");
btretroceso.addActionListener( evento );
btretroceso.setForeground (Color.red);
c.add( btretroceso );
btce=new JButton(" CE ");
btce.setForeground (Color.red);
btce.addActionListener( evento );
c.add( btce );
btlimpiar = new JButton(" C ");
btlimpiar.addActionListener( evento );
btlimpiar.setForeground (Color.red);
c.add( btlimpiar);
btmc = new JButton("MC");
btmc.addActionListener( evento );
btmc.setForeground (Color.red);
c.add( btmc);
bt7=new JButton("7");
bt7.addActionListener( evento );
bt7.setForeground (Color.blue);
c.add( bt7 );
bt8=new JButton("8");
bt8.addActionListener( evento );
bt8.setForeground (Color.blue);
c.add( bt8 );
bt9=new JButton("9");
bt9.addActionListener( evento );
bt9.setForeground (Color.blue);
c.add( bt9 );
btdividir=new JButton("/");
btdividir.addActionListener( evento );
btdividir.setForeground (Color.red);
c.add( btdividir );
btsqrt=new JButton("sqrt");
btsqrt.setForeground (Color.blue);
btsqrt.addActionListener( evento );
c.add( btsqrt );
btmr=new JButton("MR");
btmr.setForeground (Color.red);
btmr.addActionListener( evento );
c.add( btmr );
bt4=new JButton("4");
bt4.addActionListener( evento );
bt4.setForeground (Color.blue);
c.add( bt4 );
bt5=new JButton("5");
bt5.addActionListener( evento );
bt5.setForeground (Color.blue);
c.add( bt5 );
bt6=new JButton("6");
bt6.addActionListener( evento );
bt6.setForeground (Color.blue);
c.add( bt6 );
btmult=new JButton("*");
btmult.addActionListener( evento );
btmult.setForeground (Color.red);
c.add( btmult );
btporc=new JButton(" % ");
btporc.addActionListener( evento );
btporc.setForeground (Color.blue);
c.add( btporc );
btms=new JButton("MS");
btms.setForeground (Color.red);
btms.addActionListener( evento );
c.add( btms);
bt1=new JButton("1");
bt1.addActionListener( evento );
bt1.setForeground (Color.blue);
c.add( bt1 );
bt2=new JButton("2");
bt2.addActionListener( evento );
bt2.setForeground (Color.blue);
c.add( bt2 );
bt3=new JButton("3");
bt3.addActionListener( evento );
bt3.setForeground (Color.blue);
c.add( bt3 );
btrestar=new JButton("-");
btrestar.addActionListener( evento );
btrestar.setForeground (Color.red);
c.add( btrestar );
btunox=new JButton(" 1/x ");
btunox.addActionListener( evento );
btunox.setForeground (Color.blue);
c.add( btunox);
btmmas=new JButton("M+");
btmmas.addActionListener( evento );
btmmas.setForeground (Color.red);
c.add( btmmas );
bt0=new JButton("0");
bt0.addActionListener( evento );
bt0.setForeground (Color.blue);
c.add( bt0 );
btmasom=new JButton("+/-");
btmasom.addActionListener( evento );
btmasom.setForeground (Color.blue);
c.add( btmasom );
btcoma=new JButton(",");
btcoma.addActionListener( evento );
btcoma.setForeground (Color.blue);
c.add( btcoma );
btsumar = new JButton("+");
btsumar.addActionListener( evento );
btsumar.setForeground (Color.red);
c.add( btsumar );
btigual = new JButton(" = ");
btigual.addActionListener( evento );
btigual.setForeground (Color.red);
c.add( btigual);
// opcion de munu edicion inicio//
bar=new JMenuBar();
menuEdicion = new JMenu("Edicion");
menuEdicion.setMnemonic(\'E\');
menuEdicion.setForeground (Color.blue);
itemcopiar = new JMenuItem("Copiar");
itempegar = new JMenuItem("Pegar");
menuEdicion.add(itemcopiar);
menuEdicion.add(itempegar);
itemcopiar.setForeground (Color.blue);
itempegar.setForeground (Color.blue);
itemcopiar.addActionListener(this);
itempegar.addActionListener(this);
// opcion de munu edicion fin//
// opcion de munu ver inicio//
menuVer = new JMenu("Ver");
menuVer.setMnemonic(\'V\');
menuVer.setForeground (Color.blue);
itemestandar = new JMenuItem("Estandar");
itemcientifica = new JMenuItem("Cientifica");
itemndigitos = new JMenuItem("Numero de digitos en grupo");
menuVer.add(itemestandar);
menuVer.add(itemcientifica);
menuVer.addSeparator();
menuVer.add(itemndigitos);
itemestandar.setForeground (Color.blue);
itemcientifica.setForeground (Color.blue);
itemndigitos.setForeground (Color.blue);
itemestandar.addActionListener(this);
itemcientifica.addActionListener(this);
itemndigitos.addActionListener(this);
// opcion de munu ver fin//
// opcion de munu ayuda inicio//
bar=new JMenuBar();
menuAyuda = new JMenu("Ayuda");
menuAyuda.setMnemonic(\'A\');
menuAyuda.setForeground (Color.blue);
itemtemas = new JMenuItem("Temas de ayuda");
itemacerca = new JMenuItem("Acerca de Calculadora");
menuAyuda.add(itemtemas);
menuAyuda.addSeparator();
menuAyuda.add(itemacerca);
itemacerca.setForeground (Color.blue);
itemtemas.setForeground (Color.blue);
itemtemas.addActionListener(this);
itemacerca.addActionListener(this);
// opcion de munu ayuda fin//
// opcion de munu funciones logaritmicas inicio//
bar=new JMenuBar();
menulog= new JMenu("Func Log");
menulog.setMnemonic(\'F\');
menulog.setForeground (Color.blue);
itemsen = new JMenuItem("SEN");
itemcos = new JMenuItem("COS");
itemtan = new JMenuItem("TAN");
itemarcosen = new JMenuItem("ARCOSEN");
itemarcocosen = new JMenuItem("ARCOCOSEN");
itemarcotan = new JMenuItem("ARCOTANGENTE");
itemlogn = new JMenuItem("LOGARITMO NTL");
menulog.add(itemsen);
menulog.add(itemcos);
menulog.add(itemtan);
menulog.add(itemarcosen);
menulog.add(itemarcocosen);
menulog.add(itemarcotan);
menulog.add(itemlogn);
itemsen.setForeground (Color.blue);
itemcos.setForeground (Color.blue);
itemtan.setForeground (Color.blue);
itemarcosen.setForeground (Color.blue);
itemarcocosen.setForeground (Color.blue);
itemarcotan.setForeground (Color.blue);
itemlogn.setForeground (Color.blue);
itemsen.addActionListener(this);
itemcos.addActionListener(this);
itemtan.addActionListener(this);
itemarcosen.addActionListener(this);
itemarcocosen.addActionListener(this);
itemarcotan.addActionListener(this);
itemlogn.addActionListener(this);
// opcion de munu funciones logaritmicas fin//
bar.add(menuEdicion);
bar.add(menuVer);
bar.add(menuAyuda);
bar.add(menulog);
setJMenuBar(bar);
contenedor = getContentPane();
miFlow = new FlowLayout();
contenedor.setLayout(miFlow);
}//fin constructor
public static void main(String args[]){
calculadora ventana = new calculadora();
ventana.setTitle("Calculadora");//titulo de la ventana
ventana.setSize( 310, 250); //tamaño de la ventana
ventana.setLocation(350,270);//posicion de la ventana en la pantalla
ventana.setResizable(false);//la ventana no se puede modificar
ventana.setVisible(true);//llamado a la ventana
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// para que se puedad cerrar la ventana
}
private class ActionEventHandler implements ActionListener{ // se define la clase interna para la manipulación de eventos
public void actionPerformed(ActionEvent e){
if( e.getSource() == btcoma ) {
texto_pantalla.setText(texto_pantalla.getText()+".");
}
if( e.getSource() == bt0 ) {
texto_pantalla.setText(texto_pantalla.getText()+"0");
}
if( e.getSource() == bt1 ) {
texto_pantalla.setText(texto_pantalla.getText()+"1");
}
if( e.getSource() == bt2 ) {
texto_pantalla.setText(texto_pantalla.getText()+"2");
}
if( e.getSource() == bt3 ) {
texto_pantalla.setText(texto_pantalla.getText()+"3");
}
if( e.getSource() == bt4 ) {
texto_pantalla.setText(texto_pantalla.getText()+"4");
}
if( e.getSource() == bt5 ) {
texto_pantalla.setText(texto_pantalla.getText()+"5");
}
if( e.getSource() == bt6 ) {
texto_pantalla.setText(texto_pantalla.getText()+"6");
}
if( e.getSource() == bt7 ) {
texto_pantalla.setText(texto_pantalla.getText()+"7");
}
if( e.getSource() == bt8 ) {
texto_pantalla.setText(texto_pantalla.getText()+"8");
}
if( e.getSource() == bt9 ) {
texto_pantalla.setText(texto_pantalla.getText()+"9");
}
if( e.getSource() == btms ) {
if(texto_pantalla.getText().equals("")){
}
else{
memoria=texto_pantalla.getText();
txtmres.setText("M");
}
}
if( e.getSource() == btmr ) {
if(txtmres.getText().equals("")){
}
else{
texto_pantalla.setText(memoria);
}
}
if( e.getSource() == btmc ) {
memoria="";
txtmres.setText("");
}
if( e.getSource() == btsumar ) {
n1=0;
op1=texto_pantalla.getText();
n1=Double.parseDouble(op1);
men=1;
texto_pantalla.setText("");
}
if( e.getSource() == btmult ) {
n1=0;
op1=texto_pantalla.getText();
n1=Double.parseDouble(op1);
men=2;
texto_pantalla.setText("");
}
if( e.getSource() == btdividir) {
n1=0;
op1=texto_pantalla.getText();
n1=Double.parseDouble(op1);
men=3;
texto_pantalla.setText("");
}
if( e.getSource() == btrestar) {
n1=0;
op1=texto_pantalla.getText();
n1=Double.parseDouble(op1);
men=4;
texto_pantalla.setText("");
}
if( e.getSource() == btporc) {
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(n1*n2/100);
texto_pantalla.setText(String.valueOf(res));
}
if( e.getSource() == btunox) {
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(1/n2);
texto_pantalla.setText(String.valueOf(res));
}
if( e.getSource() == btmasom) {
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(n2*-1);
texto_pantalla.setText(String.valueOf(res));
}
if( e.getSource() == btmmas) { //sumar el actual en memoria mas el q que esta en pantalla
if(txtmres.getText().equals("")){//verifica que el texto_pantalla este vacio
memoria=texto_pantalla.getText();
txtmres.setText("M");
}
else{
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
n1=Double.parseDouble(memoria);
res=(n1+n2);
memoria=(String.valueOf(res));
}
}
if( e.getSource() == btsqrt) {
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.sqrt(n2);//utilizacion de la libreria math y el comando sqrt(raiz cuadrada)
texto_pantalla.setText(String.valueOf(res));
}
if( e.getSource() == btretroceso) {
if(texto_pantalla.getText().equals("")){//verifica que el texto_pantalla este vacio
}
else{
StringBuffer cad = new StringBuffer(texto_pantalla.getText());//cracion de una cadena
cad.replace( cad.length()-1, cad.length(),"");//toma el # de caracteres, y el -1 indica que voy a remplazar el ultimo caracter y le asigno vacio
texto_pantalla.setText(String.valueOf(cad));//manda a escribir la cadena luego de la operacion cad.remplace
}
}
if( e.getSource() == btigual ){
switch(men){
case 1:// suma
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(n1+n2);
texto_pantalla.setText(String.valueOf(res));
break;
case 2:// multiplicacion
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(n1*n2);
texto_pantalla.setText(String.valueOf(res));
break;
case 3://divicion
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(n1/n2);
texto_pantalla.setText(String.valueOf(res));
break;
case 4://restar
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res=(n1-n2);
texto_pantalla.setText(String.valueOf(res));
break;
}
}
if( e.getSource() == btce ){
texto_pantalla.setText("");
}
if( e.getSource() == btlimpiar ){
texto_pantalla.setText("");
}
}
}
public void actionPerformed(ActionEvent e){ //eventos en el menu
if( e.getSource() == itemcopiar ){
texto_pantalla.selectAll();//selecionar automaticamente todo el texto
String selection = texto_pantalla.getSelectedText();
StringSelection clipString =new StringSelection(selection);
clipbd.setContents(clipString, clipString);
txtmres.requestFocus();
}
if( e.getSource() == itempegar ){
}
if(e.getSource() == itemtemas){
msg="\n>>PARA REALIZAR UN CÁLCULO SENCILLO<<"+
"\nEscriba el primer número del cálculo."+
"\nHaga clic en + para sumar, en - para restar,"+
"\nen * para multiplicar o en / para dividir."+
"\nEscriba el siguiente número del cálculo. "+
"\nEscriba los restantes operadores y números."+
"\nHaga clic en = "+
"\n"+
"\n___________________________________________________"+
"\n"+
"\n>>PARA TRABAJAR CON NÚMEROS ALMACENADOS EN MEMORIA<<"+
"\nPara almacenar el número mostrado, haga clic en MS."+
"\nPara recuperar un número almacenado, haga clic en MR."+
"\nPara borrar la memoria, haga clic en MC." +
"\nPara sumar el número mostrado al número ya almacenado"+
"\nen memoria, haga clic en M+. Para ver el nuevo número, haga" +
"\nclic en MR.";
JOptionPane.showMessageDialog(null,msg);
}
if( e.getSource() == itemacerca ){
msg="Yokosoft Calculadora"+"\nVersion 5.1(Build.xpsp3.020828-1920:Service Pack 3)" +
"\nCopirigth 1981-2005 Yokosoft Corporation"+
"\n"+
"\n"+
"\nEste producto ha sido concedido bajo licencia, de cuerdo"+
"\ncon los terminos de contrato de licencia de usuario final, a:"+
"\n"+
"\nJhonny Salgado Sierra"+
"\nYokosoft"+
"\n______________________________________"+
"\nMemoria disponible para Windows: 458.224 KB";
JOptionPane.showMessageDialog(null,msg);
}
if(e.getSource() == itemsen){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.sin(n2);
texto_pantalla.setText(String.valueOf(res));
}
if(e.getSource() == itemcos){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.cos(n2);
texto_pantalla.setText(String.valueOf(res));
}
if(e.getSource() == itemtan){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.tan(n2);
texto_pantalla.setText(String.valueOf(res));
}
if(e.getSource() == itemarcosen){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.asin(n2);
texto_pantalla.setText(String.valueOf(res));
}
if(e.getSource() == itemarcocosen){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.acos(n2);
texto_pantalla.setText(String.valueOf(res));
}
if(e.getSource() == itemarcotan){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.log(n2);
texto_pantalla.setText(String.valueOf(res));
}
if(e.getSource() == itemlogn){
op2=texto_pantalla.getText();
n2=Double.parseDouble(op2);
res= Math.tan(n2);
texto_pantalla.setText(String.valueOf(res));
}
} //fin actionperformed
}
hola necesito ayuda en java hacer una calculadora que grafique 2 ecuaciones lineales
si sabes al go escribeme porfa
[email protected]
si sabes al go escribeme porfa
[email protected]
hola quisiera q me ayudaran con el codigo de una calculadora q sume reste divida multiplique y tengas las memorias de resta y suma y el signo de igualda q sea lo mas pronto posible por favor
Tengo un codigo de una calculadora, pero echa en JBuilder, sino creada a solo codigo, te la envio de toda maneras:
public class AplicacionCalculadora {
public static void main(String[] args) {
Ventana v = new Ventana();
}
}
//--------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Botonera extends JPanel{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
static JButton b1;
static JButton b2;
static JButton b3;
static JButton b4;
static JButton b5;
static JButton b6;
static JButton b7;
static JButton b8;
static JButton b9;
static JButton b0;
static JButton sumar;
static JButton restar;
static JButton multiplicar;
static JButton dividir;
static JButton igual;
static JButton borrar;
static ActionListener evento = new Control();
static String numero1;
static String numero2;
static String operacion;
public Botonera() {
super();
this.setLayout(new GridLayout(4,3));
b7 = new JButton("7");
add(b7);
b8 = new JButton("8");
add(b8);
b9 = new JButton("9");
add(b9);
dividir = new JButton("/");
add(dividir);
b4 = new JButton("4");
add(b4);
b5 = new JButton("5");
add(b5);
b6 = new JButton("6");
add(b6);
multiplicar = new JButton("*");
add(multiplicar);
b1 = new JButton("1");
add(b1);
b2 = new JButton("2");
add(b2);
b3 = new JButton("3");
add(b3);
restar = new JButton("-");
add(restar);
b0 = new JButton("0");
add(b0);
borrar = new JButton("AC");
add(borrar);
igual = new JButton("=");
add(igual);
sumar = new JButton("+");
add(sumar);
Control.escuchar();
}
public double suma(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()+Double.valueOf(n2).doubleValue();
return resultado;
}
public double resta(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()-Double.valueOf(n2).doubleValue();
return resultado;
}
public double multiplicacion(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()*Double.valueOf(n2).doubleValue();
return resultado;
}
public double divicion(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()/Double.valueOf(n2).doubleValue();
return resultado;
}
}
//-----------------------------------------------------------------------
import java.awt.event.*;
public class Control extends Botonera implements ActionListener{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent a) {
if(a.getSource()==borrar){
Ventana.display.setText(null);
numero1=null;
numero2=null;
}
if(a.getSource()==b0){
Ventana.display.setText(Ventana.display.getText()+"0");
}
if(a.getSource()==b1){
Ventana.display.setText(Ventana.display.getText()+"1");
}
if(a.getSource()==b2){
Ventana.display.setText(Ventana.display.getText()+"2");
}
if(a.getSource()==b3){
Ventana.display.setText(Ventana.display.getText()+"3");
}
if(a.getSource()==b4){
Ventana.display.setText(Ventana.display.getText()+"4");
}
if(a.getSource()==b5){
Ventana.display.setText(Ventana.display.getText()+"5");
}
if(a.getSource()==b6){
Ventana.display.setText(Ventana.display.getText()+"6");
}
if(a.getSource()==b7){
Ventana.display.setText(Ventana.display.getText()+"7");
}
if(a.getSource()==b8){
Ventana.display.setText(Ventana.display.getText()+"8");
}
if(a.getSource()==b9){
Ventana.display.setText(Ventana.display.getText()+"9");
}
if(a.getSource()==sumar){
numero1 = Ventana.display.getText();
operacion="sumar";
Ventana.display.setText("");
}
if(a.getSource()==restar){
numero1 = Ventana.display.getText();
operacion="restar";
Ventana.display.setText("");
}
if(a.getSource()==multiplicar){
numero1 = Ventana.display.getText();
operacion="multiplicar";
Ventana.display.setText("");
}
if(a.getSource()==dividir){
numero1 = Ventana.display.getText();
operacion="dividir";
Ventana.display.setText("");
}
if(a.getSource()==igual){
try{
numero2 = Ventana.display.getText();
if(operacion=="sumar"){
Ventana.display.setText(""+suma(numero1,numero2));
}
if(operacion=="restar"){
Ventana.display.setText(""+resta(numero1,numero2));
}
if(operacion=="multiplicar"){
Ventana.display.setText(""+multiplicacion(numero1,numero2));
}
if(operacion=="dividir"){
Ventana.display.setText(""+divicion(numero1,numero2));
}
}catch(Exception e){
Ventana.display.setText("");
}
}
}
public static void escuchar(){
b0.addActionListener(evento);
b1.addActionListener(evento);
b2.addActionListener(evento);
b3.addActionListener(evento);
b4.addActionListener(evento);
b5.addActionListener(evento);
b6.addActionListener(evento);
b7.addActionListener(evento);
b8.addActionListener(evento);
b9.addActionListener(evento);
borrar.addActionListener(evento);
igual.addActionListener(evento);
sumar.addActionListener(evento);
restar.addActionListener(evento);
multiplicar.addActionListener(evento);
dividir.addActionListener(evento);
}
}
//-------------------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Ventana extends JFrame{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
private Botonera botonera;
public static JTextField display;
private JLabel l1;
public Ventana() {
super();
this.setSize(250,250);
this.setLocation(200,50);
this.setVisible(true);
this.setTitle("CALCULADORA");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
cerrar();
}
});
Container c = getContentPane();
display = new JTextField();
display.setPreferredSize(new Dimension(30,30));
display.setEditable(false);
display.setBackground(Color.white);
c.add(display,BorderLayout.NORTH);
botonera = new Botonera();
c.add(botonera,BorderLayout.CENTER);
l1 = new JLabel("Creado Por Rolando Duarte");
c.add(l1,BorderLayout.SOUTH);
show();
}
public static void cerrar(){
System.exit(0);
}
}
espero que con esto ahorren tiempo, pero no se acostumbren, la mejor manera de aprender un lenguaje de programacion es programando en el mismo...
Atentamente
rolextreme
public class AplicacionCalculadora {
public static void main(String[] args) {
Ventana v = new Ventana();
}
}
//--------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Botonera extends JPanel{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
static JButton b1;
static JButton b2;
static JButton b3;
static JButton b4;
static JButton b5;
static JButton b6;
static JButton b7;
static JButton b8;
static JButton b9;
static JButton b0;
static JButton sumar;
static JButton restar;
static JButton multiplicar;
static JButton dividir;
static JButton igual;
static JButton borrar;
static ActionListener evento = new Control();
static String numero1;
static String numero2;
static String operacion;
public Botonera() {
super();
this.setLayout(new GridLayout(4,3));
b7 = new JButton("7");
add(b7);
b8 = new JButton("8");
add(b8);
b9 = new JButton("9");
add(b9);
dividir = new JButton("/");
add(dividir);
b4 = new JButton("4");
add(b4);
b5 = new JButton("5");
add(b5);
b6 = new JButton("6");
add(b6);
multiplicar = new JButton("*");
add(multiplicar);
b1 = new JButton("1");
add(b1);
b2 = new JButton("2");
add(b2);
b3 = new JButton("3");
add(b3);
restar = new JButton("-");
add(restar);
b0 = new JButton("0");
add(b0);
borrar = new JButton("AC");
add(borrar);
igual = new JButton("=");
add(igual);
sumar = new JButton("+");
add(sumar);
Control.escuchar();
}
public double suma(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()+Double.valueOf(n2).doubleValue();
return resultado;
}
public double resta(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()-Double.valueOf(n2).doubleValue();
return resultado;
}
public double multiplicacion(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()*Double.valueOf(n2).doubleValue();
return resultado;
}
public double divicion(String n1,String n2){
double resultado;
resultado = Double.valueOf(n1).doubleValue()/Double.valueOf(n2).doubleValue();
return resultado;
}
}
//-----------------------------------------------------------------------
import java.awt.event.*;
public class Control extends Botonera implements ActionListener{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent a) {
if(a.getSource()==borrar){
Ventana.display.setText(null);
numero1=null;
numero2=null;
}
if(a.getSource()==b0){
Ventana.display.setText(Ventana.display.getText()+"0");
}
if(a.getSource()==b1){
Ventana.display.setText(Ventana.display.getText()+"1");
}
if(a.getSource()==b2){
Ventana.display.setText(Ventana.display.getText()+"2");
}
if(a.getSource()==b3){
Ventana.display.setText(Ventana.display.getText()+"3");
}
if(a.getSource()==b4){
Ventana.display.setText(Ventana.display.getText()+"4");
}
if(a.getSource()==b5){
Ventana.display.setText(Ventana.display.getText()+"5");
}
if(a.getSource()==b6){
Ventana.display.setText(Ventana.display.getText()+"6");
}
if(a.getSource()==b7){
Ventana.display.setText(Ventana.display.getText()+"7");
}
if(a.getSource()==b8){
Ventana.display.setText(Ventana.display.getText()+"8");
}
if(a.getSource()==b9){
Ventana.display.setText(Ventana.display.getText()+"9");
}
if(a.getSource()==sumar){
numero1 = Ventana.display.getText();
operacion="sumar";
Ventana.display.setText("");
}
if(a.getSource()==restar){
numero1 = Ventana.display.getText();
operacion="restar";
Ventana.display.setText("");
}
if(a.getSource()==multiplicar){
numero1 = Ventana.display.getText();
operacion="multiplicar";
Ventana.display.setText("");
}
if(a.getSource()==dividir){
numero1 = Ventana.display.getText();
operacion="dividir";
Ventana.display.setText("");
}
if(a.getSource()==igual){
try{
numero2 = Ventana.display.getText();
if(operacion=="sumar"){
Ventana.display.setText(""+suma(numero1,numero2));
}
if(operacion=="restar"){
Ventana.display.setText(""+resta(numero1,numero2));
}
if(operacion=="multiplicar"){
Ventana.display.setText(""+multiplicacion(numero1,numero2));
}
if(operacion=="dividir"){
Ventana.display.setText(""+divicion(numero1,numero2));
}
}catch(Exception e){
Ventana.display.setText("");
}
}
}
public static void escuchar(){
b0.addActionListener(evento);
b1.addActionListener(evento);
b2.addActionListener(evento);
b3.addActionListener(evento);
b4.addActionListener(evento);
b5.addActionListener(evento);
b6.addActionListener(evento);
b7.addActionListener(evento);
b8.addActionListener(evento);
b9.addActionListener(evento);
borrar.addActionListener(evento);
igual.addActionListener(evento);
sumar.addActionListener(evento);
restar.addActionListener(evento);
multiplicar.addActionListener(evento);
dividir.addActionListener(evento);
}
}
//-------------------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Ventana extends JFrame{
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;
private Botonera botonera;
public static JTextField display;
private JLabel l1;
public Ventana() {
super();
this.setSize(250,250);
this.setLocation(200,50);
this.setVisible(true);
this.setTitle("CALCULADORA");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
cerrar();
}
});
Container c = getContentPane();
display = new JTextField();
display.setPreferredSize(new Dimension(30,30));
display.setEditable(false);
display.setBackground(Color.white);
c.add(display,BorderLayout.NORTH);
botonera = new Botonera();
c.add(botonera,BorderLayout.CENTER);
l1 = new JLabel("Creado Por Rolando Duarte");
c.add(l1,BorderLayout.SOUTH);
show();
}
public static void cerrar(){
System.exit(0);
}
}
espero que con esto ahorren tiempo, pero no se acostumbren, la mejor manera de aprender un lenguaje de programacion es programando en el mismo...
Atentamente
rolextreme