[英]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.