1. ¿Cómo hacer una calculadora en Java y en Visual Basic?
En Java:
Para crear la interfaz de la calculadora vacía.
package jmr.blog;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Ventana extends JFrame{
public Ventana(){
//TITULO
setTitle("Calculadora JMR");
//TAMAÑO
setSize(250,300);
//DIMENSIONABLE O NO
setResizable(false);
//LOCACION
setLocationRelativeTo(null);
//SALIR AL CLICKEAR LA CRUZ
setDefaultCloseOperation(EXIT_ON_CLOSE);
//ICONO DE LA CALCULADORA
setIconImage(new ImageIcon(getClass().getResource("/jmr/blog/res/favicon.png")).getImage());
//METODO PARA CREAR INTERFAZ Y AGREGAR EVENTOS A BOTONES
init();
//HACEMOSVISIBLE LA APP
setVisible(true);
}
privatevoid init() {
//INTERFAZ Y FUNCIONALIDAD
}
public staticvoid main(String args[]){
//CREAMOS UN NUEVO OBJETO VENTANA
new Ventana();
}
}
Quedará así:
2. Ahora para agregar el diseño, paneles y layouts.
Este código es para agregar una caja de texto:
JPanel panel_principal;
JPanel panel_botones;
JTextField caja;
Para agregar el panel de botones:
privatevoid init() {
//INTERFAZ Y FUNCIONALIDAD
//CREAMOS PANELPRINCIPALCON LAYOUT BORDERLAYOUT
panel_principal = new JPanel();
panel_principal.setLayout(new BorderLayout());
//EN EL NORTEIRA LA CAJA DE TEXTO
caja = new JTextField();
panel_principal.add("North",caja);
//EN EL CENTRO IRA EL PANELDE BOTONES
panel_botones = new JPanel();
//El GRIDLAYOUT RECIBE COMO PARAMETROS:
//FILAS,COLUMNASESPACIADO ENTREFILAS,
//ESPACIADO ENTRE COLUMNAS
panel_botones.setLayout(new GridLayout(5,4,8,8));
//agregarBotones();
panel_principal.add("Center",panel_botones);
//AGREGAMOSTODO EL CONTENIDO QUEACABAMOSDEHACER EN
//PANEL_PRINCIPAL A EL PANELDEL FORMULARIO
getContentPane().add(panel_principal);
}
Quedará así:
Para agregar botones en el panel de botones:
JButton boton[];
double op1=0,op2=0;
String operacion="";
boolean nueva=true;
6. caja.setText(String.valueOf(res));
op1=op2=0;
operacion="";
}
nueva=true;
}
});
//CE
boton[0].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
caja.setText("");
op1=op2=0;
operacion="";
}
});
}
En Visual Basic:
Hay que crear una caja de texto, una matriz de diez botes de comando para
número, otra matriz de cuatro botones de comando para (+,-,/,*), un comando para
nueva operación y un comando para el resultado.
Private SubIgual_Click()
Select Case signo 'la variable signo te dice si sumas(0) si restas(1)......
Case 0
Text1.Text = suma(anterior,Val(Text1.Text))'llamada a la función suma
Case 1
Text1.Text = resta(anterior, Val(Text1.Text))
Case 2
Text1.Text = multiplicar(anterior, Val(Text1.Text))
Case 3
Text1.Text = Dividir(anterior, Val(Text1.Text))
EndSelect
EndSub
Private SubNueva_Click()
Text1.Text = ""
EndSub
Private SubOperador_Click(Index As Integer)
signo = Index 'si index es 0 sumas, si es 1 restas......
anterior= Val(Text1.Text)
Text1.Text = ""
EndSub
Private SubNumero_Click(IndexAs Integer)
Text1.Text = Text1.Text + Numero(Index).Caption
EndSub
Private Functionsuma(Numero As Integer, Operador As Integer) As Integer
suma = Numero + Operador
EndFunction
7. Private Functionresta(NumeroAs Integer,OperadorAs Integer)As Integer
resta = Numero - Operador
EndFunction
Private Functionmultiplicar(NumeroAs Integer, Operador As Integer) As Integer
multiplicar = Numero* Operador
EndFunction
Private FunctionDividir(NumeroAs Integer, Operador As Integer) As Integer
Dividir = Numero / Operador
EndFunction
Private SubSalir_Click()
UnloadMe
EndSub