有人可以告诉我有什么问题吗?Camera 类扩展了 Jpanel
编码:
public class Main extends JFrame {
public static Image image;
//sort the cameras by their heights.
public static void main(String [] args){
image = new Image(400,400,"test");
Camera c=new Camera(100, 100, (Math.PI)/4, 0, 200,200,Math.PI,Color.MAGENTA);
image.addCamera(c);
JFrame f = new JFrame();
int width= image.getWidth();
int length = image.getLength();
f.setSize(width, length);
f.add(new Main());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Sets the title of the frame
f.setTitle(image.getName());
f.setVisible(true);
}
public void paint(Graphics g) {
System.out.println("in the paint");
Vector<Camera> cameras = image.getCameras();
for(int i=0;i<cameras.size();i++){
cameras.get(i).paintComponent(g);
}
`enter code here`}
在Camera类中有函数paintCompoment,但结果是:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:483)
at java.awt.Container.addImpl(Container.java:1084)
at java.awt.Container.add(Container.java:998)
at javax.swing.JFrame.addImpl(JFrame.java:562)
at java.awt.Container.add(Container.java:410)
at CameraPack.Main.main(Main.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)