如何在JavaFX中为Label创建更新值?

时间:2017-05-23 11:17:27

标签: java multithreading

我是Java和JavaFX的新手,我想创建一个带有Label的GUI,它显示当前的系统时间。 问题是:时间值不会更新。所以我想创建一个Thread,即使用以下命令每1秒更新一次我的Time-Value和Label:

label_time_show.setText(CURTIME);

在inizialize方法中(参见下面的代码),“label_show_time”可以使用任何值进行初始化,但是当我尝试在其他方法中使用setText时,我得到以下错误:

线程“Thread-4”中的异常java.lang.NullPointerException

我在代码中注释了Label所在的行,其中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
    }       
}

2 个答案:

答案 0 :(得分:0)

所以,鉴于信息有限,我会盲目地捅这个。在初始化方法结束时,您的标签不为空,因此请启动您的主题。

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方法中进行休眠,因为您只需要每秒更新一次。

答案 1 :(得分:-1)

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){
        }
    }
}
它对你有帮助吗? 并初始化你的标签。

更新

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