# Java Swing Calculator with precedence

Page 1 of 1

## 0 Replies - 2349 Views - Last Post: 04 November 2007 - 09:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=36808&amp;s=98023c377ce842cd881a3eac944c1e30&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 itsmichelle

Reputation: 0
• Posts: 1
• Joined: 04-November 07

# Java Swing Calculator with precedence

Posted 04 November 2007 - 09:27 PM

This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However, my teacher wants the operators to follow the algebraic order of operations by chaining multiple operations. Such as, 7 + 4 * 2= 15. The operatorListener is the ActionListener for the operator buttons. Thanks for any help you can give me.

```import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.event.*;

public class Calculator extends JFrame {

JTextField textField;
private boolean startNumber= true;
private int resultTotal;
private String operator= "=";

public static void main(String[] args) {
Calculator calc = new Calculator();
calc.setVisible(true);
}

public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,400);
setLayout(new BorderLayout());

JPanel textPanel= new JPanel();
textPanel.setLayout(new FlowLayout());
textField= new JTextField("0", 20);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBackground(Color.WHITE);

JPanel arithmeticFunctions= new JPanel();
arithmeticFunctions.setBackground(Color.WHITE);
arithmeticFunctions.setLayout(new GridLayout(5,1));

JButton divideButton= new JButton("/");
JButton multiplyButton= new JButton("x");
JButton subtractButton= new JButton("-");
JButton equalButton= new JButton("=");

JButton seven= new JButton("7");
JButton eight= new JButton("8");
JButton nine= new JButton("9");
JButton four= new JButton("4");
JButton five= new JButton("5");
JButton six= new JButton("6");
JButton one= new JButton("1");
JButton two= new JButton("2");
JButton three= new JButton("3");
JButton zero= new JButton("0");
JButton decimal= new JButton(".");
JButton posNeg= new JButton("+/=");

JPanel cent= new JPanel();
cent.setBackground(Color.WHITE);
cent.setLayout(new FlowLayout());

JButton c= new JButton("C");
}

private void actionClear() {
startNumber= true;
textField.setText("0");
resultTotal=0;
operator= "=";
}

public class numberListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String number= e.getActionCommand();
if (startNumber) {
textField.setText(number);
startNumber= false;
}
else {
textField.setText(textField.getText()+ number);
}
}
}

public class operatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

if (startNumber)
actionClear();
else
startNumber= true;

try {
String numOperator= textField.getText();
int currentTotal= Integer.parseInt(numOperator);

if (operator.equals("/")) {
resultTotal /= currentTotal;
}
else if (operator.equals("x")) {
resultTotal *= currentTotal;
}
else if (operator.equals("-")) {
resultTotal -= currentTotal;
}
else if (operator.equals("+")) {
resultTotal += currentTotal;
}

if (operator.equals("=")) {
resultTotal= currentTotal;
}
textField.setText("" + resultTotal);
}

catch (NumberFormatException ex) {
actionClear();
}

operator= e.getActionCommand();

}
}

}

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }