在JavaFX中,如何顯示使用「標籤」隨時間不斷變化的值?在JavaFx標籤中顯示更改值
18
A
回答
24
有許多方法來實現這一目標,最方便的辦法是使用JavaFX的的數據綁定機制:
// assuming you have defined a StringProperty called "valueProperty"
Label myLabel = new Label("Start");
myLabel.textProperty().bind(valueProperty);
這樣一來,您的每一次valueProperty被調用它的設置方法改變,標籤的文本更新。
9
我喜歡塞巴斯蒂安的約束性答案。
對於多樣性,這裏是另一個基於時間修改標籤文本的示例。示例顯示標籤中的數字時鐘讀數,其標籤每秒使用Timeline更改。
import java.io.IOException;
import java.net.URL;
import javafx.animation.*;
import javafx.event.*;
import javafx.scene.control.Label;
import javafx.util.Duration;
import java.util.Calendar;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class DigitalClockSample extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) throws IOException {
stage.setScene(new Scene(new DigitalClock(), 100, 50));
stage.show();
}
}
/**
* Creates a digital clock display as a simple label.
* Format of the clock display is hh:mm:ss aa, where:
* hh Hour in am/pm (1-12)
* mm Minute in hour
* ss Second in minute
* aa Am/pm marker
* Time is the system time for the local timezone.
*/
class DigitalClock extends Label {
public DigitalClock() {
bindToTime();
}
// the digital clock updates once a second.
private void bindToTime() {
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0),
new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
Calendar time = Calendar.getInstance();
String hourString = StringUtilities.pad(2, ' ', time.get(Calendar.HOUR) == 0 ? "12" : time.get(Calendar.HOUR) + "");
String minuteString = StringUtilities.pad(2, '0', time.get(Calendar.MINUTE) + "");
String secondString = StringUtilities.pad(2, '0', time.get(Calendar.SECOND) + "");
String ampmString = time.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
setText(hourString + ":" + minuteString + ":" + secondString + " " + ampmString);
}
}
),
new KeyFrame(Duration.seconds(1))
);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
}
class StringUtilities {
/**
* Creates a string left padded to the specified width with the supplied padding character.
* @param fieldWidth the length of the resultant padded string.
* @param padChar a character to use for padding the string.
* @param s the string to be padded.
* @return the padded string.
*/
public static String pad(int fieldWidth, char padChar, String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length(); i < fieldWidth; i++) {
sb.append(padChar);
}
sb.append(s);
return sb.toString();
}
}
數字時鐘輸出樣本:
12
有關使用的SimpleDateFormat如何? 不需要StringUtilities類!
private void bindToTime() {
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0),
new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
Calendar time = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
setText(simpleDateFormat.format(time.getTime()));
}
}
),
new KeyFrame(Duration.seconds(1))
);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
}
4
優秀的回覆,謝謝jewelsea爲您的輸入提供了很多幫助。
我更新了以前使用Java 8以更簡潔的格式發佈的DigitalClock。使用Java 8的新增功能,如Date API,當然還有lambdas。
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.control.Label;
import javafx.util.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class DigitalClock extends Label
{
private static DateTimeFormatter SHORT_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
public DigitalClock()
{
bindToTime();
}
private void bindToTime() {
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0),
event -> setText(LocalTime.now().format(SHORT_TIME_FORMATTER))),
new KeyFrame(Duration.seconds(1)));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
}
相關問題
- 1. 根據布爾值更改標籤JavaFX
- 2. 在JavaFX中顯示字符串/標籤
- 3. JavaFX更改標籤文本
- 4. 在JavaFX中更新標籤
- 5. select2顯示不更新標籤文本,但值正在更改
- 6. servlet根據更改組合框的值顯示div標籤值
- 7. 更新標籤JavaFX中
- 8. javafx更改標籤的錨點
- 9. 更改標籤文本的JavaFX FXML
- 10. 更改JSlider的可顯示標籤?
- 11. JavaScript使用#標籤更改顯示
- 12. JavaFX setStageToScene()使標籤顯示省略號
- 13. JavaFX標籤不顯示文本
- 14. JavaFX標籤窗格不顯示
- 15. 更改輸入值更改標籤值
- 16. 根據數據庫中的值更改標籤顯示名稱標籤上的下拉更改
- 17. Java:在線程中更改標籤值
- 18. 如何在QwtSlider中顯示值標籤?
- 19. 單擊標籤並顯示Div(標籤名稱動態更改)
- 20. 如何顯示具有變量值的標籤? (JavaFX的)
- 21. 標籤JavaFX中
- 22. 更改JavaFX虛擬鍵盤的顯示
- 23. 在更改Ionic3中的rootPage後沒有顯示標籤
- 24. 無法用javaFX在標籤中顯示文本
- 25. 更改標籤更改DropDownListFor值
- 26. 標籤不顯示在鼠標事件了JavaFx
- 27. 動態分配標籤值並在值更改時顯示消息
- 28. JavaFX標籤不更新
- 29. 在選擇組合框值後更新JavaFX中的標籤
- 30. 如何在xcode中使用標籤顯示標籤的值?