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: BorderLayout displays only Center area

  1. #1
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default BorderLayout displays only Center area

    Only the BorderLayout.CENTER displays, and it fills the frame. If I comment out the red center, I get a white center (default I assume) filling the frame. I'm obviously missing something basic, but I don't see it Test1 and Test1_panel are in separate files if that matters.

    Thanx - Charlie

    public class Test1_panel extends JPanel {
    public Test1_panel(Color c) {
    setBackground(c);
    }

    public void setSize(int x, int y) {
    setPreferredSize(new Dimension(x, y));
    }
    }

    class Test1 {
    //----------------------------------------------
    // Here's the main function to get things going.
    //----------------------------------------------
    public static void main(String argv[]) {
    Test1_frame tf = new Test1_frame("The banana boat has arrived.");
    Container p = tf.getContentPane();
    Test1_panel tp1 = new Test1_panel(Color.red);
    Test1_panel tp2 = new Test1_panel(Color.green);
    Test1_panel tp3 = new Test1_panel(Color.blue);
    Test1_panel tp4 = new Test1_panel(Color.yellow);
    Test1_panel tp5 = new Test1_panel(Color.black);
    p.add(tp2, BorderLayout.NORTH);
    p.add(tp1, BorderLayout.CENTER);
    p.add(tp5, BorderLayout.WEST);
    p.add(tp4, BorderLayout.SOUTH);
    p.add(tp3, BorderLayout.EAST);

    tf.showIt(300, 300);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,066
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: BorderLayout displays only Center area

    Please post your code correctly per this link.

  3. #3
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: BorderLayout displays only Center area

    Here's the code again properly formatted (I hope). Thanks Greg Brannon for the link to the formatting stuff

    public class Test1_panel extends JPanel {
        public Test1_panel(Color c) {
            setBackground(c);
        }
     
        public void setSize(int x, int y) {
            setPreferredSize(new Dimension(x, y));
        }
    }
     
    class Test1 {
    //----------------------------------------------
    // Here's the main function to get things going.
    //----------------------------------------------
        public static void main(String argv[]) {
            Test1_frame tf = new Test1_frame("The banana boat has arrived.");
            Container p = tf.getContentPane();
            Test1_panel tp1 = new Test1_panel(Color.red);
            Test1_panel tp2 = new Test1_panel(Color.green);
            Test1_panel tp3 = new Test1_panel(Color.blue);
            Test1_panel tp4 = new Test1_panel(Color.yellow);
            Test1_panel tp5 = new Test1_panel(Color.black);
            p.add(tp2, BorderLayout.NORTH);
            p.add(tp1, BorderLayout.CENTER);
            p.add(tp5, BorderLayout.WEST);
            p.add(tp4, BorderLayout.SOUTH);
            p.add(tp3, BorderLayout.EAST);
     
            tf.showIt(300, 300);
        }
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,066
    My Mood
    Mellow
    Thanks
    184
    Thanked 652 Times in 639 Posts

    Default Re: BorderLayout displays only Center area

    I corrected your code for you, adding comments to explain the corrections. In future, you'll need to show off-stage code, like Test1_frame, in order for us to understand what's happening at your end.
    // GB: added necessary imports the lazy way
    import javax.swing.*;
    import java.awt.*;
     
    // GB: promoted to the public class in the file
    public class Test1
    {
    //----------------------------------------------
    // Here's the main function to get things going.
    //----------------------------------------------
        // GB: in Java, 'functions' are called methods - this
        // is the main() METHOD
        public static void main( String argv[] )
        {
            // i don't know what your Test1_frame is so just used JFrame
            JFrame tf = new JFrame("The banana boat has arrived.");
     
            // GB:  adding to the container has not been necessary since Java 1.4
            // JFrame.add() does the same thing. use an updated tutorial
            // GB: set the preferred size for each of the JPanels
            Container p = tf.getContentPane();
            Test1_panel tp1 = new Test1_panel(Color.red);
            tp1.setPreferredSize( new Dimension( 500, 500 ) );
            Test1_panel tp2 = new Test1_panel(Color.green);
            tp2.setPreferredSize( new Dimension( 400, 50 ) );
            Test1_panel tp3 = new Test1_panel(Color.blue);
            tp3.setPreferredSize( new Dimension( 50, 600 ) );
            Test1_panel tp4 = new Test1_panel(Color.yellow);
            tp4.setPreferredSize( new Dimension( 400, 50 ) );
            Test1_panel tp5 = new Test1_panel(Color.black);
            tp5.setPreferredSize( new Dimension( 50, 600 ) );
     
            p.add(tp2, BorderLayout.NORTH);
            p.add(tp1, BorderLayout.CENTER);
            p.add(tp5, BorderLayout.WEST);
            p.add(tp4, BorderLayout.SOUTH);
            p.add(tp3, BorderLayout.EAST);
     
            // GB: packed the JFrame before rendering to respect
            // preferred size settings
            tf.pack();
     
            // GB: using JFrame's setVisible() method to render
            tf.setVisible( true );
        }
    }
     
    // GB:  demoted to a non-public class.
    class Test1_panel extends JPanel
    {
        public Test1_panel(Color c)
        {
            setBackground(c);
        }
     
        // GB: the setSize() method was not only unused, it is inappropriate
        // to override the setSize() method for this demo
    }

  5. #5
    Junior Member j831526's Avatar
    Join Date
    Jun 2014
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: BorderLayout displays only Center area

    Greg,

    That fixed things, and your code comments were very helpful.

    Thanx - Charlie

Similar Threads

  1. problem with BorderLayout
    By Harry Blargle in forum AWT / Java Swing
    Replies: 5
    Last Post: March 8th, 2014, 07:12 AM
  2. Replies: 5
    Last Post: August 10th, 2013, 03:21 PM
  3. Need help adding Displays to a program
    By Lacie1013 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2013, 11:50 AM
  4. BorderLayout and getHeight/getWidth
    By DOLZero in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 08:59 AM
  5. [SOLVED] JPanel inside CENTER of BorderLayout is given GRID LAYOUT
    By JonLane in forum AWT / Java Swing
    Replies: 9
    Last Post: February 25th, 2012, 11:20 AM