简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM