Ryan Jackson
Ryan Jackson

Reputation: 47

Java BorderLayout not working, JPanels will not move the way I want them to

For some reason the border layout in my code is not working, when I run the program the two JPanel components look like this when I want the two white squares to be at the top next to each other.

I am currently building a program where I'll add 9 squares (panels) then place images into them, so it's 3 by 3 next to my button panel. However I've just started off with two squares and I can't even position them correctly, they're fixed in the center and when I add another JPanel it automatically goes next to the two I've already put in the center.

I'm happy with where my button panel is but I want my other two panels to go at the top next to each other so I can continue to build my program.

How to create the required layout & alignment of the image panels?

MAIN CLASS

public static void main(String[] args) {
        JFrame application = new JFrame ("Call of Traders");

        GUI graphicalInterface = new GUI();  
        application.add(graphicalInterface);   

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        application.setLocation(200,200);
        application.pack();
        application.setVisible(true); 
        application.setResizable(false);
    }
}

SUB CLASS

public class GUI extends JPanel implements ActionListener {

    private JButton AddChairBTN = new JButton();
    private JButton AddTableBTN = new JButton();
    private JButton AddDeskBTN = new JButton();
    private JButton NewCalcBTN = new JButton();
    private JPanel buttonPanel;
    private JPanel imagePanel;
    private JPanel imagePanel2;
    private Chair customerChair = new Chair();
    private Table customerTable = new Table();
    private Desk customerDesk = new Desk();

    GUI() {
        //create button panel
        buttonPanel = new JPanel();
        buttonPanel.setPreferredSize(new Dimension(100, 300));
        buttonPanel.setOpaque(true);
        buttonPanel.setBackground(Color.white);
        imagePanel = new JPanel(new BorderLayout());
        imagePanel.setPreferredSize(new Dimension(100, 100));
        imagePanel.setOpaque(true);
        imagePanel.setBackground(Color.white);
        imagePanel2 = new JPanel(new BorderLayout());
        imagePanel2.setPreferredSize(new Dimension(100, 100));
        imagePanel2.setOpaque(true);
        imagePanel2.setBackground(Color.white);

        AddChairBTN = new JButton();
        //add action listener to each button
        AddChairBTN.addActionListener(this);
        //set button size
        AddChairBTN.setPreferredSize(new Dimension(100, 50));
        //set text for each button
        AddChairBTN.setText("Add Chair");
        AddChairBTN.setToolTipText("press to add a Chair");
        //add buttons to gui
        buttonPanel.add(AddChairBTN);

        AddTableBTN = new JButton();
        //add action listener to each button
        AddTableBTN.addActionListener(this);
        //set button size
        AddTableBTN.setPreferredSize(new Dimension(100, 50));
        //set text for each button
        AddTableBTN.setText("Add Table");
        AddTableBTN.setToolTipText("press to add a Table");
        //add buttons to gui
        buttonPanel.add(AddTableBTN);

        AddDeskBTN = new JButton();
        //add action listener to each button
        AddDeskBTN.addActionListener(this);
        //set button size
        AddDeskBTN.setPreferredSize(new Dimension(100, 50));
        //set text for each button
        AddDeskBTN.setText("Add Desk");
        AddDeskBTN.setToolTipText("press to add a Desk");
        //add buttons to gui
        buttonPanel.add(AddDeskBTN);

        NewCalcBTN = new JButton();
        //add action listener to each button
        NewCalcBTN.addActionListener(this);
        //set button size
        NewCalcBTN.setPreferredSize(new Dimension(100, 50));
        //set text for each button
        NewCalcBTN.setText("Calculate");
        NewCalcBTN.setToolTipText("press to find out the total");
        //add buttons to gui
        buttonPanel.add(NewCalcBTN);

        //Add all panels to main containter panel and add that to the window  
        this.add(buttonPanel);
        this.add(imagePanel, BorderLayout.NORTH);
        this.add(imagePanel2, BorderLayout.SOUTH);
    }
}

Upvotes: 0

Views: 180

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

By the looks of it, you wish to achieve something like this:

I'd use a combination of layouts as described in the titled borders below. Or to be more specific, the titled borders below, indicate the combination of layouts used to achieve the layout seen above.

enter image description here

Upvotes: 2

Related Questions