Simple Calculator

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 10
Reputation: theo19 is an unknown quantity at this point 
Solved Threads: 0
theo19 theo19 is offline Offline
Newbie Poster

Simple Calculator

 
0
  #1
Sep 28th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Simple Calculator

 
1
  #2
Sep 28th, 2009
theo19, for a start you have confused the names on lines 22 to 24, you have written:

  1. JPanel row2pane1 = new JPanel();
  2. JPanel row3pane1 = new JPanel();
  3. JPanel row4pane1 = new JPanel();

instead of

  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.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: theo19 is an unknown quantity at this point 
Solved Threads: 0
theo19 theo19 is offline Offline
Newbie Poster

Re: Simple Calculator

 
0
  #3
Sep 28th, 2009
whew, thanks. i didn't notice that.
still it won't work. -.-
help.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Simple Calculator

 
0
  #4
Sep 28th, 2009
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:

  1. f.show();

to :

  1. f.setVisible(true);
Last edited by majestic0110; Sep 28th, 2009 at 9:59 am.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 722 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC