시간 감소를 보여주는 GUI로 카운트 다운 타이머를 구현하는 방법을 배우고 있습니다. Groovy의 @Bindable
을 사용하여 시간 감소가 해당 UI 레이블에 자동으로 표시 될 수 있기를 바랍니다.다른 스레드에서 @Bindable 변수의 변경 사항을 해당 UI 요소에 반영 할 수 있습니까?
카운트 다운 시간 값의 감소는 타이머 스레드에서 UI 스레드와 분리되어 수행됩니다. 그러나 카운트 다운 타이머가 UI에서 업데이트되지 않습니다.
UI 업데이트의 카운트 다운 시간을 적절하게 유지하는 방법은 무엇입니까?
import groovy.swing.SwingBuilder
import java.awt.FlowLayout as FL
import javax.swing.BoxLayout as BXL
import javax.swing.JFrame
import groovy.beans.Bindable
import java.util.timer.*
// A count-down timer using Bindable to reflcet the reduction of time, when the reduction is done in a TimerTask thread
class CountDown {
int delay = 5000 // delay for 5 sec.
int period = 60*1000 // repeat every minute.
int remainingTime = 25*60*1000
// hope to be able to update the display of its change:
@Bindable String timeStr = "25:00"
public void timeString() {
int seconds = ((int) (remainingTime/1000)) % 60 ;
int minutes =((int) (remainingTime/(1000*60))) % 60;
timeStr = ((minutes < 9) ? "0" : "") + String.valueOf (minutes) + ":" + ((seconds < 9) ? "0" : "") + String.valueOf (seconds)
}
public void update() {
if (remainingTime >= period)
remainingTime = (remainingTime - period)
// else // indicate the timer expires on the panel
// println remainingTime
// convert remainingTime to be minutes and secondes
timeString()
println timeStr // this shows that the TimerTaskCountDown thread is producting the right reduction to timeStr
}
}
model = new CountDown()
class TimerTaskCountDown extends TimerTask {
public TimerTaskCountDown (CountDown modelIn) {
super()
model = modelIn
}
CountDown model
public void run() {
model.update() // here change to model.timeStr does not reflected
}
}
Timer timer = new Timer()
timer.scheduleAtFixedRate(new TimerTaskCountDown(model), model.delay, model.period)
def s = new SwingBuilder()
s.setVariable('myDialog-properties',[:])
def vars = s.variables
def dial = s.dialog(title:'Pomodoro', id:'working', modal:true,
// locationRelativeTo:ui.frame, owner:ui.frame, // to be embedded into Freeplane eventually
defaultCloseOperation:JFrame.DISPOSE_ON_CLOSE, pack:true, show:true) {
panel() {
boxLayout(axis:BXL.Y_AXIS)
panel(alignmentX:0f) {
flowLayout(alignment:FL.LEFT)
label text: bind{"Pomodoro time: " + model.timeStr}
}
panel(alignmentX:0f) {
flowLayout(alignment:FL.RIGHT)
button(action: action(name: 'STOP', defaultButton: true, mnemonic: 'S',
closure: {model.timeStr = "stopped"; vars.ok = true//; dispose() // here the change to model.timeStr gets reflected in the label
}))
}
}
}
사소한 정리, 노이즈 제거, 속도 디버깅을 단축 지연을 포함, 난에서 발견 "타이머 스윙"자바에 가장 가까운 예를 발견했습니다 http://stackoverflow.com/questions/2576353/stop-a-stopwatch/2576909#2576909 –