Why do I get this error? --> Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
It's ridiculous... I'm just adding a JPanel to a JFrame..or?
Class with JPanel
import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class Stapeldiagram extends JPanel { public Stapeldiagram() { this.setBackground(Color.white); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.fill3DRect(35, 225-150, 40, 150, true); g.draw3DRect(35,225-150,40,150,false); g.setColor(Color.green); g.fill3DRect(75, 225-100, 40, 100, true); g.draw3DRect(75, 225-100, 40, 100,false); g.setColor(Color.blue); g.fill3DRect(115, 225-190, 40, 190, true); g.draw3DRect(115, 225-190, 40,190,false); } }//end class
Class to show the JPanel
import java.awt.Color; import java.awt.FlowLayout; import javax.swing.JFrame; public class VisaStapel extends JFrame { Stapeldiagram stip1; public VisaStapel() { stip1 = new Stapeldiagram(); add(stip1); this.setBackground(Color.white); } public static void main(String[] args) { JFrame f = new JFrame(); f.setSize(400, 300); f.setLocation(100,100); f.setTitle("Min JPanel!"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); VisaStapel p = new VisaStapel ();//anropar min JPanel f.add(p); f.setVisible(true); } }