Hello,
I am trying to get my calculator GUI to +,-,* and /. I have got all of them to work except my division. I was wondering if someone could helpme figure out the problem. Any input would help and I'll leave my code in here
================================================== =======
package chapfour;
/**
*
* @author Deezle
*/
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calc2 {
Container pane;
static JFrame frame;
static JButton back;
static JButton ce;
static JButton cc;
static JButton mc;
static JButton mr;
static JButton ms;
static JButton mplus;
static JButton seven;
static JButton eight;
static JButton nine;
static JButton four;
static JButton three;
static JButton five;
static JButton six;
static JButton one;
static JButton two;
static JButton slash;
static JButton multi;
static JButton div;
static JButton sub;
static JButton add;
static JButton root;
static JButton modulus;
static JButton frac;
static JButton equal;
static JButton zero;
static JButton plusmin;
static JButton decimal;
static JTextField tf2;
static JTextField tf;
double total = 0;
double total2 = 0;
String operation;
double dTotal;
/** Creates a new instance of Calc */
public Calc2 () {
frame = new JFrame ("Calculator");
pane = frame.getContentPane ();
pane.setLayout (new GridBagLayout ());
tf = new JTextField ("0");
GridBagConstraints c = new GridBagConstraints ();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
back = new JButton ("Back");
back.addActionListener (new MyListener ());
pane.add (back, c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
ce = new JButton ("CE");
ce.addActionListener (new MyListener ());
pane.add (ce, c);
c.gridx = 4;
c.gridy = 1;
cc = new JButton ("C");
cc.addActionListener (new MyListener ());
pane.add (cc, c);
c.gridx = 0;
c.gridy = 2;
mc = new JButton ("MC");
mc.addActionListener (new MyListener ());
pane.add (mc, c);
c.gridx = 0;
c.gridy = 3;
mr = new JButton ("MR");
mr.addActionListener (new MyListener ());
pane.add (mr, c);
c.gridx = 0;
c.gridy = 4;
ms = new JButton ("MS");
ms.addActionListener (new MyListener ());
pane.add (ms, c);
c.gridx = 0;
c.gridy = 5;
mplus = new JButton ("M+");
mplus.addActionListener (new MyListener ());
pane.add (mplus, c);
c.gridx = 1;
c.gridy = 2;
seven = new JButton ("7");
seven.addActionListener (new MyListener ());
pane.add (seven, c);
c.gridx = 2;
c.gridy = 2;
eight = new JButton ("8");
eight.addActionListener (new MyListener ());
pane.add (eight, c);
c.gridx = 3;
c.gridy = 2;
nine = new JButton ("9");
nine.addActionListener (new MyListener ());
pane.add (nine, c);
c.gridx = 1;
c.gridy = 3;
four = new JButton ("4");
four.addActionListener (new MyListener ());
pane.add (four, c);
c.gridx = 2;
c.gridy = 3;
five = new JButton ("5");
five.addActionListener (new MyListener ());
pane.add (five, c);
c.gridx = 1;
c.gridy = 4;
three = new JButton ("3");
three.addActionListener (new MyListener ());
pane.add (three, c);
c.gridx = 3;
c.gridy = 3;
six = new JButton ("6");
six.addActionListener (new MyListener ());
pane.add (six, c);
c.gridx = 2;
c.gridy = 4;
two = new JButton ("2");
two.addActionListener (new MyListener ());
pane.add (two, c);
c.gridx = 3;
c.gridy = 4;
one = new JButton ("1");
one.addActionListener (new MyListener ());
pane.add (one, c);
c.gridx = 1;
c.gridy = 5;
zero = new JButton ("0");
zero.addActionListener (new MyListener ());
pane.add (zero, c);
c.gridx = 2;
c.gridy = 5;
plusmin = new JButton ("+/-");
plusmin.addActionListener (new MyListener ());
pane.add (plusmin, c);
c.gridx = 3;
c.gridy = 5;
decimal = new JButton (".");
decimal.addActionListener (new MyListener ());
pane.add (decimal, c);
c.gridx = 5;
c.gridy = 1;
slash = new JButton ("/");
slash.addActionListener (new MyListener ());
pane.add (slash, c);
c.gridx = 4;
c.gridy = 2;
multi = new JButton ("*");
multi.addActionListener (new MyListener ());
pane.add (multi, c);
c.gridx = 4;
c.gridy = 3;
div = new JButton ("/");
slash.addActionListener (new MyListener ());
pane.add (slash, c);
c.gridx = 4;
c.gridy = 4;
sub = new JButton ("-");
sub.addActionListener (new MyListener ());
pane.add (sub, c);
c.gridx = 4;
c.gridy = 5;
add = new JButton ("+");
add.addActionListener (new MyListener ());
pane.add (add, c);
c.gridx = 5;
c.gridy = 2;
root = new JButton ("sqrt");
root.addActionListener (new MyListener ());
pane.add (root, c);
c.gridx = 5;
c.gridy = 3;
modulus = new JButton ("%");
modulus.addActionListener (new MyListener ());
pane.add (modulus, c);
c.gridx = 5;
c.gridy = 4;
frac = new JButton ("1/x");
frac.addActionListener (new MyListener ());
pane.add (frac, c);
c.gridx = 5;
c.gridy = 5;
equal = new JButton ("=");
equal.addActionListener (new MyListener ());
pane.add (equal, c);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 6;
tf.addActionListener (new MyListener ());
pane.add (tf, c);
tf2 = new JTextField();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
tf2.addActionListener (new MyListener ());
pane.add (tf2, c);
frame.pack ();
frame.setVisible (true);
}
/**
* @param args the command line arguments
*/
public static void main (String[] args) {
Calc2 calc = new Calc2 ();
// TODO code application logic here
}
class MyListener
implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource () == back) {
tf.setText ("Backspace");
}
if (e.getSource () == ce) {
total = 0;
tf.setText ("0");
}
if (e.getSource () == cc) {
total = 0;
tf.setText ("0");
}
if (e.getSource () == mc) {
tf.setText ("MC");
}
if (e.getSource () == mr) {
tf.setText ("MR");
}
if (e.getSource () == ms) {
tf.setText ("MS");
}
if (e.getSource () == mplus) {
tf.setText ("M+");
}
if (e.getSource () == seven) {
total = (total*10)+7;
tf.setText(""+total);
}
if (e.getSource () == eight) {
total = (total*10)+8;
tf.setText (""+total);
}
if (e.getSource () == nine) {
total = (total*10)+9;
tf.setText (""+total);
}
if (e.getSource () == four) {
total = (total*10)+4;
tf.setText (""+total);
}
if (e.getSource () == five) {
total = (total*10)+5;
tf.setText (""+total);
}
if (e.getSource () == six) {
total = (total*10)+6;
tf.setText (""+total);
}
if (e.getSource () == three) {
total = (total*10)+3;
tf.setText (""+total);
}
if (e.getSource () == two) {
total = (total*10)+2;
tf.setText (""+total);
}
if (e.getSource () == one) {
total = (total*10)+1;
tf.setText (""+total);
}
if (e.getSource () == zero) {
total = (total*10);
tf.setText (""+total);
}
if (e.getSource () == plusmin) {
total*=-1;
tf.setText (""+total);
}
if (e.getSource () == decimal) {
tf.setText (".");
}
if (e.getSource () == slash) {
total2 = total;
total = 0;
operation = "divide";
}
if (e.getSource () == multi) {
total2 = total;
total = 0;
operation = "multiply";
}
if (e.getSource () == sub) {
total2 = total;
total = 0;
operation = "subtract";
}
if (e.getSource () == add) {
total2 = total;
total = 0;
operation = "add";
}
if (e.getSource () == root) {
dTotal = Math.sqrt(total);
tf.setText ("" + dTotal);
}
if (e.getSource () == modulus) {
tf.setText ("%");
}
if (e.getSource () == frac) {
tf.setText ("1/X");
}
if (e.getSource () == equal) {
if (operation=="add"){
total = total+total2;
tf.setText(""+total);
total = 0;
total2 = 0;
}
if (operation=="multiply"){
total = total*total2;
tf.setText(""+total);
total = 0;
total2 = 0;
}
if (operation=="divide"){
dTotal = (total2/ total);
tf.setText(""+dTotal);
total = 0;
total2 = 0;
dTotal = 0;
}
if (operation=="subtract"){
total = total2-total;
tf.setText(""+total);
total = 0;
total2 = 0;
}
}
}
}
}