Search code examples
javaborder-layout

2 Panels added to Frame are one on top of the other


I am trying to add to JPanels to JFrame but the second one just go on top of the first one and I can't seem to understand why or how to fix it.

Below is the UI that I try to add. Thanks a lot,

Rotem

private static void createAndShowGUI() {
    JFrame f = new JFrame("Maman 13 - Part 2");
    f.setLayout(new BorderLayout());
    //f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout alignLeftLayout = new FlowLayout(FlowLayout.LEFT, 5, 5);
    // first row
    Hashtable<Integer, JButton> hashTable = new Hashtable<Integer, JButton>();
    LinkedHashMap<String, Integer> buttons = new LinkedHashMap<String, Integer>();
    addFirstRow(buttons);
    JPanel firstRow = new KeyboardRow(hashTable, buttons);
    firstRow.setLayout(alignLeftLayout);
    f.add(firstRow);
    // second row
    buttons = new LinkedHashMap<String, Integer>();
    addSecondRow(buttons);
    JPanel secondRow = new KeyboardRow(hashTable, buttons);
    secondRow.setLayout(alignLeftLayout);
    f.add(secondRow);

    f.pack();
    f.setVisible(true);
}

Solution

  • Instead of

    f.setLayout(new BorderLayout());
    

    try

    f.setLayout(new GridLayout());