Calculator want Calculate!

xxxxJTxxxx's Avatar, Join Date: Feb 2009
Newbie Member
Hello everyone I'm new to the fourm as you all can see. I'm a beginner programer I'm trying to learn Java. I'm work on this simple program to help run my small business. I maintain fish tanks around town. Well I'm trying to create a simple price calculator to help making adding things up and adding the tax. Here is my diagram, and the class that I created and the program.

(label) Price of item: (textField)[input box]
(label) Number of items: (textField)[input box]
(button)[computeButton]
(label) Total Price: (textField)[input box] (uneditable) (currency format)
(label) Total Tax: (textField)[input box] (uneditable) (currency format)
(label) Total Cost: (textField)[input box] (uneditable) (currency format)

Code:
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 
5 
6 publicclass CalculatePanel extends JPanel {
7 
8 
9 
10 privateint Calculate;
11 private JButton push;
12 private JLabel label, label2, label3, label4, label5;
13 private JTextField box, box2, box3, box4, box5;
14 
15 public CalculatePanel () {
16 
17 push = new JButton ("Compute!");
18 push.addActionListener (new ButtonListener());
19 
20 label = new JLabel ("Calculate: " + Calculate); 
21 
22 label = new JLabel ("Price of item: ");
23 label2 = new JLabel ("Number of items: ");
24 label3 = new JLabel ("Total Price: ");
25 label4 = new JLabel ("Total Tax: ");
26 label5 = new JLabel ("Total Cost: ");
27 
28 
29 box = new JTextField(5);
30 box2 = new JTextField(5);
31 box3 = new JTextField(5);
32 box3.setEditable(false);
33 box4 = new JTextField(5);
34 box5 = new JTextField(5);
35 box5.setEditable(false);
36 
37 
38 add (label);
39 add (box);
40 add (label2);
41 add (box2);
42 add (label3);
43 add (box3);
44 add (label4);
45 add (box4);
46 add (label5);
47 add (box5);
48 add (push);
49 
50 
51 setPreferredSize (new Dimension(190, 225));
52 setBackground (Color.cyan);}
53 
54 publicclass ButtonListener implements ActionListener {
55 
56 publicvoid actionPerformed (ActionEvent event) {
57 
58 int p = Integer.parseInt(box.getText());
59 int n = Integer.parseInt(box2.getText());
60 int tp = Integer.parseInt(box3.getText());
61 int tt = Integer.parseInt(box4.getText());
62 int tc = Integer.parseInt(box5.getText());
63 int Calculate = p * n + tp * tt;
64 
65 Calculate++;
66 box3.setText(".07 " + Calculate);
67 box5.setText(" " + Calculate); }
68 }
69 }
The Program Runs But It Want Calculate.
Code:
1 import javax.swing.JFrame;
2 
3 publicclass Calculate {
4 //-----------------------------------------------------------------
5 // Creates the main program frame.
6 //-----------------------------------------------------------------
7 publicstaticvoid main (String[] args)
8 {
9 JFrame frame = new JFrame ("Calculate");
10 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
11 
12 frame.getContentPane().add(new CalculatePanel());
13 
14 frame.pack();
15 frame.setVisible(true);
16 }
17 }
Can anyone Help!!
0
gulliccj's Avatar, Join Date: Jun 2008
Go4Expert Member
You really should consider putting a "main" method in CalculatePanel(); This would mean less chance of failure. I am currently testing it. I will get back to you very soon.
0
gulliccj's Avatar, Join Date: Jun 2008
Go4Expert Member
for CalculatePanel.java, you were parsing empty JTextFields, Here is the NEW code:

HTML Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

 
 public class CalculatePanel extends JPanel {
 
 
 
 private int Calculate;
 private JButton push;
 private JLabel label, label2, label3, label4, label5;
 private JTextField box, box2, box3, box4, box5;
 
 public CalculatePanel () {
 
 push = new JButton ("Compute!");
 push.addActionListener (new ButtonListener());
 
 label = new JLabel ("Calculate: " + Calculate); 
 
 label = new JLabel ("Price of item: ");
 label2 = new JLabel ("Number of items: ");
 label3 = new JLabel ("Total Price: ");
 label4 = new JLabel ("Total Tax: ");
 label5 = new JLabel ("Total Cost: ");
 
 
 box = new JTextField(5);
 box2 = new JTextField(5);
 box3 = new JTextField(5);
 box3.setEditable(false);
 box4 = new JTextField(5);
 box5 = new JTextField(5);
 box5.setEditable(false);
 
 
 add (label);
 add (box);
 add (label2);
 add (box2);
 add (label3);
 add (box3);
 add (label4);
 add (box4);
 add (label5);
 add (box5);
 add (push);
 
 
 setPreferredSize (new Dimension(190, 225));
setBackground (Color.cyan);}
 
 public class ButtonListener implements ActionListener {
 
 public void actionPerformed (ActionEvent event) {
 
 double p = Double.parseDouble(box.getText());//price
 double n = Double.parseDouble(box2.getText());//number of items
  double tt = Double.parseDouble(box4.getText());//total tax
  
  /*
   *
   *ANSWER:
   *These two JTextFields are enabled(false), so you are parsing null.
   *
   */
 //int tp = Integer.parseInt(box3.getText());//total price
 //int tc = Integer.parseInt(box5.getText()); //total cost
 //int Calculate = p * n + tp * tt;
 double Calculate = p * n + tt;
 
 box3.setText(".07 " + Calculate);
 box5.setText(" " + Calculate); }
 }
 }
And for Calculate, NO CHANGES:
HTML Code:
 import javax.swing.JFrame;
 
 public class Calculate {
 //-----------------------------------------------------------------
 // Creates the main program frame.
 //-----------------------------------------------------------------
 public static void main (String[] args)
 {
 JFrame frame = new JFrame ("Calculate");
 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 
 frame.getContentPane().add(new CalculatePanel());
 
 frame.pack();
 frame.setVisible(true);
 }
 }
0
gulliccj's Avatar, Join Date: Jun 2008
Go4Expert Member
I hope I helped you. Have a nice day.