开发者

How to change the text of a JLabel in a while loop

I want to make a clock That changes the text of JLabel and i am having trouble wit开发者_开发问答h getting the text to change.

Thanks Obiedog!!!!

Heres my code for the clock if that helps


Looking at the javadoc may help you.

JLabel.setText().


It looks like your code didn't make it into the post, but I have a feeling I know what's wrong by the way you phrased your question :-)

Swing uses a single-threaded event loop to do its magic. Things like action events get called on this thread, as well as things like painting.

If you are using a simple while loop to act as your clock delay, you'll tie up the EDT (event dispatching thread), such that Swing will never have a chance to repaint your screen! You might even notice that dragging the window off-screen may blank it out entirely.

The solution is to use a delay mechanism that works within the event system. Luckily, such a thing exists! Take a look at the javax.swing.Timer (note: there are other classes called Timer, but this one is the only one that works within the event system.) By calling setText on your JLabel (and possibly repaint) in the actionListener of that Timer, you can get clock-like behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