Created
February 20, 2015 16:18
-
-
Save TheBeachMaster/0dda72347d55b84a1dc2 to your computer and use it in GitHub Desktop.
A Simple Java Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package src.javaswingcalc1; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JFrame; | |
/** | |
* | |
* @author Kennedy Otieno | |
* | |
*/ | |
public final class CalcUI extends javax.swing.JFrame implements ActionListener { | |
static double num1,num2,ans; | |
static double CgSign,Inverse,inv; | |
static int add=0,subtract=0,multiply=0,divide=0,percentage=0; | |
/** | |
* Creates new form CalcUI | |
*/ | |
public CalcUI() { | |
/* | |
This stuff down here set up those necessary Close operations,Title | |
blah blah blah!! | |
*/ | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setTitle("Arthur's Great Calc ☺"); | |
setResizable(false); | |
setLocationRelativeTo(null); | |
initComponents(); | |
this.initial(); | |
} | |
/** | |
* 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") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
private void initComponents() { | |
jTextFieldAnswer = new javax.swing.JTextField(); | |
jButtonClear = new javax.swing.JButton(); | |
jButtonMultiply = new javax.swing.JButton(); | |
jButtonDivide = new javax.swing.JButton(); | |
jButtonEqual = new javax.swing.JButton(); | |
jButtonNine = new javax.swing.JButton(); | |
jButtonEight = new javax.swing.JButton(); | |
jButtonSeven = new javax.swing.JButton(); | |
jButtonZero = new javax.swing.JButton(); | |
jButtonOne = new javax.swing.JButton(); | |
jButtonFour = new javax.swing.JButton(); | |
jButtonFive = new javax.swing.JButton(); | |
jButtonSix = new javax.swing.JButton(); | |
jButtonTwo = new javax.swing.JButton(); | |
jButtonThree = new javax.swing.JButton(); | |
jButtonCgSign = new javax.swing.JButton(); | |
jButtonPercentage = new javax.swing.JButton(); | |
jButtonDecimal = new javax.swing.JButton(); | |
jButtonInverse = new javax.swing.JButton(); | |
jButtonPlus = new javax.swing.JButton(); | |
jButtonMinus = new javax.swing.JButton(); | |
jButtonSine = new javax.swing.JButton(); | |
jButtonTangent = new javax.swing.JButton(); | |
jButtonCosine = new javax.swing.JButton(); | |
jButtonRoot = new javax.swing.JButton(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
jTextFieldAnswer.setEditable(false); | |
jTextFieldAnswer.setFont(new java.awt.Font("Lucida Sans", 1, 18)); // NOI18N | |
jTextFieldAnswer.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jTextFieldAnswerActionPerformed(evt); | |
} | |
}); | |
jButtonClear.setText("Clear"); | |
jButtonClear.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonClearActionPerformed(evt); | |
} | |
}); | |
jButtonMultiply.setText("*"); | |
jButtonMultiply.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonMultiplyActionPerformed(evt); | |
} | |
}); | |
jButtonDivide.setText("/"); | |
jButtonDivide.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonDivideActionPerformed(evt); | |
} | |
}); | |
jButtonEqual.setText("="); | |
jButtonEqual.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonEqualActionPerformed(evt); | |
} | |
}); | |
jButtonNine.setText("9"); | |
jButtonNine.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonNineActionPerformed(evt); | |
} | |
}); | |
jButtonEight.setText("8"); | |
jButtonEight.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonEightActionPerformed(evt); | |
} | |
}); | |
jButtonSeven.setText("7"); | |
jButtonSeven.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonSevenActionPerformed(evt); | |
} | |
}); | |
jButtonZero.setText("0"); | |
jButtonZero.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonZeroActionPerformed(evt); | |
} | |
}); | |
jButtonOne.setText("1"); | |
jButtonOne.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonOneActionPerformed(evt); | |
} | |
}); | |
jButtonFour.setText("4"); | |
jButtonFour.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonFourActionPerformed(evt); | |
} | |
}); | |
jButtonFive.setText("5"); | |
jButtonFive.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonFiveActionPerformed(evt); | |
} | |
}); | |
jButtonSix.setText("6"); | |
jButtonSix.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonSixActionPerformed(evt); | |
} | |
}); | |
jButtonTwo.setText("2"); | |
jButtonTwo.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonTwoActionPerformed(evt); | |
} | |
}); | |
jButtonThree.setText("3"); | |
jButtonThree.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonThreeActionPerformed(evt); | |
} | |
}); | |
jButtonCgSign.setText("+-"); | |
jButtonCgSign.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonCgSignActionPerformed(evt); | |
} | |
}); | |
jButtonPercentage.setText("%"); | |
jButtonPercentage.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonPercentageActionPerformed(evt); | |
} | |
}); | |
jButtonDecimal.setText("."); | |
jButtonDecimal.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonDecimalActionPerformed(evt); | |
} | |
}); | |
jButtonInverse.setText("1/x"); | |
jButtonInverse.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonInverseActionPerformed(evt); | |
} | |
}); | |
jButtonPlus.setText("+"); | |
jButtonPlus.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonPlusActionPerformed(evt); | |
} | |
}); | |
jButtonMinus.setText("-"); | |
jButtonMinus.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonMinusActionPerformed(evt); | |
} | |
}); | |
jButtonSine.setText("sine"); | |
jButtonSine.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonSineActionPerformed(evt); | |
} | |
}); | |
jButtonTangent.setText("tan"); | |
jButtonTangent.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonTangentActionPerformed(evt); | |
} | |
}); | |
jButtonCosine.setText("cos"); | |
jButtonCosine.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonCosineActionPerformed(evt); | |
} | |
}); | |
jButtonRoot.setText("root"); | |
jButtonRoot.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButtonRootActionPerformed(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(jTextFieldAnswer) | |
.addGroup(layout.createSequentialGroup() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addComponent(jButtonSix, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jButtonTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jButtonFive, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) | |
.addGap(7, 7, 7)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addComponent(jButtonNine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonEight, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGap(7, 7, 7)) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jButtonThree, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addGap(144, 144, 144)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) | |
.addComponent(jButtonSine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonClear, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE) | |
.addComponent(jButtonCgSign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(jButtonRoot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addGap(11, 11, 11) | |
.addComponent(jButtonTangent, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addComponent(jButtonMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addComponent(jButtonPercentage, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addComponent(jButtonDecimal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))) | |
.addGap(7, 7, 7))) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jButtonZero, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(4, 4, 4) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jButtonEqual, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jButtonInverse, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jButtonPlus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jButtonMinus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) | |
.addComponent(jButtonCosine, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))))) | |
.addContainerGap()) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addComponent(jTextFieldAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(jButtonClear) | |
.addComponent(jButtonMultiply) | |
.addComponent(jButtonDivide) | |
.addComponent(jButtonEqual)) | |
.addGap(18, 18, 18) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) | |
.addComponent(jButtonTangent, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE) | |
.addComponent(jButtonRoot, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonSine, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonCosine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addGap(18, 18, Short.MAX_VALUE) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) | |
.addComponent(jButtonPercentage, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE) | |
.addComponent(jButtonDecimal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonInverse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonCgSign, javax.swing.GroupLayout.Alignment.LEADING, 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(jButtonEight, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE) | |
.addComponent(jButtonNine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonSeven, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonPlus, 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(jButtonSix, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE) | |
.addComponent(jButtonFour, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonMinus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonFive, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addGap(18, 18, 18) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) | |
.addComponent(jButtonZero, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE) | |
.addComponent(jButtonOne, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonTwo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jButtonThree, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addContainerGap()) | |
); | |
pack(); | |
}// </editor-fold>//GEN-END:initComponents | |
private void jButtonEqualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEqualActionPerformed | |
// TODO add your handling code here: | |
num2=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()); | |
if(divide==1 || num2!=0 ){ | |
ans=num1/num2; | |
jTextFieldAnswer.setText(String.valueOf(ans)); | |
divide=0; | |
} | |
/* | |
Here NetBeans returns "infinity" if division is by Zero ☺ !! | |
*/ | |
if(multiply==1){ | |
ans=num1*num2; | |
jTextFieldAnswer.setText(String.valueOf(ans)); | |
multiply=0; | |
} | |
if(percentage==1){ | |
ans=(num1/num2)*100; | |
jTextFieldAnswer.setText(String.valueOf(ans)+"%"); | |
multiply=0; | |
} | |
if(add==1){ | |
ans=num1+num2; | |
add=0; | |
jTextFieldAnswer.setText(String.valueOf(ans)); | |
} | |
if(subtract==1){ | |
ans=num1-num2; | |
subtract=0; | |
jTextFieldAnswer.setText(String.valueOf(ans)); | |
} | |
}//GEN-LAST:event_jButtonEqualActionPerformed | |
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed | |
// TODO add your handling code here: | |
this.initial(); | |
}//GEN-LAST:event_jButtonClearActionPerformed | |
private void jButtonZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZeroActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("0"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "0"); | |
} | |
}//GEN-LAST:event_jButtonZeroActionPerformed | |
private void jButtonOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOneActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("1"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "1"); | |
} | |
}//GEN-LAST:event_jButtonOneActionPerformed | |
private void jTextFieldAnswerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAnswerActionPerformed | |
// TODO add your handling code here: | |
}//GEN-LAST:event_jTextFieldAnswerActionPerformed | |
private void jButtonNineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNineActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("9"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "9"); | |
} | |
}//GEN-LAST:event_jButtonNineActionPerformed | |
private void jButtonEightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEightActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("8"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "8"); | |
} | |
}//GEN-LAST:event_jButtonEightActionPerformed | |
private void jButtonSevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSevenActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("7"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "7"); | |
} | |
}//GEN-LAST:event_jButtonSevenActionPerformed | |
private void jButtonSixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSixActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("6"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "6"); | |
} | |
}//GEN-LAST:event_jButtonSixActionPerformed | |
private void jButtonFiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFiveActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("5"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "5"); | |
} | |
}//GEN-LAST:event_jButtonFiveActionPerformed | |
private void jButtonFourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFourActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("4"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "4"); | |
} | |
}//GEN-LAST:event_jButtonFourActionPerformed | |
private void jButtonThreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThreeActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("3"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "3"); | |
} | |
}//GEN-LAST:event_jButtonThreeActionPerformed | |
private void jButtonTwoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTwoActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().length()<16 && "".equals(jTextFieldAnswer.getText())){ | |
jTextFieldAnswer.setText("2"); | |
}else if(jTextFieldAnswer.getText().length()<16){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+ "2"); | |
} | |
}//GEN-LAST:event_jButtonTwoActionPerformed | |
private void jButtonCgSignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCgSignActionPerformed | |
// TODO add your handling code here: | |
switch (jTextFieldAnswer.getText()) { | |
case "": | |
case "+": | |
jTextFieldAnswer.setText("-"); | |
break; | |
case "-": | |
jTextFieldAnswer.setText("+"); | |
break; | |
default: | |
CgSign=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
CgSign*=-1; | |
jTextFieldAnswer.setText(String.valueOf(CgSign)); | |
break; | |
} | |
}//GEN-LAST:event_jButtonCgSignActionPerformed | |
private void jButtonDecimalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDecimalActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText().contains(".")){ | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()); | |
}else { | |
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+"."); | |
} | |
}//GEN-LAST:event_jButtonDecimalActionPerformed | |
private void jButtonDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDivideActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
divide=1; | |
this.initial(); | |
} | |
}//GEN-LAST:event_jButtonDivideActionPerformed | |
private void jButtonMultiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMultiplyActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
multiply=1; | |
this.initial(); | |
} | |
}//GEN-LAST:event_jButtonMultiplyActionPerformed | |
private void jButtonInverseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInverseActionPerformed | |
// TODO add your handling code here: | |
Inverse=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
if(jTextFieldAnswer.getText()!= null || !"0".equals(jTextFieldAnswer.getText())){ | |
inv=(1/Inverse); | |
jTextFieldAnswer.setText(String.valueOf(inv)); | |
} | |
}//GEN-LAST:event_jButtonInverseActionPerformed | |
private void jButtonPercentageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPercentageActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
percentage=1; | |
this.initial(); | |
} | |
}//GEN-LAST:event_jButtonPercentageActionPerformed | |
private void jButtonPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPlusActionPerformed | |
// TODO add your handling code here: | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
add=1; | |
this.initial(); | |
}//GEN-LAST:event_jButtonPlusActionPerformed | |
private void jButtonMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMinusActionPerformed | |
// TODO add your handling code here: | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
subtract=1; | |
this.initial(); | |
}//GEN-LAST:event_jButtonMinusActionPerformed | |
private void jButtonSineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSineActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
double arcsin=num1 - ((num1*num1*num1)/(6))+((num1*num1*num1*num1*num1)/(5*4*3*2*1)); | |
jTextFieldAnswer.setText(String.valueOf(arcsin)); | |
} | |
}//GEN-LAST:event_jButtonSineActionPerformed | |
private void jButtonTangentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTangentActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
double arctang=Math.tan(num1); | |
jTextFieldAnswer.setText(String.valueOf(arctang)); | |
} | |
}//GEN-LAST:event_jButtonTangentActionPerformed | |
private void jButtonCosineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCosineActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
double arcos=Math.cos(num1); | |
jTextFieldAnswer.setText(String.valueOf(arcos)); | |
} | |
}//GEN-LAST:event_jButtonCosineActionPerformed | |
private void jButtonRootActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRootActionPerformed | |
// TODO add your handling code here: | |
if(jTextFieldAnswer.getText()!= null){ | |
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText())); | |
double root=Math.sqrt(num1); | |
jTextFieldAnswer.setText(String.valueOf(root)); | |
} | |
}//GEN-LAST:event_jButtonRootActionPerformed | |
void initial() | |
{ | |
this.jTextFieldAnswer.setText(""); | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* Set the Nimbus look and feel */ | |
//<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. | |
* For details see http://download.oracle.com/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(CalcUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(CalcUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(CalcUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(CalcUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(() -> { | |
new CalcUI().setVisible(true); | |
}); | |
} | |
// Variables declaration - do not modify//GEN-BEGIN:variables | |
private javax.swing.JButton jButtonCgSign; | |
private javax.swing.JButton jButtonClear; | |
private javax.swing.JButton jButtonCosine; | |
private javax.swing.JButton jButtonDecimal; | |
private javax.swing.JButton jButtonDivide; | |
private javax.swing.JButton jButtonEight; | |
private javax.swing.JButton jButtonEqual; | |
private javax.swing.JButton jButtonFive; | |
private javax.swing.JButton jButtonFour; | |
private javax.swing.JButton jButtonInverse; | |
private javax.swing.JButton jButtonMinus; | |
private javax.swing.JButton jButtonMultiply; | |
private javax.swing.JButton jButtonNine; | |
private javax.swing.JButton jButtonOne; | |
private javax.swing.JButton jButtonPercentage; | |
private javax.swing.JButton jButtonPlus; | |
private javax.swing.JButton jButtonRoot; | |
private javax.swing.JButton jButtonSeven; | |
private javax.swing.JButton jButtonSine; | |
private javax.swing.JButton jButtonSix; | |
private javax.swing.JButton jButtonTangent; | |
private javax.swing.JButton jButtonThree; | |
private javax.swing.JButton jButtonTwo; | |
private javax.swing.JButton jButtonZero; | |
private javax.swing.JTextField jTextFieldAnswer; | |
// End of variables declaration//GEN-END:variables | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
jButtonEight.addActionListener(this); | |
jButtonFour.addActionListener(this); | |
jButtonDivide.addActionListener(this); | |
jButtonThree.addActionListener(this); | |
jButtonTwo.addActionListener(this); | |
jButtonOne.addActionListener(this); | |
jButtonSeven.addActionListener(this); | |
jButtonSix.addActionListener(this); | |
jButtonFive.addActionListener(this); | |
jButtonEqual.addActionListener(this); | |
jButtonInverse.addActionListener(this); | |
jButtonMinus.addActionListener(this); | |
jButtonMultiply.addActionListener(this); | |
jButtonNine.addActionListener(this); | |
jButtonCgSign.addActionListener(this); | |
jButtonInverse.addActionListener(this); | |
jButtonDecimal.addActionListener(this); | |
jButtonPercentage.addActionListener(this); | |
jButtonPlus.addActionListener(this); | |
jButtonZero.addActionListener(this); | |
} | |
/* | |
e.getSource function perofms the work or action listeners | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment