java - how to display sequentially overlapping JLabels -
this must kind of elevator. if click button ( on left or on right side of gui ) rectangle must move floor floor delay. problem gui reject display sequentially; instead, display "floors" in same time. :(
//this applet import javax.swing.japplet; @suppresswarnings("serial") public class applet extends japplet{ public applet(){ add(new elevator()); }} //this main class import java.awt.*; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.gridlayout; import javax.swing.*; import javax.swing.border.lineborder; @suppresswarnings("serial") public class lift extends jpanel implements actionlistener{ private jbutton[] externalbuttons=new jbutton[11]; private jbutton[] internalbuttons=new jbutton[13]; private jlabel[] floors=new jlabel[11]; private jlabel o1=new jlabel(); private jlabel o2=new jlabel(); private jpanel p1=new jpanel(); private jpanel p2=new jpanel(); private jpanel p3=new jpanel(); private jlabel[] labelblueleft=new jlabel[11]; private jlabel[] labelblueright=new jlabel[11]; private jlabel[] labelredleft=new jlabel[11]; private jlabel[] labelredright=new jlabel[11]; private int firstoption=0, lastoption; public elevator(){ repaint(); p1.setlayout(new gridlayout(11,1)); p2.setlayout(new gridlayout(11,1)); p3.setlayout(new gridlayout(15,1)); for(int i=1; i<11;i++){ externalbuttons[i]=new jbutton("floor " + i); externalbuttons[i].addactionlistener(this); } externalbuttons[0]=new jbutton("ground"); externalbuttons[0].addactionlistener(this); for(int i=3; i<13; i++){ internalbuttons[i]=new jbutton("floor " + (i-2)); internalbuttons[i].addactionlistener(this); } internalbuttons[0]=new jbutton("alarm!"); internalbuttons[1]=new jbutton("stop"); internalbuttons[2]=new jbutton("ground"); internalbuttons[0].addactionlistener(this); internalbuttons[1].addactionlistener(this); internalbuttons[2].addactionlistener(this); for(int i=0; i<11;i++){ floors[i]=new jlabel(""); floors[i].setlayout( new gridlayout()); floors[i].setborder(new lineborder(new color(150,150,150))); floors[i].setpreferredsize(new dimension(200,25)); if((i%2)==0) { floors[i].setbackground(color.lightgray); floors[i].setopaque(true); } labelblueleft[i]=new customlabel(25,0,35,25,0,0); labelblueright[i]=new customlabel(25,0,35,25,0,1); labelredleft[i]=new customlabel(25,0,35,25,1,0); labelredright[i]=new customlabel(25,0,35,25,1,1); } floors[firstoption].add(labelblueleft[0]); for(int i=10; i>-1; i--){ p1.add(externalbuttons[i]); p2.add(floors[i]); } p3.add(o1); p3.add(o2); for(int i=12; i>-1; i--) p3.add(internalbuttons[i]); add(p1, borderlayout.west); add(p2, borderlayout.center); add(p3, borderlayout.east); } //action performed public void actionperformed(actionevent e){ for(int i=0; i<11; i++){ if(e.getsource()==externalbuttons[i]) lastoption=i; } for(int i=2; i<13; i++){ if(e.getsource()==internalbuttons[i]) lastoption=i-2; } floors[firstoption].remove(labelblueleft[0]); if(firstoption<lastoption){ for(int i=firstoption; i<lastoption+1; i++){ floors[i].add(labelredleft[i]); floors[i].validate(); floors[i].repaint(); } } else if(firstoption>lastoption){ for(int i=firstoption; i>lastoption-1; i--){ floors[i].add(labelredleft[i]); floors[i].revalidate(); floors[i].repaint(); }} firstoption=lastoption; } } //this customlabel class import java.awt.color; import java.awt.graphics; import javax.swing.jlabel; @suppresswarnings("serial") public class customlabel extends jlabel{ private int x=25, y=0, width=35, height=20; public customlabel(int x1, int y1, int width1, int height1, int clr, int ind){ if(ind==1){x1=150; y1=0;} if(clr==1) {setforeground(color.red);} else {setforeground(color.blue);} x=x1; y=y1; width=width1; height=height1; } protected void paintcomponent(graphics g){ super.paintcomponent(g); g.fillrect(x, y, width, height); repaint(); } }
java swing japplet
No comments:
Post a Comment