1、错误描述
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:490)
at java.awt.Container.addImpl(Container.java:1091)
at java.awt.Container.add(Container.java:1005)
at javax.swing.JFrame.addImpl(JFrame.java:567)
at java.awt.Container.add(Container.java:417)
at com.you.cmdp.frame.CheckBox.<init>(CheckBox.java:42)
at com.you.cmdp.frame.CheckBox.main(CheckBox.java:62)
2、错误原因
public CheckBox()
{
frame.setBounds(100, 100, 400, 300);
frame.setFont(font);
frame.setForeground(Color.WHITE);
frame.setBackground(Color.BLACK);
add(frame);
cbOne.setText("第一节");
cbTwo.setText("第二节");
cbThree.setText("第三节");
cbFour.setText("第四节");
panel.add(cbOne);
panel.add(cbTwo);
panel.add(cbThree);
panel.add(cbFour);
frame.add(panel);
}
构造函数中,继承了JFrame,但是不会再次添加add(frame)3、解决办法
public CheckBox()
{
frame.setBounds(100, 100, 400, 300);
frame.setFont(font);
frame.setForeground(Color.WHITE);
frame.setBackground(Color.BLACK);
//add(frame);
cbOne.setText("第一节");
cbTwo.setText("第二节");
cbThree.setText("第三节");
cbFour.setText("第四节");
panel.add(cbOne);
panel.add(cbTwo);
panel.add(cbThree);
panel.add(cbFour);
frame.add(panel);
}