Results 1 to 3 of 3
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default Help with overlapping JPanels

    I am trying to write a calculator for my final and am having trouble with my 2 panels(panel of numbers and operations, panel for output) overlapping. Am I just not correctly adding them to the frame or am I missing something with GridLayout or what? Here is the code I'm having issues with

    Java Code:
    JFrame frame = new JFrame("Calculator");
    TextField textField = new TextField();
    JPanel outputPanel = new JPanel(new GridLayout(1,4));
    outputPanel.setSize(300,20);
    		
    textField.setSize(300, 20);
    textField.setEditable(false);
    textField.setText("0");
    		
    outputPanel.add(textField);
    frame.add(outputPanel);
    
    addButtons(frame);

    //AddButtons method
    Java Code:
    JPanel inputPanel = new JPanel(new GridLayout(4,4,5,5));
    
    JButton button7 = new JButton("7");
    button7.setText("7");
    inputPanel.add(button7);		
    //...rest of buttons
    		
    frame.add(inputPanel);
    outputPanel rests on top of my top 4 buttons which is also confusing me. I thought that since inputPanel was added after outputPanel that inputPanel would rest on top but it doesn't. Any advice on what I'm doing wrong?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    The JFrames contentPane uses BorderLayout per default, and simply calling add(Component) on a BorderLayout container is the same as calling add(Component, BorderLayout.CENTER). So, you are adding both panels to the same spot. You need to call add(Component, BorderLayout.North) or SOUTH, or EAST, or WEST, for one of the components.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 02:11 PM
  2. Overlapping Components using Netbeans GUI Interface
    By hitmen in forum Java Applets
    Replies: 3
    Last Post: 03-06-2009, 05:33 AM
  3. overlapping forms
    By keerthi_y19 in forum New To Java
    Replies: 4
    Last Post: 08-26-2008, 10:30 PM
  4. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 12:32 AM
  5. How to add Images to JPanels?
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •