2013-04-21 38 views
4

我想模擬應用程序GUI中的基本恆溫器。如何在java fx中每2秒更新標籤框?

我想用新的溫度值每2秒更新一次標籤框值。

例如,我的初始溫度將顯示爲68度,並更新爲69到70等,直到每2秒鐘75次。

這是我在Java fx中編寫的一段代碼。 controlpanel是標籤框存在的te形式的對象。它只更新最終值爲75.它不會每2秒更新一次。我寫了一個方法暫停,導致2秒延遲。所有標籤都會更新其最終值,但不會每2秒更新一次。當我調試時,我可以看到數值每2秒增加一次。此代碼是寫在按鈕onClick事件

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    int i=0; 
    Timer asd = new Timer(1000,null); 

    asd.setDelay(1000); 

    while(i < 10) 
    { 
     jTextField1.setText(Integer.toString(i)); 
     i++; 

     asd.start(); 
    } 
} 
+2

這如何涉及到[標籤:鞦韆]? JavaFX&Swing是不同的GUI工具包。你通常會使用其中一種。 – 2013-04-21 06:05:17

+0

認爲如果在swing框架中有這個問題的解決方案將幫助我在javafx中實現它 – user1364861 2013-04-21 06:08:14

+0

在Swing中,您將使用'javax.swing.Timer'。 – 2013-04-21 06:20:16

回答

12

爲了解決使用定時器,你需要實現TimerTask與您的代碼,並使用Timer#scheduleAtFixedRate方法反覆運行代碼的任務:

Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      System.out.print("I would be called every 2 seconds"); 
     } 
    }, 0, 2000); 

還要注意,調用

private int i = 0; 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      SwingUtilities.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
        jTextField1.setText(Integer.toString(i++)); 
       } 
      }); 
     } 
    }, 0, 2000); 
} 

在JavaFX的情況下,你的東東:任何UI操作必須Swing的UI線程(或FX UI線程,如果你正在使用JavaFX)完成d更新「FX UI線程」上的FX控件而不是Swing控件。使用javafx.application.Platform#runLater方法代替SwingUtilities

+0

嗨,你知道如何停止這個計時器嗎? – user1364861 2013-04-27 23:56:46

+0

'timer.cancel()' – 2014-02-14 09:26:29

8

這是另一種使用JavaFX動畫時間軸代替計時器的解決方案。

我喜歡這個解決方案,因爲動畫框架確保所有事情都發生在JavaFX應用程序線程上,因此您不必擔心線程問題。

temperatureprobe

import javafx.animation.*; 
import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.beans.property.*; 
import javafx.event.*; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

import java.util.Random; 

public class ThermostatApp extends Application { 
    @Override public void start(final Stage stage) throws Exception { 
    final Thermostat  thermostat  = new Thermostat(); 
    final TemperatureLabel temperatureLabel = new TemperatureLabel(thermostat); 

    VBox layout = new VBox(10); 
    layout.getChildren().addAll(temperatureLabel); 
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;"); 

    stage.setScene(new Scene(layout)); 
    stage.show(); 
    } 

    public static void main(String[] args) throws Exception { 
    launch(args); 
    } 
} 

class TemperatureLabel extends Label { 
    public TemperatureLabel(final Thermostat thermostat) { 
    textProperty().bind(
     Bindings.format(
     "%3d \u00B0F", 
     thermostat.temperatureProperty() 
    ) 
    ); 
    } 
} 

class Thermostat { 
    private static final Duration PROBE_FREQUENCY = Duration.seconds(2); 

    private final ReadOnlyIntegerWrapper temperature; 
    private final TemperatureProbe  probe; 
    private final Timeline    timeline; 

    public ReadOnlyIntegerProperty temperatureProperty() { 
    return temperature.getReadOnlyProperty(); 
    } 

    public Thermostat() { 
    probe  = new TemperatureProbe(); 
    temperature = new ReadOnlyIntegerWrapper(probe.readTemperature()); 

    timeline = new Timeline(
     new KeyFrame(
      Duration.ZERO, 
      new EventHandler<ActionEvent>() { 
      @Override public void handle(ActionEvent actionEvent) { 
       temperature.set(probe.readTemperature()); 
      } 
      } 
     ), 
     new KeyFrame(
      PROBE_FREQUENCY 
     ) 
    ); 
    timeline.setCycleCount(Timeline.INDEFINITE); 
    timeline.play(); 
    } 
} 

class TemperatureProbe { 
    private static final Random random = new Random(); 

    public int readTemperature() { 
    return 72 + random.nextInt(6); 
    } 
} 

該解決方案是基於從倒數計時器的解決方案:JavaFX: How to bind two values?