2016-11-08 66 views
1

我無法從其他類更改我的標籤的文本。 我需要不斷更新主屏幕上的日期和時間,同時我也可以同時執行其他功能。我已經使用了一個TimeSetting類,該類擴展了Thread,並且在它的run()方法中,我使用setText()在無限循環中調用了updation命令,然後再睡一會兒。 但在運行此,沒有任何反應和關閉輸出窗口,我得到一個錯誤NullPointerExcpetion來自javafx-fxml中不同類的常量標籤更新

下面是兩個類的代碼: 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> 

Tim eSetting.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(); 
} 

} 
+0

我在Netbeans中輸入了您的代碼,它顯示您命名了兩個標籤maxtemp。我認爲應該命名爲mintemp。另外,該程序正在尋找一種啓動按鈕處理方法。如果您的代碼可以編譯,您是如何確定您的問題與標籤有關的? – Sedrick

回答

0

launch()沒有完成,直到Application退出。你應該在應用程序的start方法中啓動線程。

此外new FXMLDocumentController()顯然創建了一個控制器類的新實例 - 一個沒有連接到任何fxml,所以沒有任何字段被注入。有關與控制器通信的更多信息,請訪問: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) { 
    } 
}