java - adding JPanels left to right AND keyboard input -
1. having problem adding jpanels in sequence. essentially, user select between 1 , 4 "drone" objects run simultaneously. if select two, layout should add together 2 "drones" panels jframe left right. current code stacks them on top of 1 another.
2. when add together keyboard.nextint(); program, initializes jframe , freezes.
public class rundrones { public int numdrones; final jframe display; private final jlabel howmany; private makedrone dronepane; public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } new rundrones(); } }); } public rundrones() { //scanner keyboard = new scanner(system.in); display = new jframe(); display.setlayout(new gridlayout()); jpanel container = new jpanel(); container.setlayout(new boxlayout(container,boxlayout.line_axis)); dronepane = new makedrone(); dronepane.setsize(200, 400); dronepane.setborder(borderfactory.createlineborder(color.black)); howmany = new jlabel("how many drones (1-4): ", jlabel.center); howmany.setfont(new font("verdana", 1, 25)); display.setsize(800, 400); display.setdefaultcloseoperation(jframe.exit_on_close); display.setlocationrelativeto(null); container.add(howmany); display.add(container); display.setvisible(true); //numdrones = keyboard.nextint(); numdrones = 2; if (numdrones >= 5) { howmany.settext("between 1 , 4 idiot. seek again: "); //numdrones = keyboard.nextint(); } else { container.remove(howmany); switch (numdrones){ case 4: timer drone1 = new timer(30, new drone(dronepane)); container.add(dronepane); drone1.setinitialdelay(0); drone1.start(); case 3: timer drone2 = new timer(30, new drone(dronepane)); container.add(dronepane); drone2.setinitialdelay(0); drone2.start(); case 2: timer drone3 = new timer(30, new drone(dronepane)); container.add(dronepane); drone3.setinitialdelay(0); drone3.start(); case 1: timer drone4 = new timer(30, new drone(dronepane)); container.add(dronepane); drone4.setinitialdelay(0); drone4.start(); } } } public static int getrandom() { int firstnum = new random().nextint(5) + 1; int secondnum = new random().nextint(5) + 1; homecoming firstnum - secondnum; } public class makedrone extends jpanel { private int ypos; public void setypos(int ypos) { this.ypos = ypos; repaint(); } public int getypos() { homecoming ypos; } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.black); int y = ((getheight() / 2) - 90); int x = ((getwidth() / 2) - 45); g.drawrect(x, y, 90, 180); y = (getheight() + getypos()) / 2; // y = (getheight() / 2) + getypos(); g.setcolor(getforeground()); g.fillrect(x, y, 90, 12); } } public class drone implements actionlistener { private int dronestatus; private makedrone levelbar; public drone(makedrone levelbar) { this.levelbar = levelbar; levelbar.setforeground(color.black); } @override public void actionperformed(actionevent e) { if (dronestatus >= -85 && dronestatus <= 85) { dronestatus += getrandom(); levelbar.setypos(dronestatus); if (dronestatus < -40 && dronestatus > -85) { levelbar.setforeground(color.orange); } if (dronestatus < 85 && dronestatus > 40) { levelbar.setforeground(color.orange); } if (dronestatus <= 40 && dronestatus >= -40) { levelbar.setforeground(color.black); } } else { levelbar.setforeground(color.red); ((timer) e.getsource()).stop(); } } } }
change layout before add together drones based on number of columns need...
container.remove(howmany); container.setlayout(new gridlayout(1, numdrones)); switch (numdrones){ case 4: //... don't forget, if need to, need reset layout afterwards ;)
updated...
the "other" problem you're adding instance of makedrone container. instance of component can reside on single parent, effectively, you've added dronepane once.
instead, you'll need create new instance of makedrone each column. seek replacing switch statement more like
for (int index = 0; index < numdrones; index++) { makedrone md = new makedrone(); timer timer = new timer(30, new drone(md)); container.add(md); timer.setinitialdelay(0); timer.start(); } java swing keyboard jpanel boxlayout
No comments:
Post a Comment