文档库 最新最全的文档下载
当前位置:文档库 › java netbeans计算器

java netbeans计算器

public class caltulor extends javax.swing.JFrame {

/** Creates new form caltulor */
public caltulor() {
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 regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {

ResultTxt = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
btn7 = new javax.swing.JButton();
btn8 = new javax.swing.JButton();
btn9 = new javax.swing.JButton();
btn4 = new javax.swing.JButton();
btn5 = new javax.swing.JButton();
btn6 = new javax.swing.JButton();
btn1 = new javax.swing.JButton();
btn2 = new javax.swing.JButton();
btn3 = new javax.swing.JButton();
btn0 = new javax.swing.JButton();
btnFloat = new javax.swing.JButton();
btnEqul = new javax.swing.JButton();
btnChu = new javax.swing.JButton();
btnCheng = new javax.swing.JButton();
btnJian = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

ResultTxt.setEditable(false);
ResultTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
ResultTxt.setText("0");
ResultTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResultTxtActionPerformed(evt);
}
});

jButton1.setText("Backspace");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("CE");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("C");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

btn7.setText("7");
btn7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn7ActionPerformed(evt);
}
});

btn8.setText("8");
btn8.addActionListener(new java.awt.even

t.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn8ActionPerformed(evt);
}
});

btn9.setText("9");
btn9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn9ActionPerformed(evt);
}
});

btn4.setText("4");
btn4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn4ActionPerformed(evt);
}
});

btn5.setText("5");
btn5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn5ActionPerformed(evt);
}
});

btn6.setText("6");
btn6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn6ActionPerformed(evt);
}
});

btn1.setText("1");
btn1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn1ActionPerformed(evt);
}
});

btn2.setText("2");
btn2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn2ActionPerformed(evt);
}
});

btn3.setText("3");
btn3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn3ActionPerformed(evt);
}
});

btn0.setText("0");
btn0.setToolTipText("");
btn0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn0ActionPerformed(evt);
}
});

btnFloat.setText(".");
btnFloat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFloatActionPerformed(evt);
}
});

btnEqul.setText("=");
btnEqul.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEqulActionPerformed(evt);
}
});

btnChu.setText("/");
btnChu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnChuActionPerformed(evt);
}
});

btnCheng.setText("*");
btnCheng.addActionListe

ner(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnChengActionPerformed(evt);
}
});

btnJian.setText("_");
btnJian.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnJianActionPerformed(evt);
}
});

btnAdd.setText("+");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ResultTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btn4, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(btn7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
.addComponent(btn1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(btn0, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnFloat, 0, 0, Short.MAX_VALUE)
.addComponent(btn2, 0, 0, Short.MAX_VALUE)
.addComponent(btn5, 0, 0, Short.MAX_VALUE)
.addComponent(btn8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnEqul, 0, 0, Short.MAX_VALUE)

.addComponent(btn3, 0, 0, Short.MAX_VALUE)
.addComponent(btn6, 0, 0, Short.MAX_VALUE)
.addComponent(btn9, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAdd, 0, 0, Short.MAX_VALUE)
.addComponent(btnJian, 0, 0, Short.MAX_VALUE)
.addComponent(btnCheng, 0, 0, Short.MAX_VALUE)
.addComponent(btnChu, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))))
.addGap(23, 23, 23))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(ResultTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn7)
.addComponent(btn8)
.addComponent(btn9)
.addComponent(btnChu))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn4)
.addComponent(btn5)
.addComponent(btn6)
.addComponent(btnCheng))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn1)
.addComponent(btn2)
.addComponent(btn3)
.addComponent(btnJian))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn0)
.addComponent(btnFloat)
.addComponent(btnEqul)
.addComponent(btnAdd))
.addContainerGap(26, Short.MAX_VALUE))
);

pack();
}// //GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
ResultStr=ResultStr.substring(0, ResultStr.length()-1);
ResultTxt.setText(ResultStr);
}//GEN-LA

ST:event_jButton1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
b=0;
ResultStr="";
ResultTxt.setText("0");

}//GEN-LAST:event_jButton3ActionPerformed

