swing: UI for a countdown timer

2019-07-20 14:55发布

问题:

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:

alt text http://ecx.images-amazon.com/images/I/41oixoyyEAL._SL500_AA300_.jpg

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 and lets it go.

回答1:

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.



回答2:

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.



回答3:

It is fairly simple... Labels and buttons should be fine.. Here are some examples

alt text http://www.itblogs.info/img/android_stopwatch.png

alt text http://handheld.softpedia.com/images/software/thumbs/Stopwatch-Android-thumb.png

alt text http://www.leancrew.com/all-this/images/iphone-stopwatch-laps.png



回答4:

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.



回答5:

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.