Explora Libros electrónicos
Categorías
Explora Audiolibros
Categorías
Explora Revistas
Categorías
Explora Documentos
Categorías
(CUN)
PROYECTO CALCULADORA
NETBEANS 8.2
Proyecto calculadora básica con botón elevado al cuadrado en NetBeans
Realizamos los mismo con las otras funciones únicamente cambiando el signo.
/**
* @author 57321
*/
public Calculadora() {
initComponents();
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
*/
@SuppressWarnings("unchecked")
jButton19.setText("jButton1");
jButton17.setText("jButton1");
jButton20.setText("jButton1");
jButton18.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Pantalla.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
Pantalla.setOpaque(true);
jButton1.setText("C");
jButton1.addActionListener(new java.awt.event.ActionListener() {
jButton1ActionPerformed(evt);
});
jButton2.setText("*");
jButton2.addActionListener(new java.awt.event.ActionListener() {
jButton2ActionPerformed(evt);
});
jButton3.setText("-");
jButton3.addActionListener(new java.awt.event.ActionListener() {
jButton3ActionPerformed(evt);
});
jButton4.setText("/");
jButton4.addActionListener(new java.awt.event.ActionListener() {
jButton4ActionPerformed(evt);
});
jButton5.setText("7");
jButton5.addActionListener(new java.awt.event.ActionListener() {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("8");
jButton6.addActionListener(new java.awt.event.ActionListener() {
jButton6ActionPerformed(evt);
});
jButton7.setText("9");
jButton7.addActionListener(new java.awt.event.ActionListener() {
jButton7ActionPerformed(evt);
});
jButton9.setText("4");
jButton9.addActionListener(new java.awt.event.ActionListener() {
jButton9ActionPerformed(evt);
});
jButton10.setText("5");
jButton10.addActionListener(new java.awt.event.ActionListener() {
jButton10ActionPerformed(evt);
});
jButton11.setText("6");
jButton11.addActionListener(new java.awt.event.ActionListener() {
jButton11ActionPerformed(evt);
}
});
jButton12.setText("+");
jButton12.addActionListener(new java.awt.event.ActionListener() {
jButton12ActionPerformed(evt);
});
jButton13.setText("1");
jButton13.addActionListener(new java.awt.event.ActionListener() {
jButton13ActionPerformed(evt);
});
jButton14.setText("2");
jButton14.addActionListener(new java.awt.event.ActionListener() {
jButton14ActionPerformed(evt);
});
jButton15.setText("3");
jButton15.addActionListener(new java.awt.event.ActionListener() {
jButton15ActionPerformed(evt);
});
jButton21.setText("0");
jButton21.addActionListener(new java.awt.event.ActionListener() {
});
jButton23.setText(".");
jButton23.addActionListener(new java.awt.event.ActionListener() {
jButton23ActionPerformed(evt);
});
jButton24.setText("=");
jButton24.addActionListener(new java.awt.event.ActionListener() {
jButton24ActionPerformed(evt);
});
jButton16.setText("^2");
jButton16.addActionListener(new java.awt.event.ActionListener() {
jButton16ActionPerformed(evt);
});
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 60,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton13, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
);
pack();
}// </editor-fold>
this.Pantalla.setText("");
this.primernumero=Float.parseFloat(this.Pantalla.getText());
this.operador="/";
this.Pantalla.setText("");
this.primernumero=Float.parseFloat(this.Pantalla.getText());
this.operador="*";
this.Pantalla.setText("");
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
this.primernumero=Float.parseFloat(this.Pantalla.getText());
this.operador="+";
this.Pantalla.setText("");
this.Pantalla.setText(this.Pantalla.getText()+"0";
this.Pantalla.setText(this.Pantalla.getText()+"1";
this.Pantalla.setText(this.Pantalla.getText()+"2";
this.Pantalla.setText(this.Pantalla.getText()+"3";
this.Pantalla.setText(this.Pantalla.getText()+"4";
this.Pantalla.setText(this.Pantalla.getText()+"5";
this.Pantalla.setText(this.Pantalla.getText()+"6";
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
this.Pantalla.setText(this.Pantalla.getText()+"7";
this.Pantalla.setText(this.Pantalla.getText()+"8";
this.Pantalla.setText(this.Pantalla.getText()+"9";
this.segundonumero=Float.parseFloat(this.Pantalla.getText());
switch(this.operador){
case "+":this.Pantalla.setText(sincero(this.primernumero+this.segundonumero));break;
case "-":this.Pantalla.setText(sincero(this.primernumero-this.segundonumero));break;
case "*":this.Pantalla.setText(sincero(this.primernumero*this.segundonumero));break;
case "/":this.Pantalla.setText(sincero(this.primernumero/this.segundonumero));break;
case "^2":this.Pantalla.setText(sincero(this.primernumero*primernumero));break;
this.primernumero=Float.parseFloat(this.Pantalla.getText());
this.operador="-";
this.Pantalla.setText("");
if(!this.Pantalla.getText().contains(".")){
this.Pantalla.setText(this.Pantalla.getText()+".";
}
this.primernumero=Float.parseFloat(this.Pantalla.getText());
this.operador="^2";
this.Pantalla.setText("");
String retorno="";
retorno=Float.toString(resultado);
if(resultado%1==0){
retorno=retorno.substring(0, retorno.length()-2);
return retorno;
/**
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new Calculadora().setVisible(true);
});