swing: UI for a countdown timer

2019-07-20 14:47发布

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.

5条回答
够拽才男人
2楼-- · 2019-07-20 15:20

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楼-- · 2019-07-20 15:20

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.

查看更多
走好不送
4楼-- · 2019-07-20 15:25

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.

查看更多
我想做一个坏孩纸
6楼-- · 2019-07-20 15:37

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.

查看更多
登录 后发表回答