[英]want to change label text from another fxml controller class in javafx
[英]Constant label updation from different class in javafx-fxml
我無法從其他課程更改我的標簽文本。 我需要在主屏幕上不斷更新日期和時間,同時我也可以同時執行其他功能。 我使用了一個TimeSetting類,它擴展了Thread,在它的run()方法中,我使用setText()在無限循環中調用了updation命令,然后將該方法暫停了一秒鍾。 但是在運行它時,沒有任何反應,在關閉輸出窗口時,我收到一個錯誤,說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>
TimeSetting.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();
}
}
在Application
退出之前, launch()
不會完成。 您應該在應用程序的start
方法中啟動該線程。
此外, new FXMLDocumentController()
顯然創建了一個控制器類的新實例 - 一個沒有連接到任何fxml的實例,因此沒有注入任何字段。 有關與控制器通信的更多信息,請參見: 傳遞參數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) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.