我无法更改其他类(class)的标签文本。 我需要不断更新主屏幕上的日期和时间,同时我也可以同时执行其他功能。 我使用了 TimeSetting 类,它扩展了 Thread,在它的 run() 方法中,我使用 setText() 在无限循环中调用了更新命令,然后让该方法 hibernate 了一秒钟。 但是在运行此命令时,没有任何反应,并且在关闭输出窗口时,我收到一个错误,提示NullPointerExcpetion
这是两个类的代码: FXMLDocumentController.java
package crt;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class FXMLDocumentController extends Thread implements Initializable
{
@FXML
protected Label check;
@FXML
**protected Label date;**
@FXML
protected Label time;
@FXML
protected Label RRRR;
@FXML
protected Label DDDD;
@FXML
protected Label SSSS;
@FXML
protected Label temp;
@FXML
protected Label maxtemp;
@FXML
protected Label mintemp;
@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
//dc.setDate(date.textProperty().bind(valueproperty));
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menu.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle("MENU");
stage.setScene(new Scene(root1));
stage.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="367.0" prefWidth="510.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="crt.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="387.0" layoutY="302.0" minHeight="25.0" minWidth="80.0" onAction="#handleButtonAction" onTouchPressed="#handleButtonAction" text="Menu" />
<Label fx:id="date" layoutX="56.0" layoutY="64.0" minHeight="25.0" minWidth="80.0" />
<Label fx:id="time" layoutX="361.0" layoutY="64.0" minHeight="25.0" minWidth="80.0" text="S" />
<Label fx:id="RRRR" layoutX="76.0" layoutY="100.0" minHeight="25.0" minWidth="70.0" />
<Label fx:id="DDDD" layoutX="195.0" layoutY="100.0" minHeight="25.0" minWidth="70.0" />
<Label fx:id="SSSS" layoutX="314.0" layoutY="100.0" minHeight="25.0" minWidth="70.0" />
<Text layoutX="136.0" layoutY="163.0" strokeType="OUTSIDE" strokeWidth="0.0" text="TEMP :-" />
<Label fx:id="temp" layoutX="275.0" layoutY="156.0" minHeight="25.0" minWidth="70.0" text="A" />
<Text layoutX="136.0" layoutY="203.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MAX TEMP :-" />
<Label fx:id="maxtemp" layoutX="275.0" layoutY="188.0" minHeight="25.0" minWidth="70.0" text="B" />
<Text layoutX="136.0" layoutY="243.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MIN TEMP :-" />
<Label fx:id="maxtemp" layoutX="275.0" layoutY="225.0" minHeight="25.0" minWidth="70.0" text="C" />
<ProgressBar layoutX="401.0" layoutY="21.0" prefHeight="18.0" prefWidth="70.0" progress="0.0" />
<Button fx:id="startbutton" layoutX="14.0" layoutY="18.0" mnemonicParsing="false" onAction="#startstart" text="START" />
<Label fx:id="check" layoutX="42.0" layoutY="306.0" />
</children>
</AnchorPane>
时间设置.java
package crt;
import java.util.Date;
public class TimeSetting extends Thread {
@Override
public void run()
{
FXMLDocumentController fdc = new FXMLDocumentController();
// fdc.load();
int i=0;
while(true)
{
Date d = new Date();
fdc.date.setText("fd" + i);
i++;
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
CRT.java
package crt;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CRT extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) throws InterruptedException {
launch(args);
TimeSetting ts = new TimeSetting();
ts.start();
}
}
最佳答案
launch()
在 Application
退出之前不会完成。您应该在应用程序的 start
方法中启动线程。
此外,new FXMLDocumentController()
显然会创建 Controller 类的一个新实例 - 一个未连接到任何 fxml 的实例,因此不会注入(inject)任何字段。有关与 Controller 通信的更多信息可以在此处找到:Passing Parameters JavaFX FXML
此外,如果您让它正常工作,您仍然使用与 JavaFX 应用程序线程不同的线程来修改 UI。不应该这样做。而是使用 Platform.runLater
更新 UI:
while(true) {
Date d = new Date();
final String text = "fd" + i;
Platform.runLater(() -> {
fdc.date.setText(text);
});
i++;
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
}
}
关于java - javafx-fxml 中不同类的持续标签更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40489557/