子面板中的BoxLayout问题



我正在为一家模拟披萨公司编写一个程序,当我运行代码时,我收到了一个BoxLayout不能共享的错误。我是布局经理的新手,以前只使用过Flow。

当时我正在使用这种方法,我在下面介绍了课程的其余部分,但如果你需要了解更多,请告诉我。

/***** 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 ----- $5");
    JLabel allAmerican = new JLabel("All American = Pepperoni, Ground Beef,"
            + " Ham, and Bacon with American Cheese ----- $10");
    /*** 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;
}

这是类

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 ----- $5");
        JLabel allAmerican = new JLabel("All American = Pepperoni, Ground Beef,"
                + " Ham, and Bacon with American Cheese ----- $10");
        /*** 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 ----- $2");
        /*** 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,当在构造函数中调用时,未完全初始化。因此,下面与上面给出的示例/答案相关的代码将产生相同的错误。

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

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

相关内容

  • 没有找到相关文章

最新更新