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 5 of 5

Thread: My JPanels don's stack the way I expect them to

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

    Default My JPanels don's stack the way I expect them to

    This is my code:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    class BlackjackGUI {
    JButton hit;
    JButton stand;

    public static void main (String[] args) {

    BlackjackGUI blackjack = new BlackjackGUI();
    blackjack.go();
    }

    public void go() {
    JFrame frame = new JFrame();
    frame.setSize(500,500);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JPanel tophalf = new JPanel();
    JPanel bottomhalf = new JPanel();
    JLabel guititle = new JLabel (" Blackjack V1.0 ");
    JPanel dealerhandtitle = new JPanel ();
    JPanel dealerhandcards = new JPanel ();
    JPanel dealerside = new JPanel();
    JPanel playerside = new JPanel();
    JPanel playerhandtitle = new JPanel ();
    JPanel playerdealerarea = new JPanel ();
    JPanel playerhandcards = new JPanel ();
    JPanel hitstand = new JPanel ();
    JButton Hit = new JButton(" Hit ");
    JButton Stand = new JButton("Stand");
    JLabel dealerhandtitletext = new JLabel ("Dealer's Hand");
    JLabel dealerhandcardstext = new JLabel ("Dealer's Cards");
    JLabel playerhandtitletext = new JLabel ("Player's Hand");
    JLabel playerhandcardstext = new JLabel ("Player's Cards");


    frame.getContentPane() .add(BorderLayout.NORTH, tophalf);
    frame.getContentPane() .add(BorderLayout.SOUTH, bottomhalf);
    tophalf.setBackground(Color.white);
    bottomhalf.setBackground(Color.darkGray);
    tophalf.add(guititle);
    bottomhalf.add(BorderLayout.WEST, Hit);
    bottomhalf.add(BorderLayout.EAST, Stand);
    frame.getContentPane() .add(BorderLayout.CENTER, playerdealerarea);
    playerdealerarea.add(BorderLayout.NORTH, dealerside);
    playerdealerarea.add(BorderLayout.SOUTH, playerside);
    dealerside.add(BorderLayout.NORTH, dealerhandtitle);
    dealerside.add(BorderLayout.SOUTH, dealerhandcards);
    playerside.add(BorderLayout.NORTH, playerhandcards);
    playerside.add(BorderLayout.SOUTH, playerhandtitle);
    dealerhandtitle.add(dealerhandtitletext);
    dealerhandcards.add(dealerhandcardstext);
    playerhandtitle.add(playerhandtitletext);
    playerhandcards.add(playerhandcardstext);


    frame.setVisible(true);
    }

    }

    I am trying to stack my panels vertically to eventually make a blackjack game. Could someone please explain to me why the panels are stacking sideways rather than vertically? Thank you so much for the help.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 601 Times in 516 Posts

    Default Re: My JPanels don's stack the way I expect them to

    What exactly did you expect it to look like?

    I suggest you create an SSCCE with a single JPanel and a couple JLabels, because it's a bit difficult to separate your actual problem from the rest of your layout code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 601 Times in 516 Posts

    Default Re: My JPanels don's stack the way I expect them to

    What exactly did you expect it to look like?

    I suggest you create an SSCCE with a single JPanel and a couple JLabels, because it's a bit difficult to separate your actual problem from the rest of your layout code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My JPanels don's stack the way I expect them to

    Okay. Sorry, I was really tired.

    Basically I am hoping to have the panels stack vertically. I want four panels, one on top of the other. I tried to put a panel in the NORTH region of my JFrame and one in the SOUTH region of my JFrame. This worked. I then wanted to break the CENTER region into multiple panels. I added a JPanel to the CENTER using "frame.getContentPane() .add(BorderLayout.CENTER, centerarea);" where 'frame' is my JFrame and 'centerarea' is my JPanel. This seems to work as well. However, when I try to add a panel to the NORTH section and another to the SOUTH section [using -> centerarea.add(BorderLayout.SOUTH, thirdpanel); ], they seem to end up side by side rather than one on top of the other as I expected. Is this slightly clearer? This is the essence of my problem: I want to put two JPanels in another JPanel and have them be on top of each other.

    JFrame frame = new JFrame();
    JPanel centerarea = new JPanel();
    JPanel bottom = new JPanel();
    JPanel top = new JPanel();

    frame.getContentPane() .add(BorderLayout.NORTH, top);
    frame.getContentPane() .add(BorderLayout.SOUTH, bottom);

    frame.getContentPane() .add(BorderLayout.CENTER, centerarea);

    JPanel secondpanel = new JPanel();
    JPanel thirdpanel = new JPanel();

    centerarea.add(BorderLayout.NORTH, secondpanel);
    centerarea.add(BorderLayout.SOUTH, thirdpanel);



    I am hoping to end up with this:

    _______________
    |......TOP...........|
    |______________|
    | SECONDPANEL..|
    |______________|
    | THIRDPANEL.....|
    |______________|
    | BOTTOM..........|
    |______________|

    (The ... are just placeholders)
    Last edited by amcqueen; September 14th, 2011 at 03:30 PM.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 601 Times in 516 Posts

    Default Re: My JPanels don's stack the way I expect them to

    You'd probably be better off using a BoxLayout. Recommended reading: A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    But the reason it's not working the way you expect is that JPanels' default layout is FlowLayout, not BorderLayout. The content pane is a special case that automatically gets a BorderLayout. To do it your way, you'd have to set the layout to a BorderLayout first. But like I said, BoxLayout is your friend here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Need Expect Help and Advice
    By Addo kofi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 29th, 2011, 08:41 PM
  2. Resizing JPanels in JScrollPanes
    By aussiemcgr in forum AWT / Java Swing
    Replies: 4
    Last Post: November 29th, 2010, 01:13 PM
  3. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  4. [SOLVED] Dynamic numbber of JPanels
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 14th, 2010, 02:44 AM
  5. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM