java - JFrame not displaying properly -
i haven't done programming while, missing obvious here.
i trying run following code, should create empty jframe
, put in center of screen:
public class maingui { // initilizes main jframe public void maingui() { jframe.setdefaultlookandfeeldecorated(true); jframe frame = new jframe("data deriver"); //frame.setcontentpane(makegui(frame)); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(300,180); frame.setlocationrelativeto(null); frame.setresizable(true); frame.setvisible(true); } public static void main(string[] args) { new maingui(); } }
however, when compile code (using jdk 8.0_45) , run it, nothing happens. no windows open, no errors thrown, literally nothing happens. compiles without error, code should work, nothing being displayed.
i'm not sure wrong.
you creating instance of maingui
class, don't have explicit constructor, after program exits.
you have call maingui()
method creates , displays frame:
public static void main(string[] args) { new maingui().maingui(); }
or can make maingui()
method constructor (which gets called when write new maingui()
) - maybe intention:
public maingui() { jframe.setdefaultlookandfeeldecorated(true); // ...rest of code }
note:
note should swing-related work in edt (event dispatch thread), should run maingui()
in edt this:
public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new maingui(); } }); }
see initial threads more details.
Comments
Post a Comment