gpt4 book ai didi

Java:线程内的SetText

转载 作者:行者123 更新时间:2023-11-30 07:07:17 25 4
gpt4 key购买 nike

谁能帮我解决这个问题:

我有一些我想用作计时器(或时钟)的 TextField,所以我将文本设置在我在 Controller 类中调用的线程中。

package application;

import java.net.URL;
import java.util.ResourceBundle;

import org.joda.time.DateTime;

import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import jdk.nashorn.internal.runtime.FindProperty;

public class MainWindowController extends Thread implements Initializable{

@FXML
private TextField dayText;

@FXML
private TextField monthText;

@FXML
private TextField yearText;

@FXML
private TextField hoursText;

@FXML
private TextField minutesText;

@FXML
private TextField secondsText;

@FXML
private TextField julianDayText;

@Override
public void initialize(URL arg0, ResourceBundle arg1) {

this.start();

}

@Override
public void run() {

while(true){

DateTime d = new DateTime(System.currentTimeMillis());

dayText.setText(String.valueOf(d.getDayOfMonth()));
monthText.setText(String.valueOf(d.getMonthOfYear()));
yearText.setText(String.valueOf(d.getYear()));

hoursText.setText(String.valueOf(d.getHourOfDay()));
minutesText.setText(String.valueOf(d.getMinuteOfHour()));
secondsText.setText(String.valueOf(d.getSecondOfMinute()));
}
}
}

我不知道为什么我在运行我的代码后得到一个 NullPointerException(它工作了一点然后崩溃了):

Exception in thread "Thread-4" java.lang.NullPointerException
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:339)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.scene.control.TextInputControl$TextProperty.fireValueChangedEvent(TextInputControl.java:1116)
at javafx.scene.control.TextInputControl$TextProperty.markInvalid(TextInputControl.java:1120)
at javafx.scene.control.TextInputControl$TextProperty.set(TextInputControl.java:1056)
at javafx.scene.control.TextInputControl.setText(TextInputControl.java:279)
at application.MainWindowController.run(MainWindowController.java:208)

请帮助并提前致谢

最佳答案

您可以使用时间线和关键帧。只需将 showTime() 替换为您自己的代码即可。并且不要扩展线程。

public class FXMLTimeController implements Initializable {

@FXML
private TextField txtTime;

//timeline
private Timeline timeline;

private void showTime() {
txtTime.setText((new Date()).toString());
}

@Override
public void initialize(URL url, ResourceBundle rb) {
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(false);

timeline.getKeyFrames().add(
new KeyFrame(Duration.seconds(1),
new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
showTime();
}
}));
timeline.play();
}

}

关于Java:线程内的SetText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25017351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com