Results 1 to 7 of 7
Thread: Calculator program
- 03-18-2009, 11:39 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Calculator program
I need help here! I'm trying to make a calculator program. Only the GUI with no functions, no listeners, no applets. AWT only. Here's the code:
The problem is - the text field isn't displayed, and then the frame size is limited to the buttons grid. It should be around 400 X 300. How can I fix that?Java Code:import java.awt.*; public class Calculator { private Frame frame; private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18; private TextField display; public static void main(String args[]) { Calculator calcu = new Calculator(); calcu.init(); } public void init() { frame = new Frame("MKDL System Calculator"); frame.setSize(400,400); frame.setBackground(Color.black); display = new TextField("0."); Panel panel = new Panel(); panel.setLayout (new GridLayout (3, 6)); b1 = new Button("7"); b2 = new Button("8"); b3 = new Button("9"); b4 = new Button("+"); b5 = new Button("÷"); b6 = new Button("="); b7 = new Button("4"); b8 = new Button("5"); b9 = new Button("6"); b10 = new Button("-"); b11 = new Button("="); b12 = new Button("Back"); b13 = new Button("1"); b14 = new Button("2"); b15 = new Button("3"); b16 = new Button("*"); b17 = new Button("+/-"); b18 = new Button("C"); panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); panel.add(b5); panel.add(b6); panel.add(b7); panel.add(b8); panel.add(b9); panel.add(b10); panel.add(b11); panel.add(b12); panel.add(b13); panel.add(b14); panel.add(b15); panel.add(b16); panel.add(b17); panel.add(b18); panel.setSize (200,200); panel.setBackground(Color.blue); frame.add(display); frame.add(panel); frame.pack(); frame.setVisible(true); } }
- 03-18-2009, 11:49 AM #2
try this:
display = new TextField("0.", 20);
20 might be to big, but it should set the width of the textfield
- 03-18-2009, 12:09 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
And another problem: Why is the size of my output limited to the 18 buttons I put there? it should be 400 x 300 or something!
- 03-18-2009, 12:11 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
help!
Here's the modified code:
I want the buttons on one panel, and the display text field above it.Java Code:import java.awt.*; public class Calculator { private Frame frame; private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18; private TextField display; public static void main(String args[]) { Calculator calcu = new Calculator(); calcu.init(); } public void init() { frame = new Frame("MKDL System Calculator"); frame.setSize(400,400); frame.setBackground(Color.black); display = new TextField("0.", 20); frame.add(display); Panel panel = new Panel(); panel.setLayout (new GridLayout (3,6)); panel.setSize(200,200); b1 = new Button("7"); b2 = new Button("8"); b3 = new Button("9"); b4 = new Button("+"); b5 = new Button("÷"); b6 = new Button("="); b7 = new Button("4"); b8 = new Button("5"); b9 = new Button("6"); b10 = new Button("-"); b11 = new Button("="); b12 = new Button("Back"); b13 = new Button("1"); b14 = new Button("2"); b15 = new Button("3"); b16 = new Button("*"); b17 = new Button("+/-"); b18 = new Button("C"); panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); panel.add(b5); panel.add(b6); panel.add(b7); panel.add(b8); panel.add(b9); panel.add(b10); panel.add(b11); panel.add(b12); panel.add(b13); panel.add(b14); panel.add(b15); panel.add(b16); panel.add(b17); panel.add(b18); panel.setSize (200,200); panel.setBackground(Color.blue); frame.add(panel); frame.pack(); frame.setVisible(true); } }
- 03-18-2009, 12:48 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Update: here's the new code:
How can I add some space?Java Code:import java.awt.*; public class Calculator { private Frame frame; private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18; private TextField display; public static void main(String args[]) { Calculator calcu = new Calculator(); calcu.init(); } public void init() { frame = new Frame("MKDL System Calculator"); frame.setSize(400,400); frame.setBackground(Color.black); Panel panel = new Panel(); frame.setLayout (new GridLayout (2,1)); display = new TextField("0.", 20); panel.setLayout (new GridLayout (3,6)); panel.setSize(200,200); b1 = new Button("7"); b2 = new Button("8"); b3 = new Button("9"); b4 = new Button("+"); b5 = new Button("÷"); b6 = new Button("="); b7 = new Button("4"); b8 = new Button("5"); b9 = new Button("6"); b10 = new Button("-"); b11 = new Button("="); b12 = new Button("Back"); b13 = new Button("1"); b14 = new Button("2"); b15 = new Button("3"); b16 = new Button("*"); b17 = new Button("+/-"); b18 = new Button("C"); panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); panel.add(b5); panel.add(b6); panel.add(b7); panel.add(b8); panel.add(b9); panel.add(b10); panel.add(b11); panel.add(b12); panel.add(b13); panel.add(b14); panel.add(b15); panel.add(b16); panel.add(b17); panel.add(b18); frame.add(display); frame.add(panel); frame.pack(); frame.setVisible(true); } }
- 03-18-2009, 01:27 PM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Hello,
to add some space you would need yo use a layout for your buttons on the panel. You dont have to use so much panels, by using one button per panel. Just set the layout of the JPanel then add all the buttons.
e.g.
:)Java Code:JPanel cont = new JPanel() cont.setLayout(new FlowLayout())
GridBagLayout is Better in Looks but I forgot how to use it:( Is there anyone to tell me the parameters for the GridBagLayout.????
:)
- 03-18-2009, 01:43 PM #7
You can probably add Insets to your objects, that will add space around your objects. I haven't tried that in GridLayout, but it definatly works in GridBagLayout.
You would have to import java.awt.GridBagLayout, GridBagConstraints, and Insets.
you create a new GridBagConstraints object and set its parameters, then add the object to the container with add(object, constraints);
Its a much more complex layout manager but once you get the hang of it, it by far the most functional.
Similar Threads
-
Calculator Program HELP NEEDED FAST! Homework assignment
By SteroidalPsycho in forum New To JavaReplies: 3Last Post: 03-05-2009, 04:02 AM -
unreachable statement - Java calculator program
By V2001Gordon in forum New To JavaReplies: 3Last Post: 12-13-2008, 12:57 AM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM -
calculator not working
By Renegade85 in forum New To JavaReplies: 5Last Post: 03-10-2008, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks