(java.lang.IllegalArgumentException) Error with gui -
i making simple gui program in java. when click run gives me error looks this:
exception in thread "awt-eventqueue-0" java.lang.illegalargumentexception: adding window container @ java.awt.container.checknotawindow(unknown source) @ java.awt.container.addimpl(unknown source) @ javax.swing.jlayeredpane.addimpl(unknown source) @ java.awt.container.add(unknown source) @ javax.swing.jrootpane.setcontentpane(unknown source) @ javax.swing.jframe.setcontentpane(unknown source) @ main.cool(main.java:31) @ main$1.run(main.java:43) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$500(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
and here code:
import javax.swing.*; import java.awt.container; import java.awt.event.*; public class main extends jframe implements actionlistener { protected jbutton click, fun; public main() { click = new jbutton("click"); click.setactioncommand("click"); click.addactionlistener(this); add(click); click.setsize(16, 16); fun = new jbutton("wow"); fun.setactioncommand("wow"); fun.addactionlistener(this); add(fun); } public static void cool() { jframe frame = new jframe("test!"); frame.setdefaultcloseoperation(jframe.exit_on_close); main main = new main(); frame.setcontentpane(main); frame.setsize(128, 128); frame.setvisible(true); frame.setresizable(true); } public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { cool(); } }); } public void actionperformed(actionevent e) { // todo auto-generated method stub if("click".equals(e.getactioncommand())) { system.out.println("oh right"); } else if ("wow".equals(e.getactioncommand())) { system.out.println("hi"); } } }
i believe bug may in cool() method; setcontentpane line. not sure. can please me.
yes; bug triggered in frame.setcontentpanel()
in cool()
method in main
class. main
class should extend jpanel
instead of extend jframe
.
aside: avoid declaring local variables match class names; and, avoid declaring class names same standard method names...so, rename main
class main
(if must, try more descriptive...perhaps application
), , make local variable name other main
...perhaps m
or application
.
Comments
Post a Comment