簡體   English   中英

如何在JavaFX中為Label創建一個更新值?

[英]How to create an updating value to Label in JavaFX?

我是Java和JavaFX的新手,我想創建一個帶有Label的GUI,以顯示當前系統時間。 問題是:時間值不會更新。 因此,我想創建一個線程,該線程使用以下命令每1秒更新一次我的時間值和標簽:

label_time_show.setText(curTime);

在inizialize方法(請參見下面的代碼)中,可以使用任何值對“ label_show_time”進行初始化,但是當我嘗試在其他方法中設置setText時,出現以下錯誤:

線程“ Thread-4”中的異常java.lang.NullPointerException

我注釋了代碼中的行,其中Label為null,Label不為null。

有人可以幫我解決這個問題嗎?

public class FXMLDocumentController implements Initializable, Runnable
{
    public String curTime;  // <--- main value of time (String)


    @FXML
    private Label label_time_show;
    @FXML
    private Label label_time;

    // initializer
    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {    
        java.util.Date now = new java.util.Date();
        Long sysTime = now.getTime();
        String sysTimeString = sysTime.toString();    
        Integer h = now.getHours();
        Integer m = now.getMinutes();
        Integer s = now.getSeconds();
        String hh = h.toString();
        String mm = m.toString();
        String ss = s.toString();
        curTime = hh + ":" + mm + ":" + ss;


        label_show_time.setText(curTime); // <---- label_show_time is NOT null            
    }    

    // run method
    @Override
    public void run()
    {
        System.out.println("THREAD FXMLController running....");  
        while(true)
        {        
            time();            
        }        
    }

    public void time()
    {
        java.util.Date now = new java.util.Date();
        Long sysTime = now.getTime();
        String sysTimeString = sysTime.toString();    
        Integer h = now.getHours();
        Integer m = now.getMinutes();
        Integer s = now.getSeconds();
        String hh = h.toString();
        String mm = m.toString();
        String ss = s.toString();
        curTime = hh + ":" + mm + ":" + ss;

        label_show_time.setText(curTime);   // <---- label_show_time is null !!! throws ERROR
    }       
}

因此,鑒於信息有限,我將對此視而不見。 在initialize方法的末尾,您的標簽不為null,因此請啟動線程。

public void initialize(URL url, ResourceBundle rb) 
{    

    //java.util.Date now = new java.util.Date();
    //Long sysTime = now.getTime();
    //String sysTimeString = sysTime.toString();    
    //Integer h = now.getHours();
    //Integer m = now.getMinutes();
    //Integer s = now.getSeconds();
    //String hh = h.toString();
    //String mm = m.toString();
    //String ss = s.toString();
    //curTime = hh + ":" + mm + ":" + ss;
    //label_show_time.setText(curTime); // <---- label_show_time is NOT null 
    //Since the label not null here, start your thread here.
    new Thread(this).start();           
}    

然后,您的time方法將是類似的,除了您將事情發布到平台線程。

public void time()
{
    java.util.Date now = new java.util.Date();
    Long sysTime = now.getTime();
    String sysTimeString = sysTime.toString();    
    Integer h = now.getHours();
    Integer m = now.getMinutes();
    Integer s = now.getSeconds();
    String hh = h.toString();
    String mm = m.toString();
    String ss = s.toString();
    curTime = hh + ":" + mm + ":" + ss;
    Platform.runLater(()->{
        label_show_time.setText(curTime);
    });
}       

您可能希望在run方法中保持睡眠狀態,因為您只需要每秒更新一次。

public void run(){
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    while (true){   
        Date date = new Date();
        label_show_time.setText(dateFormat.format(date));
        try{
            TimeUnit.SECONDS.sleep(1);
        }catch(InterruptedException e){
        }
    }
}

mb它對您有幫助嗎? 並初始化您的標簽。

更新:

 public void initialize(URL url, ResourceBundle rb) {
        ...
 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Date date = new Date();
                    System.out.println(dateFormat.format(date));
                    try{
                        TimeUnit.SECONDS.sleep(1);
                    }catch(InterruptedException e){
                    }
                }
            }
        }).start();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM