简体   繁体   English

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

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

im new to Java and JavaFX and I want to create a GUI with a Label which shows the current system Time. 我是Java和JavaFX的新手,我想创建一个带有Label的GUI,以显示当前系统时间。 The Problem is: the Time Value doesn't update. 问题是:时间值不会更新。 So I wanted to create a Thread, that is updating my Time-Value and also the Label every 1 second with the command: 因此,我想创建一个线程,该线程使用以下命令每1秒更新一次我的时间值和标签:

label_time_show.setText(curTime); label_time_show.setText(curTime);

In the inizialize method (see code below) the "label_show_time" can be inizialized with any value, but when I try to setText in an other method I get the following Error: 在inizialize方法(请参见下面的代码)中,可以使用任何值对“ label_show_time”进行初始化,但是当我尝试在其他方法中设置setText时,出现以下错误:

Exception in thread "Thread-4" java.lang.NullPointerException 线程“ Thread-4”中的异常java.lang.NullPointerException

I commented the line in the Code where the Label is null and where the Label is NOT null. 我注释了代码中的行,其中Label为null,Label不为null。

Can someone help me with this Problem? 有人可以帮我解决这个问题吗?

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
    }       
}

So, given the limited information, I'll take a blind stab at this. 因此,鉴于信息有限,我将对此视而不见。 At the end of your initialize method your label is not null, so start your thread then. 在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();           
}    

Then your time method would be similar except you would post things to the Platform thread. 然后,您的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);
    });
}       

You might want to put a sleep in your run method since you only need to update every second. 您可能希望在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 it helps you? mb它对您有帮助吗? and init your label. 并初始化您的标签。

update: 更新:

 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