java - Swing timers basic functionality -


i'm new @ java graphic design, , you, if possible, me easy example me understand basic functionality of jframes, timers, swingcontrollers, , stuff. how implement following case:

we have jframe jpanel inside. when execution starts, jpanel white, want change it's colour every 2 seconds:

public class mijframe extends javax.swing.jframe {      public mijframe() {         initcomponents();     }       public static void main(string args[]) {         java.awt.eventqueue.invokelater(new runnable() {              public void run() {                 new mijframe().setvisible(true);                 jpanel1.setbackground(color.yellow);                 jpanel1.setbackground(color.red);             }         });     }      // variables declaration - not modify     private static javax.swing.jpanel jpanel1;     // end of variables declaration } 

at first, used sleep method of thread object between setbackgroud() methods doesn't work, shows last change. how use here timer object?

first of all, whenever need change colour of said thingy, set opaque property true said thingy. in case it's jpanel first of must use panelobject.setopaque(true), look , feels calling method must background colour changes take effect.

do try code example, regarding rest :

import java.awt.*; import java.awt.event.*; import javax.swing.*; /*  * @see  * http://stackoverflow.com/q/11036830/1057230  */  public class colourtimer {     private jpanel contentpane;     private timer timer;     private int counter;     private color[] colours = {                                 color.red,                                 color.white,                                 color.blue,                                 color.dark_gray,                                 color.yellow,                                 color.light_gray,                                 color.black,                                 color.magenta,                                 color.pink,                                 color.cyan                               };      private actionlistener timeraction = new actionlistener()     {         @override         public void actionperformed(actionevent ae)         {             if (counter == (colours.length - 1))                 counter = 0;             contentpane.setbackground(colours[counter++]);         }         };      public colourtimer()     {         counter = 0;     }      private void displaygui()     {         jframe frame = new jframe("colour timer");         frame.setdefaultcloseoperation(jframe.dispose_on_close);          contentpane = new jpanel();         contentpane.setopaque(true);          final jbutton button = new jbutton("stop");         button.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent ae)             {                 if (timer.isrunning())                 {                     button.settext("start");                     timer.stop();                 }                 else                 {                     button.settext("stop");                     timer.start();                 }             }         });          frame.getcontentpane().add(contentpane, borderlayout.center);         frame.getcontentpane().add(button, borderlayout.page_end);         frame.setsize(300, 200);         frame.setlocationbyplatform(true);         frame.setvisible(true);         timer = new timer(2000, timeraction);         timer.start();     }      public static void main(string... args)     {         eventqueue.invokelater(new runnable()         {             @override             public void run()             {                 new colourtimer().displaygui();             }         });     } } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -