Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Calculator layout problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator layout problem

    Hey there pretty new to GUI java programming, my task is to create a calculator, im just having trouble with the actual layout of the calculator so far it is supposed to have a text box then below the buttons of the calculator and below this another text box, but all i can seem to do is create one grid which isn't adequate, this is my code, please help!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    class BlackFrame extends JFrame
    {
     
    JTextField box1, box2;
    JButton Zero, C, Exclemation, Equals, One, Two, Three, Multiply, Four, Five, Six, Minus, Seven, Eight, Nine, Plus;
    // GUI components
    // State of calculator
     
    public BlackFrame ()
    {
     
    JFrame frame = new JFrame("Black (DC)");
    frame.setSize(200, 250);
    frame.setVisible(true);
     
     
    JPanel a = new JPanel();
    frame.add(a);
    a.setVisible(true);
     
    JPanel b = new JPanel();
    frame.add(b);
    b.setVisible(true);
     
    JPanel c = new JPanel();
    frame.add(c);
    c.setVisible(true);
     
    box1 = new JTextField (10);
    a.add(box1);
     
     
    c.setLayout(new GridLayout(4, 1));
     
     
    Zero = new JButton("0");
    c.add(Zero);
    C = new JButton("C");
    c.add(C);
    Exclemation = new JButton("!");
    c.add(Exclemation);
    Equals = new JButton("=");
    c.add(Equals);
    One = new JButton("1");
    c.add(One);
    Two = new JButton("2");
    c.add(Two);
    Three = new JButton("3");
    c.add(Three);
    Multiply = new JButton("*");
    c.add(Multiply);
    Four = new JButton("4");
    c.add(Four);
    Five = new JButton("5");
    c.add(Five);
    Six = new JButton("6");
    c.add(Six);
    Minus = new JButton("-");
    c.add(Minus);
    Seven = new JButton("7");
    c.add(Seven);
    Eight = new JButton("8");
    c.add(Eight);
    Nine = new JButton("9");
    c.add(Nine);
    Plus = new JButton("+");
    c.add(Plus);
     
     
    box2 = new JTextField (10);
    b.add(box2);
     
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    }
    }

    Edit/Delete Message


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Calculator layout problem

    For future reference, please use the code tags.

    The following link might help with your layout: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Calculator layout problem

    Check out BorderLayout. Your first text box panel can go NORTH, your grid panel CENTER and you second text box panel SOUTH.

    A couple of other things - your BlackFrame class extends JFrame but doesn't display itself, it creates another frame to display. Either do one or the other, not both - you'll end up very confused at some point (e.g. already you're setting the default close operation on the BlackFrame instead of the internal frame you're actually displaying!). Also, don't set the frame visible until all the components are in place. That way you don't need to make each panel visible separately, the frame will tell all its contents to be visible for you. Plus you should call frame.pack() before setting it visible - this tells it to lay out it's display components properly.

Similar Threads

  1. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  2. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  3. [SOLVED] Another Calculator Problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 4th, 2010, 03:31 PM
  4. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM
  5. problem in managing layout
    By namreen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 12:52 AM