Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Ventana.<init>(Ventana.java:16)
at Main.main(Main.java:10)
estos son los codigos de main.java, Ventana.java y Panel.java
Código:
public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Ventana v = new Ventana(); } }
Código:
import java.awt.FlowLayout; import javax.swing.*; public class Ventana extends JFrame { public Ventana(){ super("Graficos"); this.setLayout(null); this.setSize(400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Panel p = new Panel(); add(p); this.setVisible(true); } }
Código:
import java.awt.Color; import java.awt.Graphics; import javax.swing.*; public class Panel extends JFrame { public Panel(){ this.setSize(400,400); } public void paintComponent(Graphics g){ super.paintComponents(g); g.setColor(Color.WHITE); g.fillRect(0, 0, 400, 400); g.setColor(Color.BLACK); g.drawRect(100, 100, 40, 40); } }