At the first compilation, the following error appeared:

The selection cannot be lauched, and are no recent launche.

After solving it and rebuilding, the following stack trace was issued:

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 javax.swing.JLayeredPane.addImpl(JLayeredPane.java:231) at java.awt.Container.add(Container.java:973) at javax.swing.JRootPane.setContentPane(JRootPane.java:626) at javax.swing.JFrame.setContentPane(JFrame.java:698) at ru.bubblesShooter.GameStart.main(GameStart.java:12) 

How can this be caused?

Source:

 package ru.bubblesShooter; import javax.swing.JFrame; public class GameStart { public static void main(String[] args) { GamePanel panel = new GamePanel(); JFrame startFrame = new JFrame(); startFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); startFrame.setContentPane(panel); startFrame.pack(); startFrame.setLocationRelativeTo(null); startFrame.setVisible(true); panel.start(); } } package ru.bubblesShooter; import javax.swing.JFrame; import java.awt.*; import java.awt.image.BufferedImage; public class GamePanel extends JFrame implements Runnable{ // Fields public static int WIDTH = 400; public static int HEIGHT = 400; private Thread thread; private BufferedImage image; private Graphics2D g; private GameBack background; // Constructor public GamePanel() { super(); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setFocusable(true); requestFocus(); } public void start() { thread = new Thread(this); thread.start(); } // Methods @Override public void run() { image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); g =(Graphics2D) image.getGraphics(); background = new GameBack(); while(true) { // TODO States gameUpdate(); gameRender(); gameDraw(); try { thread.sleep(33); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO FPS } } public void gameUpdate() { // Background update background.update(); } public void gameRender() { // Background draw background.draw(g); } private void gameDraw() { Graphics g2 = this.getGraphics(); g2.drawImage(image, 0, 0, null); // GC g2.dispose(); } } package ru.bubblesShooter; import java.awt.*; public class GameBack { // Fields private Color color; // Constructor public GameBack() { color = Color.BLUE; } // Methods public void update() { } public void draw(Graphics2D g) { g.setColor(color); g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT); } } 

    1 answer 1

    The text of the error in general explains everything. You are trying to add something to the container that is a window ( java.awt.Window ), and this is prohibited (I suppose windows are designed as top-level containers). Your GamePanel class inherits from JFrame , and that from Window , so GamePanel cannot be used for JFrame.setContentPane() .

    You need to inherit a GamePanel from something else, for example, from JPanel .

    • Thanks for the help. Found a mistake, now everything is working fine. The error is not much in the place where you spoke, but I am essentially the same. Invalid inheritance. - Andrey Kulik
    • I can not answer the answer, because rating is not enough. - Andrey Kulik
    • @ AndreiKulik What was the mistake? - Roman
    • I inherited from JFrame, and it was necessary from JPanel, well, and there all rolled because of this. - Andrey Kulik
    • @ AndreiKulik Umm ... I kind of wrote in the answer, and you said that the error was not in the place where I indicated. - Roman