I need help in my GUI calculator

I need help in my GUI calculator

Page 1 of 1

2 Replies - 1567 Views - Last Post: 02 September 2009 - 05:42 AM Rate Topic: -----

#1 bitoyz0008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-September 09

I need help in my GUI calculator

Post icon  Posted 02 September 2009 - 05:16 AM

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalcFrame extends JFrame implements ActionListener {

static JTextField answer = new JTextField("0",12);
JButton zero = new JButton("0");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton equal = new JButton("=");
JButton dot = new JButton(".");
JButton AC = new JButton("AC");

double number1, number2, result;
char choice;
static String lastCommand = null;

private static void assign(String no)
{
if((answer.getText()).equals("0"))
answer.setText(no);
else if(lastCommand == "=")
{
answer.setText(no);
lastCommand = null;
}
else
answer.setText(answer.getText()+no);
}

Panel p1,p2,p3,p4,p5,p6;


public CalcFrame() {

p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();

setTitle("Fates Calculator");
setSize(300,250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

p1.setLayout(new GridLayout (1, 3, 2, 2) );
p2.setLayout(new GridLayout (1, 3, 2, 2) );
p3.setLayout(new GridLayout (1, 3, 2, 2) );
p4.setLayout(new GridLayout (1, 3, 2, 2) );
p5.setLayout(new GridLayout (1, 3, 2, 2) );

dot.addActionListener(this);
equal.addActionListener(this);
zero.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
add.addActionListener(this);
subtract.addActionListener(this);
AC.addActionListener(this);

answer.setEditable(false);

AC.setForeground(Color.red);
p1.add(AC);
p2.add(seven);
p2.add(eight);
p2.add(nine);
p2.add(divide);
p3.add(four);
p3.add(five);
p3.add(six);
p3.add(multiply);
p4.add(one);
p4.add(two);
p4.add(three);
p4.add(subtract);
p5.add(zero);
p5.add(dot);
p5.add(add);
p5.add(equal);

setLayout(new GridLayout(6,1));
add(answer);
add(p1);
add(p2);
add(p3);
add(p4);
add(p5);

setVisible(true);
pack();
}

public void performedAction(ActionEvent evt) {

Object source = evt.getSource();

if (source == add) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '+';
}
if (source == subtract) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '-';
}
if (source == multiply) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '*';
}
if (source == divide) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '/';
}
if (source == zero) {
assign("0");
} else
if (source == one) {
assign("1");
} else
if (source == two) {
assign("2");
} else
if (source == three) {
assign("3");
} else
if (source == four) {
assign("4");
} else
if (source == five) {
assign("5");
} else
if (source == six) {
assign("6");
} else
if (source == seven) {
assign("7");
} else
if (source == eight) {
assign("8");
} else
if (source == nine) {
assign("9");
}

if (source == equal) {
number2 = Double.parseDouble(answer.getText());
if (choice == '+') {
result = number1 + number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
if (choice == '-') {
result = number1 - number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
if (choice == '*') {
result = number1 * number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
if (choice == '/') {
result = number1 / number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
}

if (source == AC) {
answer.setText("0");
number1 = 0;
number2 = 0;
}

}
public static void main(String[] args) {
CalcFrame frame = new CalcFrame();
}
}

View Postbitoyz0008, on 2 Sep, 2009 - 04:15 AM, said:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalcFrame extends JFrame implements ActionListener {

static JTextField answer = new JTextField("0",12);
JButton zero = new JButton("0");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton equal = new JButton("=");
JButton dot = new JButton(".");
JButton AC = new JButton("AC");

double number1, number2, result;
char choice;
static String lastCommand = null;

private static void assign(String no)
{
if((answer.getText()).equals("0"))
answer.setText(no);
else if(lastCommand == "=")
{
answer.setText(no);
lastCommand = null;
}
else
answer.setText(answer.getText()+no);
}

Panel p1,p2,p3,p4,p5,p6;


public CalcFrame() {

p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();

setTitle("Fates Calculator");
setSize(300,250);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

p1.setLayout(new GridLayout (1, 3, 2, 2) );
p2.setLayout(new GridLayout (1, 3, 2, 2) );
p3.setLayout(new GridLayout (1, 3, 2, 2) );
p4.setLayout(new GridLayout (1, 3, 2, 2) );
p5.setLayout(new GridLayout (1, 3, 2, 2) );

dot.addActionListener(this);
equal.addActionListener(this);
zero.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
add.addActionListener(this);
subtract.addActionListener(this);
AC.addActionListener(this);

answer.setEditable(false);

AC.setForeground(Color.red);
p1.add(AC);
p2.add(seven);
p2.add(eight);
p2.add(nine);
p2.add(divide);
p3.add(four);
p3.add(five);
p3.add(six);
p3.add(multiply);
p4.add(one);
p4.add(two);
p4.add(three);
p4.add(subtract);
p5.add(zero);
p5.add(dot);
p5.add(add);
p5.add(equal);

setLayout(new GridLayout(6,1));
add(answer);
add(p1);
add(p2);
add(p3);
add(p4);
add(p5);

setVisible(true);
pack();
}

public void performedAction(ActionEvent evt) {

Object source = evt.getSource();

if (source == add) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '+';
}
if (source == subtract) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '-';
}
if (source == multiply) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '*';
}
if (source == divide) {
number1 = Double.parseDouble(answer.getText());
answer.setText("");
choice = '/';
}
if (source == zero) {
assign("0");
} else
if (source == one) {
assign("1");
} else
if (source == two) {
assign("2");
} else
if (source == three) {
assign("3");
} else
if (source == four) {
assign("4");
} else
if (source == five) {
assign("5");
} else
if (source == six) {
assign("6");
} else
if (source == seven) {
assign("7");
} else
if (source == eight) {
assign("8");
} else
if (source == nine) {
assign("9");
}

if (source == equal) {
number2 = Double.parseDouble(answer.getText());
if (choice == '+') {
result = number1 + number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
if (choice == '-') {
result = number1 - number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
if (choice == '*') {
result = number1 * number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
if (choice == '/') {
result = number1 / number2;
answer.setText("" + result);
number1 = 0;
number2 = 0;
}
}

if (source == AC) {
answer.setText("0");
number1 = 0;
number2 = 0;
}

}
public static void main(String[] args) {
CalcFrame frame = new CalcFrame();
}
}



it seems that it contains errors..... help ls

Is This A Good Question/Topic? 0
  • +

Replies To: I need help in my GUI calculator

#2 ProdicalProgrammer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 26-June 09

Re: I need help in my GUI calculator

Posted 02 September 2009 - 05:28 AM

its called actionPerformed not performedAction
please try to clarify the problem more next time :-)
Was This Post Helpful? 0
  • +
  • -

#3 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 226
  • Joined: 05-August 09

Re: I need help in my GUI calculator

Posted 02 September 2009 - 05:42 AM

Also please Posted Image next time.

This post has been edited by Travis1012: 02 September 2009 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1