简体   繁体   English

swing:倒数计时器的用户界面

[英]swing: UI for a countdown timer

I need a variable countdown timer for between 1-10 seconds, that can be stopped and restarted. 我需要一个介于1到10秒之间的可变倒数计时器,该计时器可以停止并重新启动。 What's a good set of UI elements to use for this? 可以使用哪些好的UI元素集? I need something intuitive that uses a fairly small amount of screen real estate, comparable to a normal-sized JButton. 我需要一个直观的东西,它使用相当少的屏幕空间,与普通大小的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. 注意 :我知道如何实现计时器并重新启动它们,我只需要弄清楚要使用哪些UI元素。

NOTE 2 : I need a variable countdown timer. 注意2 :我需要一个可变的倒数计时器。 If the user wants 1 second, I want a 1 second timer. 如果用户想要1秒,我想要1秒计时器。 If the user wants an 8.2 second timer, I want an 8.2 second timer. 如果用户想要一个8.2秒的计时器,我想要一个8.2秒的计时器。 The kitchen timer above is simple, the user just turns it to a certain amount and lets it go. 上面的厨房定时器很简单,用户只需将其旋转到一定程度然后放开即可。

Why not use a JProgressBar that starts off "full" and empties as the time decreases? 为什么不使用JProgressBar ,它以“ full”开始并随着时间的减少而清空? You overlay the remaining time in seconds over the bar, therefore avoiding using additional screen real estate. 您将剩余时间(以秒为单位)覆盖在条形图上,因此避免使用其他屏幕空间。

It is fairly simple... Labels and buttons should be fine.. Here are some examples 这非常简单...标签和按钮应该没问题。这是一些示例

替代文字
(source: itblogs.info ) (来源: itblogs.info

替代文字
(source: softpedia.com ) (来源: softpedia.com

替代文字
(source: leancrew.com ) (来源: leancrew.com

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. 对于短期解决方案,我使用了JSlider ...我向其BoundedRangeModel中添加了一个ActionListener,并在发生更改并且BoundedRangeModel.getValueIsAdjusting()返回false时设置了计时器。 When my timer counts down but is not yet expired and the BoundedRangeModel.getValueIsAdjusting() returns false, I call BoundedRangeModel.setValue(). 当我的计时器倒计时但尚未过期并且BoundedRangeModel.getValueIsAdjusting()返回false时,我将调用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. 一个(非常荒谬的)简单解决方案:如果您不需要太多图形功能,只需使用一个JButton即可显示计时器运行时剩余的秒数。 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. 然后,您可以在计时器运行时单击以停止(或暂停)它。

I'd use a JSpinner for the setting; 我将使用JSpinner进行设置; a javax.swing.Timer for the counting; 一个用于计数的javax.swing.Timer and a single button, labelled "Start" or "Stop" as a function of the Timer state. 以及一个标记为“开始”或“停止”的按钮,作为Timer状态的函数。 Almost anything would do for display, but @Adamski's JProgressBar idea has appeal. 几乎任何东西都可以显示,但是@Adamski的JProgressBar想法很JProgressBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM