(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 }
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 }
