swing: UI for a countdown timer
I need a variable countdown timer for between 1-10 seconds, that can be stopped and restarted. What's a good set of UI elements to use for this? I need something intuitive that uses a fairly small amount of screen real estate, comparable to a normal-sized JButton. A windup kitchen egg timer would be the best physical analogy:
NOTE: I know how to implement timers and restart them, I just need to figure out what UI elements to use.
NOTE 2: I need a variable countdown timer. If the user wants 1 second, I want a 1 second timer. If the user wants an 8.2 second timer, I want an 8.2 second timer. The kitchen timer above is simple, the user just turns it to a certain amount a开发者_JAVA技巧nd lets it go.
Why not use a JProgressBar
that starts off "full" and empties as the time decreases? You overlay the remaining time in seconds over the bar, therefore avoiding using additional screen real estate.
For a short term solution I used a JSlider... I add an ActionListener to its BoundedRangeModel, and set my timer when there is a change and the BoundedRangeModel.getValueIsAdjusting() returns false. When my timer counts down but is not yet expired and the BoundedRangeModel.getValueIsAdjusting() returns false, I call BoundedRangeModel.setValue().
Not too happy with it but it kinda does what I want.
A (ridiculously) simple solution: if you don't need too much graphical flair, just use a single JButton that displays the seconds remaining when the timer is running. When the timer's off, it displays "Start"; clicking it will begin the countdown. You could then stop (or pause) it by clicking when the timer is running.
It is fairly simple... Labels and buttons should be fine.. Here are some examples
(source: itblogs.info)
(source: softpedia.com)
(source: leancrew.com)
I'd use a JSpinner
for the setting; a javax.swing.Timer
for the counting; and a single button, labelled "Start" or "Stop" as a function of the Timer
state. Almost anything would do for display, but @Adamski's JProgressBar
idea has appeal.
精彩评论