Skip to content

Instantly share code, notes, and snippets.

@TheBeachMaster
Created November 14, 2014 10:30
Show Gist options
  • Save TheBeachMaster/80743a37acf3ae5ba8d9 to your computer and use it in GitHub Desktop.
Save TheBeachMaster/80743a37acf3ae5ba8d9 to your computer and use it in GitHub Desktop.
My Calculator app
/*
* 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 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() {
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">
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();
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);
}
});
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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButtonClear, javax.swing.GroupLayout.DEFAULT_SIZE, 72, 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(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))
.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)))
.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(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.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(jButtonZero, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 57, 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(jButtonCgSign, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
.addComponent(jButtonPercentage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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))
.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)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonSix, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonFive, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
.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))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButtonThree, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
.addComponent(jButtonTwo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonOne, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButtonZero, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButtonEqualActionPerformed(java.awt.event.ActionEvent evt) {
// 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));
}
}
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.initial();
}
private void jButtonZeroActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonOneActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jTextFieldAnswerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButtonNineActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonEightActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonSevenActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonSixActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonFiveActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonFourActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonThreeActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonTwoActionPerformed(java.awt.event.ActionEvent evt) {
// 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");
}
}
private void jButtonCgSignActionPerformed(java.awt.event.ActionEvent evt) {
// 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;
}
}
private void jButtonDecimalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(jTextFieldAnswer.getText().contains(".")){
jTextFieldAnswer.setText(jTextFieldAnswer.getText());
}else {
jTextFieldAnswer.setText(jTextFieldAnswer.getText()+".");
}
}
private void jButtonDivideActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(jTextFieldAnswer.getText()!= null){
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText()));
divide=1;
this.initial();
}
}
private void jButtonMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(jTextFieldAnswer.getText()!= null){
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText()));
multiply=1;
this.initial();
}
}
private void jButtonInverseActionPerformed(java.awt.event.ActionEvent evt) {
// 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));
}
}
private void jButtonPercentageActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(jTextFieldAnswer.getText()!= null){
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText()));
percentage=1;
this.initial();
}
}
private void jButtonPlusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText()));
add=1;
this.initial();
}
private void jButtonMinusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
num1=Double.parseDouble(String.valueOf(jTextFieldAnswer.getText()));
subtract=1;
this.initial();
}
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
private javax.swing.JButton jButtonCgSign;
private javax.swing.JButton jButtonClear;
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 jButtonSeven;
private javax.swing.JButton jButtonSix;
private javax.swing.JButton jButtonThree;
private javax.swing.JButton jButtonTwo;
private javax.swing.JButton jButtonZero;
private javax.swing.JTextField jTextFieldAnswer;
// End of variables declaration
@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
*/
}
@TheBeachMaster
Copy link
Author

Works great. IDE is Netbeans. Happy coding ☺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment