子面板中 BoxLayout 的问题

Issues with BoxLayout in a subpanel

我正在为一家模拟披萨公司编写程序,当我 运行 我的代码时,我得到了 BoxLayout cannot be shared 错误。我是布局管理器的新手,之前只使用过 Flow。

当时我正在使用这种方法,我在下面包含了 class 的其余部分,但如果您需要查看更多内容,请告诉我。

/***** Pizza Method *****/
private JPanel Pizza()   {

    /*Creates Pizza Panel*/
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    JPanel center = new JPanel();
    center.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    /*Buffer Panel*/
    JPanel buffer = new JPanel();
    buffer.setPreferredSize(new Dimension(20, 30));

    /*Title*/
    JLabel title = new JLabel("Signature Pizzas: ");

    /*** Pizzas ***/
    JLabel pepperoni = new JLabel("Pepperoni = Good Ol' Cheese and Pepperoni ----- ");
    JLabel allAmerican = new JLabel("All American = Pepperoni, Ground Beef,"
            + " Ham, and Bacon with American Cheese ----- ");

    /*** Add Components ***/
    center.add(pepperoni);
    center.add(Box.createVerticalGlue());
    center.add(allAmerican);

    panel.add(title, BorderLayout.NORTH);
    panel.add(buffer, BorderLayout.WEST);
    panel.add(new JPanel(), BorderLayout.SOUTH);
    panel.add(center, BorderLayout.CENTER);

    return panel;
}

这是Class

package pizza;

import java.awt.*;
import javax.swing.*;

public class SigDishPanel extends JPanel {

    public SigDishPanel()   {
        /*** Set Defaults***/
        setLayout(new GridLayout(2, 1));
        setBackground(Color.ORANGE);

        /*** Adds Pizza and Drinks Panels ***/
        add(Pizza());
        add(Drinks());
    }



    /***** Pizza Method *****/
    private JPanel Pizza()   {

        /*Creates Pizza Panel*/
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JPanel center = new JPanel();
        center.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        /*Buffer Panel*/
        JPanel buffer = new JPanel();
        buffer.setPreferredSize(new Dimension(20, 30));

        /*Title*/
        JLabel title = new JLabel("Signature Pizzas: ");

        /*** Pizzas ***/
        JLabel pepperoni = new JLabel("Pepperoni = Good Ol' Cheese and Pepperoni ----- ");
        JLabel allAmerican = new JLabel("All American = Pepperoni, Ground Beef,"
                + " Ham, and Bacon with American Cheese ----- ");

        /*** Add Components ***/
        center.add(pepperoni);
        center.add(Box.createVerticalGlue());
        center.add(allAmerican);

        panel.add(title, BorderLayout.NORTH);
        panel.add(buffer, BorderLayout.WEST);
        panel.add(new JPanel(), BorderLayout.SOUTH);
        panel.add(center, BorderLayout.CENTER);

        return panel;
    }



    /***** Drinks Method *****/
    private JPanel Drinks()   {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JPanel buffer = new JPanel();
        buffer.setPreferredSize(new Dimension(20, 30));

        JLabel title = new JLabel("Signature Beverages: ");

        /*** Drinks ***/
        JLabel bearBrew = new JLabel("Bear Claw Brew ----- ");

        /*** Add Components ***/
        panel.add(title, BorderLayout.NORTH);
        panel.add(buffer, BorderLayout.WEST);

        panel.add(bearBrew, BorderLayout.CENTER);

        return panel;
    }
}

您正在使用 BoxLayout 设置中心的布局,因此需要将其传递给 BoxLayout 构造函数。所以不是这个:

center.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

而是这个:

center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));

补充一下...我注意到 BoxLayout 需要一个已初始化的目标。 this,在构造函数中调用时 未完全初始化,因此下面与上面给出的 example/answer 相关的代码将产生同样的错误。

JPanel center = new JPanel(new BoxLayout(center, BoxLayout.Y_AXIS));

请注意,在这种情况下使用了 center,但是它失败了,因为它在构造函数中被调用,因此上面的解决方案。