简体   繁体   English

BorderLayout()似乎不起作用。 JButton不会去南方

[英]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. 如标题所示,由于某些原因,其中装有JButtons的面板仍向北。 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. WEST部件上还有4个单选按钮和两个标签。 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. 您要在倒数第二行重新添加quizButtons It is overriding your previous placement of SOUTH . 它覆盖了您以前放置SOUTH位置。

Remove: 去掉:

takeQuizCard.add(quizButtons);

And keep: 并保持:

takeQuizCard.add(quizButtons, BorderLayout.SOUTH);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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