943,770 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1245
  • Java RSS
Sep 28th, 2009
0

Simple Calculator

Expand Post »
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class Calculator {
  5. public static void main(String[] args) {
  6. new Calculator();
  7. }
  8. public Calculator()
  9. {
  10. JFrame f = new JFrame(" ");
  11. JPanel textfieldpanel1 = new JPanel();
  12. JPanel textfieldpanel2 = new JPanel();
  13. JPanel row1panel = new JPanel();
  14. JPanel row2pane1 = new JPanel();
  15. JPanel row3pane1 = new JPanel();
  16. JPanel row4pane1 = new JPanel();
  17. JTextField mainpanel = new JTextField(16);
  18.  
  19. JButton b1 = new JButton("1");
  20. JButton b2 = new JButton("2");
  21. JButton b3 = new JButton("3");
  22. JButton b4 = new JButton("4");
  23. JButton b5 = new JButton("5");
  24. JButton b6 = new JButton("6");
  25. JButton b7 = new JButton("7");
  26. JButton b8 = new JButton("8");
  27. JButton b9 = new JButton("9");
  28. JButton b10 = new JButton("0");
  29. JButton divide = new JButton("/");
  30. JButton multiply = new JButton("*");
  31. JButton subtract = new JButton("-");
  32. JButton add = new JButton("+");
  33. JButton equals = new JButton("=");
  34. JButton dot = new JButton(". ");
  35. JButton clear = new JButton("C");
  36. textfieldpanel1.setLayout(new GridLayout(1,1));
  37. textfieldpanel1.add(textfieldpanel1);
  38. textfieldpanel2.add(textfieldpanel1);
  39.  
  40. row1panel.setLayout(new GridLayout(1,3));
  41.  
  42. row2panel.setLayout(new GridLayout(1,3));
  43.  
  44. row3panel.setLayout(new GridLayout(1,3));
  45.  
  46. row4panel.setLayout(new GridLayout(1,3));
  47.  
  48. row1panel.add(b7);
  49. row1panel.add(b8);
  50. row1panel.add(b9);
  51. row1panel.add(divide);
  52.  
  53. row2panel.add(b4);
  54. row2panel.add(b5);
  55. row2panel.add(b6);
  56. row2panel.add(multiply);
  57.  
  58. row3panel.add(b1);
  59. row3panel.add(b2);
  60. row3panel.add(b3);
  61. row3panel.add(subtract);
  62.  
  63. row4panel.add(b10);
  64. row4panel.add(dot);
  65. row4panel.add(clear);
  66. row4panel.add(add);
  67.  
  68. mainpanel.setLayout(new GridLayout(5,1));
  69. mainpanel.add(textfieldpanel2);
  70. mainpanel.add(row1panel);
  71. mainpanel.add(row2panel);
  72. mainpanel.add(row3panel);
  73. mainpanel.add(row4panel);
  74. f.setContentPane(mainpanel);
  75. f.setSize(200,200);
  76. f.show();
  77. f.setResizable(false);
  78. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79. }
  80. }

It won't work.
Please help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theo19 is offline Offline
10 posts
since Nov 2008
Sep 28th, 2009
1

Re: Simple Calculator

theo19, for a start you have confused the names on lines 22 to 24, you have written:

java Syntax (Toggle Plain Text)
  1. JPanel row2pane1 = new JPanel();
  2. JPanel row3pane1 = new JPanel();
  3. JPanel row4pane1 = new JPanel();

instead of

java Syntax (Toggle Plain Text)
  1. JPanel row2panel = new JPanel();
  2. JPanel row3panel = new JPanel();
  3. JPanel row4panel = new JPanel();

i.e. you have written row2pane1 instead of row2panel. This file will compile when you change those three lines, but I am guessing you are going to have some more issues with this program, for example line 37 where you add a panel to itself.
Last edited by majestic0110; Sep 28th, 2009 at 9:16 am.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Sep 28th, 2009
0

Re: Simple Calculator

whew, thanks. i didn't notice that.
still it won't work. -.-
help.
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class Calculator {
  5. public static void main(String[] args) {
  6. new Calculator();
  7. }
  8. public Calculator()
  9. {
  10. JFrame f = new JFrame(" ");
  11. JPanel textfieldpanel1 = new JPanel();
  12. JPanel textfieldpanel2 = new JPanel();
  13. JPanel row1panel = new JPanel();
  14. JPanel row2panel = new JPanel();
  15. JPanel row3panel = new JPanel();
  16. JPanel row4panel = new JPanel();
  17. JTextField mainpanel = new JTextField(16);
  18.  
  19. JButton b1 = new JButton("1");
  20. JButton b2 = new JButton("2");
  21. JButton b3 = new JButton("3");
  22. JButton b4 = new JButton("4");
  23. JButton b5 = new JButton("5");
  24. JButton b6 = new JButton("6");
  25. JButton b7 = new JButton("7");
  26. JButton b8 = new JButton("8");
  27. JButton b9 = new JButton("9");
  28. JButton b10 = new JButton("0");
  29. JButton divide = new JButton("/");
  30. JButton multiply = new JButton("*");
  31. JButton subtract = new JButton("-");
  32. JButton add = new JButton("+");
  33. JButton equals = new JButton("=");
  34. JButton dot = new JButton(". ");
  35. JButton clear = new JButton("C");
  36. textfieldpanel1.setLayout(new GridLayout(1,1));
  37. textfieldpanel1.add(textfieldpanel1);
  38. textfieldpanel2.add(tf);
  39.  
  40. row1panel.setLayout(new GridLayout(1,3));
  41.  
  42. row2panel.setLayout(new GridLayout(1,3));
  43.  
  44. row3panel.setLayout(new GridLayout(1,3));
  45.  
  46. row4panel.setLayout(new GridLayout(1,3));
  47.  
  48. row1panel.add(b7);
  49. row1panel.add(b8);
  50. row1panel.add(b9);
  51. row1panel.add(divide);
  52.  
  53. row2panel.add(b4);
  54. row2panel.add(b5);
  55. row2panel.add(b6);
  56. row2panel.add(multiply);
  57.  
  58. row3panel.add(b1);
  59. row3panel.add(b2);
  60. row3panel.add(b3);
  61. row3panel.add(subtract);
  62.  
  63. row4panel.add(b10);
  64. row4panel.add(dot);
  65. row4panel.add(clear);
  66. row4panel.add(add);
  67.  
  68. mainpanel.setLayout(new GridLayout(5,1));
  69. mainpanel.add(textfieldpanel2);
  70. mainpanel.add(row1panel);
  71. mainpanel.add(row2panel);
  72. mainpanel.add(row3panel);
  73. mainpanel.add(row4panel);
  74. f.setContentPane(mainpanel);
  75. f.setSize(200,200);
  76. f.show();
  77. f.setResizable(false);
  78. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79. }
  80. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theo19 is offline Offline
10 posts
since Nov 2008
Sep 28th, 2009
0

Re: Simple Calculator

look at line 37, you are adding a panel to itself. This will cause an Illegal Argument Exception. Comment out line 37 and think about what it is you are trying to do. It will run (maybe not quite as you might expect) when you comment out line 37. Also, on line 76 you invoke method show(), which, as of Java version 1.5 has been deprecated. Please change this line from:

java Syntax (Toggle Plain Text)
  1. f.show();

to :

java Syntax (Toggle Plain Text)
  1. f.setVisible(true);
Last edited by majestic0110; Sep 28th, 2009 at 9:59 am.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Jcroll pane
Next Thread in Java Forum Timeline: how to extract metadata from html files using java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC