I can't figure out why this won't put buttons 1, 2, and 3, on the top of the frame, with buttons 4, 5 and 6 on the bottom? I realize there should be space between the top row and the bottom row.

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;


public class layoutExample 
{
    public static void main(String[] args)
    {

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout(20, 40));




        JButton jn1 = new JButton("Button 1");
        JButton jn2 = new JButton("Button 2");
        JButton jn3 = new JButton("Button 3");

        JPanel panelCenter = new JPanel();
        panelCenter.add(jn1);
        panelCenter.add(jn2);
        panelCenter.add(jn3);
        frame.add(panelCenter, BorderLayout.CENTER);



        JButton jn4 = new JButton("Button 4");
        JButton jn5 = new JButton("Button 5");
        JButton jn6 = new JButton("Button 6");


        JPanel panelCenter2 = new JPanel();
        panelCenter.add(jn4);
        panelCenter.add(jn5);
        panelCenter.add(jn6);
        frame.add(panelCenter2, BorderLayout.SOUTH);

        frame.setSize(570, 80);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

Recommended Answers

All 2 Replies

You are declared panelCenter2, but you still add buttons to panelCenter.

commented: Spot on! +5

As quuba said, you have declared panelCenter2 but you are still adding the JButtons jn4, jn5 and jn6 to panelCentre1 - which is positioned using BorderLayout.CENTER. Hence why your last three buttons are not positioned in the SOUTH. One of the pitfalls of "copy and paste" ! P.S. also, please use tags - it makes reading code so much easier.[CODE=java] tags - it makes reading code so much easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.