Skip to content

Instantly share code, notes, and snippets.

@svarlamov
Forked from NeilHanlon/Calculator.java
Last active February 16, 2021 06:36
Show Gist options
  • Save svarlamov/bc6c7f77584cc8a9386012ab86d68128 to your computer and use it in GitHub Desktop.
Save svarlamov/bc6c7f77584cc8a9386012ab86d68128 to your computer and use it in GitHub Desktop.
Main.java
/*
* @name Main.java
*
* Created on Jan 13, 2012, 5:03:48 PM
*/
package plugins;
import java.text.NumberFormat;
import java.text.DecimalFormat;
/**
*
* @author Neil
*/
public class Main extends javax.swing.JFrame {
double result, num1, num2;
String operator = "", resultText = "", num1T = "", num2T = "";
Boolean opset;
/** Creates new form Main */
public Main() {
this.setTitle("Calculator");
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
resultPane = new javax.swing.JTextPane();
button7 = new javax.swing.JButton();
button4 = new javax.swing.JButton();
button1 = new javax.swing.JButton();
buttonDot = new javax.swing.JButton();
button8 = new javax.swing.JButton();
button9 = new javax.swing.JButton();
buttonDiv = new javax.swing.JButton();
button5 = new javax.swing.JButton();
button2 = new javax.swing.JButton();
button0 = new javax.swing.JButton();
button6 = new javax.swing.JButton();
button3 = new javax.swing.JButton();
buttonEq = new javax.swing.JButton();
buttonmult = new javax.swing.JButton();
buttonMin = new javax.swing.JButton();
buttonPlus = new javax.swing.JButton();
buttonClear = new javax.swing.JButton();
buttonDel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
resultPane.setEditable(false);
resultPane.setFont(new java.awt.Font("Tahoma", 0, 18));
jScrollPane1.setViewportView(resultPane);
button7.setText("7");
button7.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button7MouseClicked(evt);
}
});
button4.setText("4");
button4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button4MouseClicked(evt);
}
});
button1.setText("1");
button1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button1MouseClicked(evt);
}
});
buttonDot.setText(".");
buttonDot.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonDotMouseClicked(evt);
}
});
button8.setText("8");
button8.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button8MouseClicked(evt);
}
});
button9.setText("9");
button9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button9MouseClicked(evt);
}
});
buttonDiv.setText("/");
buttonDiv.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonDivMouseClicked(evt);
}
});
button5.setText("5");
button5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button5MouseClicked(evt);
}
});
button2.setText("2");
button2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button2MouseClicked(evt);
}
});
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
button0.setText("0");
button0.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button0MouseClicked(evt);
}
});
button0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button0ActionPerformed(evt);
}
});
button6.setText("6");
button6.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button6MouseClicked(evt);
}
});
button3.setText("3");
button3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
button3MouseClicked(evt);
}
});
buttonEq.setText("=");
buttonEq.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonEqMouseClicked(evt);
}
});
buttonmult.setText("*");
buttonmult.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonmultMouseClicked(evt);
}
});
buttonmult.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonmultActionPerformed(evt);
}
});
buttonMin.setText("-");
buttonMin.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonMinMouseClicked(evt);
}
});
buttonPlus.setText("+");
buttonPlus.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonPlusMouseClicked(evt);
}
});
buttonClear.setText("Clear");
buttonClear.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonClearMouseClicked(evt);
}
});
buttonDel.setText("Del");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(buttonClear, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(buttonDel, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(120, 120, 120))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(120, 120, 120)
.addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(180, 180, 180)
.addComponent(buttonmult, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(138, 138, 138)
.addComponent(buttonMin, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(buttonDot, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonEq, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(buttonPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonDiv, buttonDot, buttonEq, buttonMin, buttonPlus, buttonmult});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(buttonDel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonClear, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonDot, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonEq, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(button9, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(buttonDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonmult, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonMin, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonDiv, buttonDot, buttonEq, buttonMin, buttonPlus, buttonmult});
pack();
}// </editor-fold>
private void buttonmultActionPerformed(java.awt.event.ActionEvent evt) {
}
private void button0ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void buttonClearMouseClicked(java.awt.event.MouseEvent evt) {
resultPane.setText("");
resultText = "";
result = 0;
num1T = "";
num2T = "";
operator = "";
}
private void button1MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "1";
resultPane.setText(num1T);
} else {
num2T = num2T + "1";
resultPane.setText(num2T);
}
}
private void button2MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "2";
resultPane.setText(num1T);
} else {
num2T = num2T + "2";
resultPane.setText(num2T);
}
}
private void button3MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "3";
resultPane.setText(num1T);
} else {
num2T = num2T + "3";
resultPane.setText(num2T);
}
}
private void button4MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "4";
resultPane.setText(num1T);
} else {
num2T = num2T + "4";
resultPane.setText(num2T);
}
}
private void button5MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "5";
resultPane.setText(num1T);
} else {
num2T = num2T + "5";
resultPane.setText(num2T);
}
}
private void button6MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "6";
resultPane.setText(num1T);
} else {
num2T = num2T + "6";
resultPane.setText(num2T);
}
}
private void button7MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "7";
resultPane.setText(num1T);
} else {
num2T = num2T + "7";
resultPane.setText(num2T);
}
}
private void button8MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "8";
resultPane.setText(num1T);
} else {
num2T = num2T + "8";
resultPane.setText(num2T);
}
}
private void button9MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "9";
resultPane.setText(num1T);
} else {
num2T = num2T + "9";
resultPane.setText(num2T);
}
}
private void buttonDotMouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + ".";
resultPane.setText(num1T);
} else {
num2T = num2T + ".";
resultPane.setText(num2T);
}
}
private void button0MouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
num1T = num1T + "0";
resultPane.setText(num1T);
} else {
num2T = num2T + "0";
resultPane.setText(num2T);
}
}
private void buttonPlusMouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
operator = "plus";
} else {
doOperation(num1T, num2T, operator, resultText);
num1T = "";
num2T = "";
operator = "";
}
}
private void buttonMinMouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
operator = "subtract";
} else {
doOperation(num1T, num2T, operator, resultText);
num1T = "";
num2T = "";
operator = "";
}
}
private void buttonmultMouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
operator = "multiply";
} else {
doOperation(num1T, num2T, operator, resultText);
num1T = "";
num2T = "";
operator = "";
}
}
private void buttonDivMouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
operator = "divide";
} else {
doOperation(num1T, num2T, operator, resultText);
num1T = "";
num2T = "";
operator = "";
}
}
private void buttonEqMouseClicked(java.awt.event.MouseEvent evt) {
if (operator == "") {
} else {
doOperation(num1T, num2T, operator, resultText);
num1T = "";
num2T = "";
operator = "";
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
private void doOperation(String num1S, String num2S, String cOp, String res1) {
char op = cOp.charAt(0);
num2 = Double.parseDouble(num2S);
if (resultText == "") {
num1 = Double.parseDouble(num1S);
switch (op) {
case 'p':
result = num1 + num2;
break;
case 's':
result = num1 - num2;
break;
case 'd':
result = num1 / num2;
break;
case 'm':
result = num1 * num2;
break;
}
} else {
double res = Double.parseDouble(res1);
switch (op) {
case 'p':
result = res + num2;
break;
case 's':
result = res - num2;
break;
case 'd':
result = res / num2;
break;
case 'm':
result = res * num2;
break;
}
}
NumberFormat f = NumberFormat.getInstance();
if (f instanceof DecimalFormat) {
((DecimalFormat) f).setDecimalSeparatorAlwaysShown(false);
}
resultText = f.format(result);
resultPane.setText(resultText);
}
// Variables declaration - do not modify
private javax.swing.JButton button0;
private javax.swing.JButton button1;
private javax.swing.JButton button2;
private javax.swing.JButton button3;
private javax.swing.JButton button4;
private javax.swing.JButton button5;
private javax.swing.JButton button6;
private javax.swing.JButton button7;
private javax.swing.JButton button8;
private javax.swing.JButton button9;
private javax.swing.JButton buttonClear;
private javax.swing.JButton buttonDel;
private javax.swing.JButton buttonDiv;
private javax.swing.JButton buttonDot;
private javax.swing.JButton buttonEq;
private javax.swing.JButton buttonMin;
private javax.swing.JButton buttonPlus;
private javax.swing.JButton buttonmult;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane resultPane;
// End of variables declaration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment