Container.add上的IllegalArgumentException

时间:2014-08-07 01:54:43

标签: java awt

我创建了一个程序,它将有4个类+一个摘要,它将给出颜色,价格,类型和选项 这些课程工作正常,但主要的是给我一些问题。从错误读取的方式看起来我已经添加了一个窗口到容器或东西,但我没有看到/理解我的错误。

这是我的主要类的代码:

public class VehiclePanelProgram extends JFrame
{
    VehicleColorPanel    vcp = new VehicleColorPanel();
    VehicleOptionsPanel  vop = new VehicleOptionsPanel(this);
    VehicleTypePanel     vtp = new VehicleTypePanel(this);
    VehiclePricePanel    vpp = new VehiclePricePanel();

    //It will show a summary of data from the other panels
    JPanel summaryPanel = new JPanel();
    JButton summaryButton = new JButton("Summary");

    public VehiclePanelProgram()
    {
        Container canvas = getContentPane();
        canvas.setLayout( new BorderLayout());

        canvas.add(vcp,BorderLayout.CENTER);//Line 23
        canvas.add(vop,BorderLayout.EAST);
        canvas.add(vpp,BorderLayout.NORTH);
        canvas.add(vtp,BorderLayout.WEST);

        fixUpSummaryPanel();
        canvas.add(summaryPanel, BorderLayout.SOUTH);
        this.setSize(600, 350);
        this.setTitle(" Vehicle Panel Program ");
        this.show();   
    }//End Program constructor

    public void fixUpSummaryPanel()
    {
        summaryPanel.setLayout( new FlowLayout());
        summaryPanel.add(summaryButton);

        summaryButton.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent ae)
            {

            }//End Action Constructor
        });//End ActionListener
    }//End SummaryPanel Constructor

    public void disableSunroof(){vop.disSunroof();}
    public void enableSunroof(){vop.enSunroof();}
    public void disableConvertible(){vtp.disCon();}
    public void enableConvertible(){vtp.enCon();}

    public static void main(String args[]) 
    {
        VehiclePanelProgram app = new VehiclePanelProgram();//Line 58
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }//End Main

}//End VehiclePanelProgram

这是我不断得到的输出:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Container.java:488)
    at java.awt.Container.addImpl(Container.java:1089)
    at java.awt.Container.add(Container.java:971)
    at vehiclepanelprogram.VehiclePanelProgram.<init>(VehiclePanelProgram.java:23)
    at vehiclepanelprogram.VehiclePanelProgram.main(VehiclePanelProgram.java:58)

如果需要,我可以为您提供其他课程。

1 个答案:

答案 0 :(得分:0)

问题是你的类继承自Window,它是一个顶级容器,但你试图将它添加到另一个顶级容器(canvas)。你不被允许这样做。