Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container --- What am I doing wrong? -
this question has answer here:
i have been having problem following:
package keyz; import java.awt.color; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import javax.swing.jframe; public class keypractise extends jframe{ public static int x; public static int z; public static float hue; public static void main(string[] args) { // todo auto-generated method stub jframe window = new jframe("color square"); keypractise content = new keypractise((window.getwidth() / 2), window.getheight() / 2); window.setcontentpane(content); window.setsize(600,600); window.setlocation(400,400); window.setdefaultcloseoperation( jframe.exit_on_close ); window.setvisible(true); content.requestfocusinwindow(); content.setbackground(color.white); } public keypractise (int xx, int zz){ xx = x; zz = z; listener listener = new listener(); addkeylistener(listener); } private class listener implements keylistener{ @override public void keypressed(keyevent evt) { // todo auto-generated method stub int key = evt.getkeycode(); if (key == keyevent.vk_up){ if ((hue + 0.00392) < 1.00000){ hue += 0.00392; } if ((hue + 0.00392) >= 1.00000){ hue -= hue; } repaint(); } }//keypressed @override public void keyreleased(keyevent evt) { // todo auto-generated method stub } @override public void keytyped(keyevent evt) { // todo auto-generated method stub } public void paintcomponent(graphics g) { g.setcolor(color.gethsbcolor(hue, 1.0f, 1.0f)); g.fillrect(x, z, 50, 50); } } }
i exception:
exception in thread "main" java.lang.illegalargumentexception: adding window container
i believe problem "adding window container." if so, other way can done avoid scenario?
also, told proper style add setbackground(color);
method keypractise
object constructor , add super.paintcomponent(g);
paintcomponent()
rather did. when error in eclipse:
the method paintcomponent(graphics) undefined type object
is bad use content.setbackground()
in main subroutine?
thanks!
i have changed below line
public class keypractise extends jframe{
to
public class keypractise extends container{
and working now... please check
Comments
Post a Comment