| | |
Simple Calculator
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; public class Calculator { public static void main(String[] args) { new Calculator(); } public Calculator() { JFrame f = new JFrame(" "); JPanel textfieldpanel1 = new JPanel(); JPanel textfieldpanel2 = new JPanel(); JPanel row1panel = new JPanel(); JPanel row2pane1 = new JPanel(); JPanel row3pane1 = new JPanel(); JPanel row4pane1 = new JPanel(); JTextField mainpanel = new JTextField(16); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); JButton b5 = new JButton("5"); JButton b6 = new JButton("6"); JButton b7 = new JButton("7"); JButton b8 = new JButton("8"); JButton b9 = new JButton("9"); JButton b10 = new JButton("0"); JButton divide = new JButton("/"); JButton multiply = new JButton("*"); JButton subtract = new JButton("-"); JButton add = new JButton("+"); JButton equals = new JButton("="); JButton dot = new JButton(". "); JButton clear = new JButton("C"); textfieldpanel1.setLayout(new GridLayout(1,1)); textfieldpanel1.add(textfieldpanel1); textfieldpanel2.add(textfieldpanel1); row1panel.setLayout(new GridLayout(1,3)); row2panel.setLayout(new GridLayout(1,3)); row3panel.setLayout(new GridLayout(1,3)); row4panel.setLayout(new GridLayout(1,3)); row1panel.add(b7); row1panel.add(b8); row1panel.add(b9); row1panel.add(divide); row2panel.add(b4); row2panel.add(b5); row2panel.add(b6); row2panel.add(multiply); row3panel.add(b1); row3panel.add(b2); row3panel.add(b3); row3panel.add(subtract); row4panel.add(b10); row4panel.add(dot); row4panel.add(clear); row4panel.add(add); mainpanel.setLayout(new GridLayout(5,1)); mainpanel.add(textfieldpanel2); mainpanel.add(row1panel); mainpanel.add(row2panel); mainpanel.add(row3panel); mainpanel.add(row4panel); f.setContentPane(mainpanel); f.setSize(200,200); f.show(); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
It won't work.
Please help.
theo19, for a start you have confused the names on lines 22 to 24, you have written:
instead of
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.
java Syntax (Toggle Plain Text)
JPanel row2pane1 = new JPanel(); JPanel row3pane1 = new JPanel(); JPanel row4pane1 = new JPanel();
instead of
java Syntax (Toggle Plain Text)
JPanel row2panel = new JPanel(); JPanel row3panel = new JPanel(); 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.
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
whew, thanks. i didn't notice that.
still it won't work. -.-
help.
still it won't work. -.-
help.
Java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.*; public class Calculator { public static void main(String[] args) { new Calculator(); } public Calculator() { JFrame f = new JFrame(" "); JPanel textfieldpanel1 = new JPanel(); JPanel textfieldpanel2 = new JPanel(); JPanel row1panel = new JPanel(); JPanel row2panel = new JPanel(); JPanel row3panel = new JPanel(); JPanel row4panel = new JPanel(); JTextField mainpanel = new JTextField(16); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); JButton b5 = new JButton("5"); JButton b6 = new JButton("6"); JButton b7 = new JButton("7"); JButton b8 = new JButton("8"); JButton b9 = new JButton("9"); JButton b10 = new JButton("0"); JButton divide = new JButton("/"); JButton multiply = new JButton("*"); JButton subtract = new JButton("-"); JButton add = new JButton("+"); JButton equals = new JButton("="); JButton dot = new JButton(". "); JButton clear = new JButton("C"); textfieldpanel1.setLayout(new GridLayout(1,1)); textfieldpanel1.add(textfieldpanel1); textfieldpanel2.add(tf); row1panel.setLayout(new GridLayout(1,3)); row2panel.setLayout(new GridLayout(1,3)); row3panel.setLayout(new GridLayout(1,3)); row4panel.setLayout(new GridLayout(1,3)); row1panel.add(b7); row1panel.add(b8); row1panel.add(b9); row1panel.add(divide); row2panel.add(b4); row2panel.add(b5); row2panel.add(b6); row2panel.add(multiply); row3panel.add(b1); row3panel.add(b2); row3panel.add(b3); row3panel.add(subtract); row4panel.add(b10); row4panel.add(dot); row4panel.add(clear); row4panel.add(add); mainpanel.setLayout(new GridLayout(5,1)); mainpanel.add(textfieldpanel2); mainpanel.add(row1panel); mainpanel.add(row2panel); mainpanel.add(row3panel); mainpanel.add(row4panel); f.setContentPane(mainpanel); f.setSize(200,200); f.show(); f.setResizable(false); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
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:
to :
java Syntax (Toggle Plain Text)
f.show();
to :
java Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Help w/a simple js calculator please... (JavaScript / DHTML / AJAX)
- Is This Simple Calculator Coded Correctly? C++ (C++)
- C++ simple calculator (C++)
- Simple C Calculator (C)
- Simple calculator code problem (Assembly)
- simple calculator using stacks (C++)
- Code Snippet: A Simple Calculator (C)
- simple calculator help (Java)
Other Threads in the Java Forum
- Previous Thread: Jcroll pane
- Next Thread: how to extract metadata from html files using java
Views: 722 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application applications arguments array arrays automation bank binary bluetooth chat class classes clear client code component database db detection development dice draw ebook eclipse error event exception file formatingtextintooltipjava fractal game givemetehcodez graphics gui helpwithhomework html ide image infinite input integer invokingapacheantprogrammatically j2me jarfile java javaprojects jmf jni jpanel julia linux list loop map method methods mobile netbeans newbie number object openjavafx oracle os print problem program programming project recursion repositories scanner screen scrollbar server set size sms socket sort sorting sql sqlserver state storm string superclass swing swt test text-file threads time transfer tree windows






