java - How do I use actionListeners without JButtons? -
i reading documentation on swing timers, when came across information actionlistener
s. when further researched, find how create actionlistener
attached jbutton
, etc. how can create plain actionlistener
, not attached anything?
my timer not working correctly, , thought may because incorrectly using actionlistener
.
here code:
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.timer; public class mytimer { actionlistener al = new actionlistener() { public void actionperformed(actionevent evt) { system.out.println("testing"); } }; public mytimer() { timer timer = new timer(10, al); timer.start(); } public static void main(string[] args) { mytimer start = new mytimer(); } }
how can create plain actionlistner, not attached anything?
loot @ this:
actionlistener listener = new actionlistener() { public void actionperformed(actionevent e) { system.out.println("hello world!"); } }; // using listener 2 seconds delay java.swing.timer timer = new java.swing.timer(2000, listener); timer.setrepeats(false); // start timer timer.start();
try this:
import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.timer; public class mytimer { actionlistener al = new actionlistener() { public void actionperformed(actionevent evt) { system.out.println("testing"); } }; public mytimer() { timer timer = new timer(1000, al); timer.start(); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { new mytimer(); } }); } }
Comments
Post a Comment