Práctica java 1
- 1. ISTP: Manuel Seoane Corrales Java Swing
Lic. Bernardo C. Hermitaño Atencio
CALCULADORA CON JAVA EN NETBEANS
public class Calculadora extends javax.swing.JFrame {
//variables
double masmenos;
double primerdouble;
double segundouble;
double totaldouble;
//para los botones
int masClic;
int menosClic;
int divClic;
int multiClic;
int decimalClic;
private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
txtDisplay.setText("");
decimalClic=0;
}
private void btnUnoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnUno.getText());
}
private void btnDosActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+btnDos.getText());
}
private void btnTresActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnTres.getText());
}
private void btnCuatroActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnCuatro.getText());
}
- 2. ISTP: Manuel Seoane Corrales Java Swing
Lic. Bernardo C. Hermitaño Atencio
private void btnCincoActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnCinco.getText());
}
private void btnSeisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnSeis.getText());
}
private void btnSieteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnSiete.getText());
}
private void btnOchoActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnOcho.getText());
}
private void btnNueveActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnNueve.getText());
}
private void btnCeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtDisplay.setText(txtDisplay.getText()+btnCero.getText());
}
private void btnPuntoActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
if(decimalClic==0){
txtDisplay.setText(txtDisplay.getText()+btnPunto.getText());
decimalClic=1;
}
}
private void
btnMasMenosActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
masmenos=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
masmenos=masmenos*(‐1);
txtDisplay.setText(String.valueOf(masmenos));
}
private void btnMasActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
primerdouble=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
txtDisplay.setText("");
masClic=1;
decimalClic=0;
}
- 3. ISTP: Manuel Seoane Corrales Java Swing
Lic. Bernardo C. Hermitaño Atencio
private void btnMenosActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
primerdouble=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
txtDisplay.setText("");
menosClic=1;
decimalClic=0;
}
private void btnMultiplicaActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
primerdouble=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
txtDisplay.setText("");
multiClic=1;
decimalClic=0;
}
private void btnDivideActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
primerdouble=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
txtDisplay.setText("");
divClic=1;
decimalClic=0;
}
private void btnIgualActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
segundouble=(Double.parseDouble(String.valueOf(txtDisplay.getText())));
if(masClic>0){
totaldouble=primerdouble+segundouble;
txtDisplay.setText(String.valueOf(totaldouble));
primerdouble=0;
segundouble=0;
masClic=0;
}
if(menosClic>0){
totaldouble=primerdouble‐segundouble;
txtDisplay.setText(String.valueOf(totaldouble));
primerdouble=0;
segundouble=0;
menosClic=0;
}
if(multiClic>0){
totaldouble=primerdouble*segundouble;
txtDisplay.setText(String.valueOf(totaldouble));
primerdouble=0;
segundouble=0;
multiClic=0;
}
if(divClic>0){
totaldouble=primerdouble/segundouble;
txtDisplay.setText(String.valueOf(totaldouble));
primerdouble=0;
segundouble=0;
divClic=0;
} }