Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calc;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- public class Gui extends javax.swing.JFrame {
- double firstNum = 0;
- double secondNum = 0;
- String operation = "";
- public Gui() {
- initComponents();
- }
- //@SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jTextField1 = new javax.swing.JTextField();
- Button0 = new javax.swing.JButton();
- Button1 = new javax.swing.JButton();
- Button2 = new javax.swing.JButton();
- Button3 = new javax.swing.JButton();
- Button4 = new javax.swing.JButton();
- Button5 = new javax.swing.JButton();
- Button6 = new javax.swing.JButton();
- Button7 = new javax.swing.JButton();
- Button8 = new javax.swing.JButton();
- Button9 = new javax.swing.JButton();
- CButton = new javax.swing.JButton();
- DivideButton = new javax.swing.JButton();
- PowButton = new javax.swing.JButton();
- PlusButton = new javax.swing.JButton();
- MinusButton = new javax.swing.JButton();
- PointButton = new javax.swing.JButton();
- FinalButton = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Calculator v1.0");
- setBackground(new java.awt.Color(204, 204, 204));
- setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- setForeground(new java.awt.Color(255, 255, 255));
- setResizable(false);
- jTextField1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
- jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
- jTextField1.setText("0");
- jTextField1.setToolTipText("");
- jTextField1.setEnabled(false);
- jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- jTextField1MouseClicked(evt);
- }
- });
- Button0.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button0.setText("0");
- Button0.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button0.setFocusPainted(false);
- Button0.setFocusable(false);
- Button0.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button0ActionPerformed(evt);
- }
- });
- Button1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button1.setText("1");
- Button1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button1.setFocusPainted(false);
- Button1.setFocusable(false);
- Button1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button1ActionPerformed(evt);
- }
- });
- Button2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button2.setText("2");
- Button2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button2.setFocusPainted(false);
- Button2.setFocusable(false);
- Button2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button2ActionPerformed(evt);
- }
- });
- Button3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button3.setText("3");
- Button3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button3.setFocusPainted(false);
- Button3.setFocusable(false);
- Button3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button3ActionPerformed(evt);
- }
- });
- Button4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button4.setText("4");
- Button4.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button4.setFocusPainted(false);
- Button4.setFocusable(false);
- Button4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button4ActionPerformed(evt);
- }
- });
- Button5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button5.setText("5");
- Button5.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button5.setFocusPainted(false);
- Button5.setFocusable(false);
- Button5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button5ActionPerformed(evt);
- }
- });
- Button6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button6.setText("6");
- Button6.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button6.setFocusPainted(false);
- Button6.setFocusable(false);
- Button6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button6ActionPerformed(evt);
- }
- });
- Button7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button7.setText("7");
- Button7.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button7.setFocusPainted(false);
- Button7.setFocusable(false);
- Button7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button7ActionPerformed(evt);
- }
- });
- Button8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button8.setText("8");
- Button8.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button8.setFocusPainted(false);
- Button8.setFocusable(false);
- Button8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button8ActionPerformed(evt);
- }
- });
- Button9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- Button9.setText("9");
- Button9.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- Button9.setFocusPainted(false);
- Button9.setFocusable(false);
- Button9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- Button9ActionPerformed(evt);
- }
- });
- CButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- CButton.setText("C");
- CButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- CButton.setFocusPainted(false);
- CButton.setFocusable(false);
- CButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- CButtonActionPerformed(evt);
- }
- });
- DivideButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- DivideButton.setText("/");
- DivideButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- DivideButton.setFocusPainted(false);
- DivideButton.setFocusable(false);
- DivideButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- DivideButtonActionPerformed(evt);
- }
- });
- PowButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- PowButton.setText("*");
- PowButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- PowButton.setFocusPainted(false);
- PowButton.setFocusable(false);
- PowButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PowButtonActionPerformed(evt);
- }
- });
- PlusButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- PlusButton.setText("+");
- PlusButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- PlusButton.setFocusPainted(false);
- PlusButton.setFocusable(false);
- PlusButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PlusButtonActionPerformed(evt);
- }
- });
- MinusButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- MinusButton.setText("-");
- MinusButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- MinusButton.setFocusPainted(false);
- MinusButton.setFocusable(false);
- MinusButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- MinusButtonActionPerformed(evt);
- }
- });
- PointButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- PointButton.setText(".");
- PointButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- PointButton.setFocusPainted(false);
- PointButton.setFocusable(false);
- PointButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- PointButtonActionPerformed(evt);
- }
- });
- FinalButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- FinalButton.setText("=");
- FinalButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- FinalButton.setFocusPainted(false);
- FinalButton.setFocusable(false);
- FinalButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- FinalButtonActionPerformed(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(jTextField1)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(CButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(DivideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGap(6, 6, 6)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(PowButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(MinusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(PlusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(Button0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(PointButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(FinalButton, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(DivideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(PowButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(MinusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(CButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addComponent(PlusButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(Button0, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(PointButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addComponent(FinalButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void Button7ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "7");
- } else {
- jTextField1.setText("7");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "7");
- }
- }
- private void Button8ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "8");
- } else {
- jTextField1.setText("8");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "8");
- }
- }
- private void Button9ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "9");
- } else {
- jTextField1.setText("9");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "9");
- }
- }
- private void Button0ActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText(jTextField1.getText() + "0");
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (temp.contains(" ")) {
- if (temp.startsWith("00", a + 1)) {
- temp = temp.substring(0, temp.length() - 1);
- } else if (temp.startsWith("00", a + 1)) {
- temp = temp.substring(0, temp.length() - 1);
- } else if (temp.startsWith("00", a + 1)) {
- temp = temp.substring(0, temp.length() - 1);
- } else if (temp.startsWith("00", a + 1)) {
- temp = temp.substring(0, temp.length() - 1);
- }
- }
- if (temp.startsWith("00")) {
- temp = temp.substring(0, temp.length() - 1);
- }
- jTextField1.setText(temp);
- }
- private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "1");
- } else {
- jTextField1.setText("1");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "1");
- }
- }
- private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "2");
- } else {
- jTextField1.setText("2");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "2");
- }
- }
- private void Button3ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "3");
- } else {
- jTextField1.setText("3");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "3");
- }
- }
- private void Button4ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "4");
- } else {
- jTextField1.setText("4");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "4");
- }
- }
- private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "5");
- } else {
- jTextField1.setText("5");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "5");
- }
- }
- private void Button6ActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (!temp.equals("0")) {
- jTextField1.setText(jTextField1.getText() + "6");
- } else {
- jTextField1.setText("6");
- }
- if (temp.substring(a + 1).startsWith("0")) {
- jTextField1.setText(temp.substring(0, a + 1) + "6");
- }
- }
- private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {
- String temp = jTextField1.getText();
- if (temp.endsWith(" * ") | temp.endsWith(" / ")
- | temp.endsWith(" + ") | temp.endsWith(" - ")) {
- jTextField1.setText(temp.substring(0, temp.length() - 3));
- } else if (temp.endsWith("Infinity") | temp.endsWith("NaN")) {
- jTextField1.setText("0");
- } else if (0 == temp.length() | 1 == temp.length()) {
- jTextField1.setText("0");
- } else if (0 != temp.length()) {
- jTextField1.setText(temp.substring(0, temp.length() - 1));
- }
- }
- private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- if (temp.contains(" ")) {
- temp = jTextField1.getText();
- if (operation.contains(" * ")) {
- operation = " / ";
- temp = temp.replace(" * ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" + ")) {
- operation = " / ";
- temp = temp.replace(" + ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" - ")) {
- operation = " / ";
- temp = temp.replace(" - ", operation);
- jTextField1.setText(temp);
- }
- } else {
- firstNum = Double.valueOf(jTextField1.getText());
- operation = " / ";
- temp = String.valueOf(firstNum);
- if (temp.endsWith(".0")) {
- temp = temp.substring(0, temp.length() - 2);
- jTextField1.setText(temp + operation);
- } else {
- jTextField1.setText(temp + operation);
- }
- }
- }
- private void PowButtonActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- if (temp.contains(" ")) {
- temp = jTextField1.getText();
- if (operation.contains(" / ")) {
- operation = " * ";
- temp = temp.replace(" / ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" + ")) {
- operation = " * ";
- temp = temp.replace(" + ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" - ")) {
- operation = " * ";
- temp = temp.replace(" - ", operation);
- jTextField1.setText(temp);
- }
- } else {
- firstNum = Double.valueOf(jTextField1.getText());
- operation = " * ";
- temp = String.valueOf(firstNum);
- if (temp.endsWith(".0")) {
- temp = temp.substring(0, temp.length() - 2);
- jTextField1.setText(temp + operation);
- } else {
- jTextField1.setText(temp + operation);
- }
- }
- }
- private void PlusButtonActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- if (temp.contains(" ")) {
- temp = jTextField1.getText();
- if (operation.contains(" * ")) {
- operation = " + ";
- temp = temp.replace(" * ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" / ")) {
- operation = " + ";
- temp = temp.replace(" / ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" - ")) {
- operation = " + ";
- temp = temp.replace(" - ", operation);
- jTextField1.setText(temp);
- }
- } else {
- firstNum = Double.valueOf(jTextField1.getText());
- operation = " + ";
- temp = String.valueOf(firstNum);
- if (temp.endsWith(".0")) {
- temp = temp.substring(0, temp.length() - 2);
- jTextField1.setText(temp + operation);
- } else {
- jTextField1.setText(temp + operation);
- }
- }
- }
- private void MinusButtonActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- if (temp.contains(" ")) {
- temp = jTextField1.getText();
- if (operation.contains(" * ")) {
- operation = " - ";
- temp = temp.replace(" * ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" + ")) {
- operation = " - ";
- temp = temp.replace(" + ", operation);
- jTextField1.setText(temp);
- } else if (operation.contains(" / ")) {
- operation = " - ";
- temp = temp.replace(" / ", operation);
- jTextField1.setText(temp);
- }
- } else {
- firstNum = Double.valueOf(jTextField1.getText());
- operation = " - ";
- temp = String.valueOf(firstNum);
- if (temp.endsWith(".0")) {
- temp = temp.substring(0, temp.length() - 2);
- jTextField1.setText(temp + operation);
- } else {
- jTextField1.setText(temp + operation);
- }
- }
- }
- private void FinalButtonActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- int a = temp.lastIndexOf(" ");
- if (operation.trim().equals("*")) {
- if (temp.contains(" ")) {
- secondNum = Double.valueOf(temp.substring(a + 1));
- double answer = firstNum * secondNum;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- } else {
- double numPerOperation = secondNum;
- double answer = Double.valueOf(temp);
- answer *= numPerOperation;
- firstNum = answer;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- }
- } else if (operation.trim().equals("/")) {
- if (temp.contains(" ")) {
- secondNum = Double.valueOf(temp.substring(a + 1));
- double answer = firstNum / secondNum;
- temp = String.valueOf(answer);
- if (temp.equals("NaN") | temp.equals("Infinity")) {
- jTextField1.setText(temp);
- } else {
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- }
- } else {
- double numPerOperation = secondNum;
- double answer = Double.valueOf(temp);
- answer /= numPerOperation;
- firstNum = answer;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- }
- } else if (operation.trim().equals("-")) {
- if (temp.contains(" ")) {
- secondNum = Double.valueOf(temp.substring(a + 1));
- double answer = firstNum - secondNum;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- } else {
- double numPerOperation = secondNum;
- double answer = Double.valueOf(temp);
- answer -= numPerOperation;
- firstNum = answer;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- }
- } else if (operation.trim().equals("+")) {
- if (temp.contains(" ")) {
- secondNum = Double.valueOf(temp.substring(a + 1));
- double answer = firstNum + secondNum;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- } else {
- double numPerOperation = secondNum;
- double answer = Double.valueOf(temp);
- answer += numPerOperation;
- firstNum = answer;
- answer = new BigDecimal(answer).setScale(10, RoundingMode.HALF_UP).doubleValue();
- temp = String.valueOf(answer);
- if (temp.endsWith(".0")) {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length() - 2);
- } else {
- temp = String.valueOf(answer).substring(0, String.valueOf(answer).length());
- }
- jTextField1.setText(temp);
- }
- }
- }
- private void PointButtonActionPerformed(java.awt.event.ActionEvent evt) {
- String temp = jTextField1.getText();
- String secondNumtemp = "";
- int a = temp.indexOf(" ");
- int point1 = temp.indexOf(".");
- if (!temp.startsWith(".", point1)) {
- jTextField1.setText(jTextField1.getText() + ".");
- } else if (temp.contains(" ")) {
- if (a != -1) {
- secondNumtemp = temp.substring(a + 3);
- }
- if (!secondNumtemp.contains(".")) {
- jTextField1.setText(jTextField1.getText() + ".");
- }
- }
- }
- private void CButtonActionPerformed(java.awt.event.ActionEvent evt) {
- jTextField1.setText("0");
- }
- // 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 CButton;
- private javax.swing.JButton DivideButton;
- private javax.swing.JButton FinalButton;
- private javax.swing.JButton MinusButton;
- private javax.swing.JButton PlusButton;
- private javax.swing.JButton PointButton;
- private javax.swing.JButton PowButton;
- private javax.swing.JTextField jTextField1;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment