【问题标题】:BoxLayout can't be shared error, trying to fit two JPanels in a JFrame [duplicate]BoxLayout can't be shared 错误,试图在 JFrame 中安装两个 JPanel [重复]
【发布时间】:2019-01-10 10:25:24
【问题描述】:

尝试在一个框架中放置 2 个面板,一个位于顶部的小面板,一个填满框架的其余部分。但是,此代码返回“无法共享 BoxLayout”错误。

        JFrame frame = new JFrame("Clients");
        frame.setSize(1000,900);

        JPanel sorters = new JPanel();
        sorters.setSize(1000, 100);
        frame.getContentPane().add(sorters);

        JPanel rowPane = new JPanel();
        JScrollPane scrPane = new JScrollPane(rowPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrPane.setSize(1000, 800);
        frame.getContentPane().add(scrPane);

        frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));

【问题讨论】:

标签: java swing jframe jpanel


【解决方案1】:

BoxLayout 必须将其应用到的实际容器作为第一个参数,在您的情况下,容器是框架的 contentPane:

frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

【讨论】:

    猜你喜欢
    • 2014-10-28
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2014-04-12
    相关资源
    最近更新 更多