java - JInternalFrame does not show up -
from mainframe create internal frame , add desktop pane, internal frame won't show up. tried figure out 3 hours , kinda need it. before implemented observer pattern worked fine. thought got , changed stuff , double checked crucial code. no solution far. realized how crappy english sometimes. sorry that!
the 2 code blocks shortened. sorry still pretty long. not sure made mistake. if need more code or else, tell me. thank in advance.
this code of main frame:
public class langtonsameise extends jframe implements actionlistener { private jdesktoppane desk; private jpanel panelbuttons; jmenubar jmb; jmenu file,modus; jmenuitem load,save, exit, msetzen,mmalen,mlaufen; jslider slider; static int xint, yint,xframe=450,yframe=450; static boolean bsetzen = false, bmalen = false, running = false; random randomgenerator = new random(); jlabel xlabel, ylabel, speed, statustext,status; jbutton start, stop, addant; jtextfield xfield, yfield; public langtonsameise() { // desktop desk = new jdesktoppane(); getcontentpane().add(desk, borderlayout.center); /* 4 lines work fine. internal frame gets displayed. jinternalframe j = new jinternalframe(); desk.add(j); j.setsize(new dimension(300,300)); j.setvisible(true); */ speed = new jlabel("geschwindigkeit"); xlabel = new jlabel("x:"); ylabel = new jlabel("y:"); xlabel.sethorizontalalignment(jlabel.right); ylabel.sethorizontalalignment(jlabel.right); xlabel.setopaque(true); ylabel.setopaque(true); xfield = new jtextfield(); yfield = new jtextfield(); xfield.setdocument(new integerdocument(2)); yfield.setdocument(new integerdocument(2)); start = new jbutton("fenster erstellen"); stop = new jbutton("stopp"); start.setmargin(new insets(0, 0, 0, 0)); stop.setmargin(new insets(0, 0, 0, 0)); // buttons panelbuttons = new jpanel(); panelbuttons.setlayout(new gridlayout()); panelbuttons.add(start); panelbuttons.add(xlabel); panelbuttons.add(xfield); panelbuttons.add(ylabel); panelbuttons.add(yfield); panelbuttons.add(speed); panelbuttons.add(slider); panelbuttons.add(new panel()); panelbuttons.add(stop); start.addactionlistener(this); stop.addactionlistener(this); add(panelbuttons, borderlayout.north); statustext = new jlabel("aktueller modus:"); status = new jlabel("stopp"); // jmenubar jmb = new jmenubar(); setjmenubar(jmb); file = new jmenu("datei"); modus = new jmenu("modus"); mlaufen = new jmenuitem("laufen"); mmalen = new jmenuitem("malen"); msetzen = new jmenuitem("setzen"); load = new jmenuitem("simulation laden"); save = new jmenuitem("simulation speichern"); msetzen.addactionlistener(this); mmalen.addactionlistener(this); mlaufen.addactionlistener(this); exit = new jmenuitem("exit"); file.add(save); file.add(load); file.addseparator(); file.add(exit); save.addactionlistener(this); load.addactionlistener(this); modus.add(mlaufen); modus.add(msetzen); modus.add(mmalen); jmb.add(file); jmb.add(modus); (int = 0; < 20; i++) { jmb.add(new jlabel(" ")); } jmb.add(statustext); jmb.add(status); setsize(new dimension(1000, 900)); dimension dim = toolkit.getdefaulttoolkit().getscreensize(); this.setlocation(dim.width / 2 - this.getsize().width / 2, dim.height / 2 - this.getsize().height / 2); xfield.settext("5"); yfield.settext("5"); setvisible(true); } public void actionperformed(actionevent e) { if (e.getactioncommand().equals("fenster erstellen")) { if (xfield.gettext().equals("") || yfield.gettext().equals("")) { new popupwindow( "sie müssen eine zahl zwischen 2 und 99 angeben!!"); } else { xint = integer.parseint(xfield.gettext()); yint = integer.parseint(yfield.gettext()); state s = new state(); kindfenster k = new kindfenster(s, xint, yint); s.addobserver(k); addchild(k, this.getsize().width, this.getsize().height); } } if (e.getactioncommand().equals("stopp")) { running=!running; status.settext("stopp"); } } public void addchild(jinternalframe kind, int xpixel, int ypixel) { // kind.setsize(370, 370); kind.setlocation(randomgenerator.nextint(xpixel - kind.getsize().height), randomgenerator.nextint(ypixel - kind.getsize().height - 100)); kind.setdefaultcloseoperation(jinternalframe.dispose_on_close); desk.add(kind); kind.setvisible(true); kind.repaint(); kind.validate(); } public static void main(string[] args) { langtonsameise hauptfenster = new langtonsameise(); } }
and internal frame:
public class kindfenster extends jinternalframe implements actionlistener, serializable,observer { /** * */ private static final long serialversionuid = 8939449766068226519l; static int nr = 0; static int x,y,xscale,yscale,xframe,yframe; static int sleeptime; state s; arraylist<jbutton> jbarray = new arraylist<>(); arraylist<imageicon> ameisen = new arraylist<imageicon>(); public static arraylist<ameisethread> threadlist = new arraylist<>(); color altefarbe, neuefarbe; jbutton save, addant; jpanel panelbuttonskind; jslider sliderkind; public jpanel panelspielfeld; static setzenactionlistener sal = new setzenactionlistener(); static mousemotionactionlistener mmal = new mousemotionactionlistener(); public kindfenster(state s,int x, int y) { super("kind " + (++nr), true, true, true, true); setlayout(new borderlayout()); this.s=s; setsize(new dimension(xframe, yframe)); panelspielfeld = new jpanel(); panelspielfeld.setlayout(new gridlayout(y, x)); panelbuttonskind = new jpanel(); panelbuttonskind.setlayout(new gridlayout(1, 3)); save = new jbutton("simulation speichern"); addant = new jbutton("ameise hinzufügen"); save.setmargin(new insets(0, 0, 0, 0)); addant.setmargin(new insets(0, 0, 0, 0)); addant.addactionlistener(this); save.addactionlistener(this); sliderkind = new jslider(jslider.horizontal, 1, 10, 5); sliderkind.setsnaptoticks(true); sliderkind.setpaintticks(true); sliderkind.setpainttrack(true); sliderkind.setmajortickspacing(1); sliderkind.setpaintlabels(true); sliderkind.addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { if (threadlist.size() >= 0) { (ameisethread t : threadlist) { jslider source = (jslider) e.getsource(); if (!source.getvalueisadjusting()) { int speed = source.getvalue(); sleeptime = 1000 / speed; } } } } }); panelbuttonskind.add(save); panelbuttonskind.add(sliderkind); panelbuttonskind.add(addant); add(panelbuttonskind, borderlayout.north); add(panelspielfeld, borderlayout.center); this.addcomponentlistener(new mycomponentadapter()); setvisible(true); repaint(); validate(); } public void actionperformed(actionevent e) { if (e.getactioncommand().equals("ameise hinzufügen")) { threadlist.add(new ameisethread(this)); threadlist.get(threadlist.size() - 1).start(); } if (e.getactioncommand().equals("simulation speichern")) { outputstream fos = null; try { fos = new fileoutputstream("test"); objectoutputstream o = new objectoutputstream(fos); o.writeobject(this ); } catch (ioexception e1) { system.err.println(e1); } { try { fos.close(); } catch (exception e1) { e1.printstacktrace(); } } } }
i think because didn't set size jinternalframe
. in code comment:
//kind.setsize(370, 370);
when remove "//" works me.
Comments
Post a Comment