I want to change extend Canvas
to JFrame
. But why does my program not run? Command prompt reports:
Exception in thread "main" java.langIllegalArgumentException:
adding a window to a container
at java.awt.Container.chekNotAWindow(Container.java:483)" and so many more
Here is my code:
class Layar extends Canvas implements Runnable,KeyListener
\\ init
Layar()
{
super();
try{
//adding a picture
}
catch(Exception e){}
new Thread(this).start();
addKeyListener(this);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(img,x_back,y_back+200,null);//background
//key listener
public void run()
{
//try catch
}
public class stage2
{
public static void main(String[] args)
{
JFrame window = new JFrame("aaaa");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(50,50);
window.setSize(700,700);
window.setResizable(false);
window.add(new Layar());
window.setVisible(true);
}
}