Search code examples
javaswinguser-interfacelayout-managerborder-layout

BorderLayout() doesn't seem to be working. JButtons won't go to SOUTH


As the title says, a panel with JButtons in it stays north for some reason. Here's the code which I think is relevant.

    f = new JFrame();
    f.setTitle("Book Quiz");
    f.setSize(800, 400);
    f.setLocation(400, 250);
    f.setResizable(false);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);

    card = new JPanel();
    cardLayout = new CardLayout();
    card.setLayout(cardLayout);

    takeQuizCard = new JPanel();
    takeQuizCard.setLayout(new BorderLayout());

    quizButtons = new JPanel();
    submit = new JButton("Submit Answer");
    next = new JButton("Next");
    quizDone = new JButton("Done");
    quizDone.addActionListener(this);
    quizQuit = new JButton("Quit");
    quizQuit.addActionListener(this);
    quizButtons.setLayout(new FlowLayout());
    quizButtons.add(submit);
    quizButtons.add(next);
    quizButtons.add(quizDone);
    quizButtons.add(quizQuit);
    takeQuizCard.add(quizButtons, BorderLayout.SOUTH);
    quizInfo = new JPanel(new GridLayout(0, 1));

    card.add(takeQuizCard, TAKE_QUIZ_CARD);

    takeQuizCard.add(quizButtons);

    f.add(card);

There are also 4 radio buttons and two labels on the WEST part. I left it out so it doesn't distract anyone, but if it is relevant, I'll add it. Anyone have any ideas? I have another 'card' in my program that works properly, and everything seems to be the same code-wise.


Solution

  • You are re-adding quizButtons on the second last line. It is overriding your previous placement of SOUTH.

    Remove:

    takeQuizCard.add(quizButtons);
    

    And keep:

    takeQuizCard.add(quizButtons, BorderLayout.SOUTH);