private void ResultTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResultTxtActionPerformed
// TODO add your handling code here:

// TODO add your handling code here:
}//GEN-LAST:event_ResultTxtActionPerformed
public double a;
public double b;
public int c;
private String ResultStr=new String();
public void mynum(String x)
{
ResultStr= ResultStr+x;
ResultTxt.setText(ResultStr);
}
public void zh()
{ b=Double.parseDouble(ResultStr);
ResultTxt.setText("0");
ResultStr="";
}
public void cal(int y)
{
switch(y)
{
case 1:
a=b+Double.parseDouble(ResultStr);
ResultTxt.setText(String.valueOf(a));
break;
case 2:
a=b-Double.parseDouble(ResultStr);
ResultTxt.setText(String.valueOf(a));
break;
case 3:
a=b/Double.parseDouble(ResultStr);
ResultTxt.setText(String.valueOf(a));
break;
case 4:
a=b*Double.parseDouble(ResultStr);
ResultTxt.setText(String.valueOf(a));
break;
}

ResultStr=String.valueOf(a);
}
private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn0ActionPerformed
if(Double.parseDouble(ResultStr)!=0)
{mynum("0");}
else
{mynum("");}// TODO add your handling code here:
}//GEN-LAST:event_btn0ActionPerformed

private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn7ActionPerformed
// TODO add your handling code here:
mynum("7");
}//GEN-LAST:event_btn7ActionPerformed

private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn8ActionPerformed
mynum("8");
}//GEN-LAST:event_btn8ActionPerformed

private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn9ActionPerformed
mynum("9");// TODO add your handling code here:
}//GEN-LAST:event_btn9ActionPerformed

private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn4ActionPerformed
mynum("4");// TODO add your handling code here:
}//GEN-LAST:event_btn4ActionPerformed

private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn5ActionPerformed
mynum("5");// TODO add your handling code here:
}//GEN-LAST:event_btn5ActionPerformed

private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn6ActionPerformed
mynum("6");// TODO add your handling code here:
}//GEN-LAST:event_btn6ActionPerformed

private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn1ActionPerformed
mynum("1");// TODO add your handling code here

:
}//GEN-LAST:event_btn1ActionPerformed

private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn2ActionPerformed
mynum("2");// TODO add your handling code here:
}//GEN-LAST:event_btn2ActionPerformed

private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn3ActionPerformed
mynum("3");// TODO add your handling code here:
}//GEN-LAST:event_btn3ActionPerformed

private void btnFloatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFloatActionPerformed
if(ResultStr.trim().indexOf(".")==-1)
{mynum(".");}
// TODO add your handling code here:
}//GEN-LAST:event_btnFloatActionPerformed

private void btnEqulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEqulActionPerformed
cal(c);
}//GEN-LAST:event_btnEqulActionPerformed

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
zh();
c=1;// TODO add your handling code here:
}//GEN-LAST:event_btnAddActionPerformed

private void btnChuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChuActionPerformed
zh();
c=3;// TODO add your handling code here:
}//GEN-LAST:event_btnChuActionPerformed

private void btnChengActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChengActionPerformed
zh();
c=4;// TODO add your handling code here:

}//GEN-LAST:event_btnChengActionPerformed

private void btnJianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnJianActionPerformed
zh();
c=2;// TODO add your handling code here:
}//GEN-LAST:event_btnJianActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
ResultStr="";
ResultTxt.setText("0"); // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see https://www.wendangku.net/doc/8214065931.html,/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(caltulor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util

.logging.Logger.getLogger(caltulor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(caltulor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(caltulor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new caltulor().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField ResultTxt;
private javax.swing.JButton btn0;
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btn4;
private javax.swing.JButton btn5;
private javax.swing.JButton btn6;
private javax.swing.JButton btn7;
private javax.swing.JButton btn8;
private javax.swing.JButton btn9;
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnCheng;
private javax.swing.JButton btnChu;
private javax.swing.JButton btnEqul;
private javax.swing.JButton btnFloat;
private javax.swing.JButton btnJian;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration//GEN-END:variables
}

相关文档
相关文档 最新文档