java - Components in second JFrame not showing up -
i want start jframe pressing button in jframe. if press button shows jframe not buttons , sliders in it.
public class mainmenu extends jframe { private jframe klick; private static final long serialversionuid = 9002; public static void main(string[] args) { new mainmenu(); } public mainmenu() { buildgui1(); } public void buildgui1() throws nullpointerexception { settitle("hauptmenü"); setsize(800, 480); setlayout(new gridlayout()); setalwaysontop(false); setlocation((toolkit.getdefaulttoolkit().getscreensize().width)/4, (toolkit.getdefaulttoolkit().getscreensize().height)/4); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); setvisible(true); final jbutton startclickbt = new jbutton("start clicker"); startclickbt.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dispose(); if(klick == null ) { klick =new clicker(); add(klick); } } }); add(startclickbt); }
}
the second has quite same costructor.sliders , buttons not static. @ places buttons should appear screen black.
hope can me :)
edit:
public class clicker extends jframe { private static final long serialversionuid = 9001; protected jpanel panel; static click j = null; protected jslider jsl; protected jtextfield tf; static final int cps_min= 0; static final int cps_max= 100; static final int cps_init= 25; private int amount; private boolean visible; public int getdelay() { return math.abs(jsl.getvalue()-100); } public int getamount() { return amount; } public boolean getvisible() { return visible; } public void setopen(boolean visible) { this.visible=visible; } public clicker(boolean visible) { buildgui(visible); j = new click(false).addposition(new point((toolkit.getdefaulttoolkit().getscreensize().width)/2, (toolkit.getdefaulttoolkit().getscreensize().height)/2)).addposition(new point(getlocation().x+1, getlocation().y+20)); while(true) { j.runclicks(getdelay()); } } public clicker() { buildgui(true); j = new click(false).addposition(new point((toolkit.getdefaulttoolkit().getscreensize().width)/2, (toolkit.getdefaulttoolkit().getscreensize().height)/2)).addposition(new point(getlocation().x+1, getlocation().y+20)); while(true) { j.runclicks(getdelay()); } } public void buildgui(boolean visible) { settitle("clicker"); setsize(340, 200); setlayout(new gridlayout()); setalwaysontop(true); setlocation(0, 0); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); setvisible(visible); final jbutton bt1 = new jbutton("schließen"); bt1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { system.exit(0); } }); final jbutton bt2 = new jbutton("start "); bt2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { j.setclick(true); } }); final jbutton bt3 = new jbutton("stop "); bt3.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { j.setclick(false); joptionpane.showmessagedialog(null, "klicken gestoppt. klicks: " + j.getclickamount()); j.setclickamout(0); } }); final jbutton bt4 = new jbutton("startanzahl"); bt4.addactionlistener(new actionlistener() { private int amount; public void actionperformed(actionevent e) { string wert = tf.gettext(); try { amount = integer.parseint(wert); }catch(exception ee) { joptionpane.showmessagedialog(null,"das war keine zahl oder mehr als ein integer"); } j.doclick(amount); } }); tf = new jtextfield("anzahl gewünschte clicks max "+ integer.max_value, 10); tf.seteditable(true); jsl = new jslider(jslider.horizontal,cps_min,cps_max,cps_init); jsl.setminortickspacing(5); jsl.setmajortickspacing(10); jsl.setpaintticks(true); jsl.setpaintlabels(true); panel = new jpanel(); panel.addmousemotionlistener(new mousemotionlistener() { @override public void mousedragged(mouseevent event) { j.setclick(false); } @override public void mousemoved(mouseevent event) { j.setclick(false); } }); panel.addkeylistener(new keyadapter() { @override public void keypressed(keyevent e) { if(e.getkeycode() == keyevent.vk_f1) { j.setclick(true); } if(e.getkeycode() == keyevent.vk_f2) { j.setclick(false); } } }); add(panel); add(bt2); add(bt3); add(bt1); add(bt4); add(jsl); add(tf); pack(); } }
make setvisible
last thing call...
public void buildgui1() throws nullpointerexception { settitle("hauptmenü"); setsize(800, 480); setlayout(new gridlayout()); setalwaysontop(false); setlocation((toolkit.getdefaulttoolkit().getscreensize().width) / 4, (toolkit.getdefaulttoolkit().getscreensize().height) / 4); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); final jbutton startclickbt = new jbutton("start clicker"); startclickbt.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dispose(); if (klick == null) { klick = new clicker(); add(klick); // add when need add/remove components revalidate(); repaint(); } } }); add(startclickbt); // move here setvisible(true); }
also, use revalidate()
, repaint()
encourage container update it's layout when adding new components
recommendations:
- make sure ui started within context of edt, see initial threads more details
- avoid extending directly top level containers
jframe
, instead consider usingjpanel
base container. free's ui wider range of use-cases , prevents getting locked in (as can't add frames other frames) - the use of multiple jframes, good/bad practice?
updated
you have 2 immediate problems
clicker
extendsjframe
, trying add container, not possible in swing , cause exception, however...- you blocking event dispatching thread
while (true)
loop, meaning swing unable process new events, includingrepaint
events
for example...
public clicker(boolean visible) { buildgui(visible); j = new click(false).addposition(new point((toolkit.getdefaulttoolkit().getscreensize().width) / 2, (toolkit.getdefaulttoolkit().getscreensize().height) / 2)).addposition(new point(getlocation().x + 1, getlocation().y + 20)); // bad while (true) { j.runclicks(getdelay()); } } public clicker() { buildgui(true); j = new click(false).addposition(new point((toolkit.getdefaulttoolkit().getscreensize().width) / 2, (toolkit.getdefaulttoolkit().getscreensize().height) / 2)).addposition(new point(getlocation().x + 1, getlocation().y + 20)); // bad while (true) { j.runclicks(getdelay()); } }
now, click
looks it's ui component brings bunch of other problems, don't have code it's impossible comment.
the immediate solutions might use a:
swingworker
, see worker threads , swingworker more detailsswing
timer
, see how use swing timers more details- plain old
thread
things remember
- swing (like ui frameworks) single threaded, blocks thread (like never ending loops), prevent processing new events, including paint events, make application it's "hung", because has
- swing not thread safe. interactions ui must made within context of event dispatching thread. both
swingworker
, swingtimer
provide capability update ui safely.
Comments
Post a Comment