当程序在javafx中运行时,如何向布局添加或删除节点
我正在用javafx编写一个代码,它基于一个字符串构建一个布局,在我按下一个特定的按钮后,它将读取另一个字符串并构建一个完全不同的布局。代码如下:当程序在javafx中运行时,如何向布局添加或删除节点,java,javafx,layout,task,Java,Javafx,Layout,Task,我正在用javafx编写一个代码,它基于一个字符串构建一个布局,在我按下一个特定的按钮后,它将读取另一个字符串并构建一个完全不同的布局。代码如下: Service<Void> cicle = new Service<Void>() { @Override protected Task<Void> createTask() { return new Task<Void>() { @Overrid
Service<Void> cicle = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
String[] map = { "10", "10", "logo" };
boolean waiting = false;
Timer.start();
while (Timer.update() / 1000 < time) {
if (!waiting) {
for (int i = 0; i < map.length; i += 3) {
Label gigi = new Label("hi");
gigi.setLayoutX(Integer.parseInt(map[i]));
gigi.setLayoutX(Integer.parseInt(map[i + 1]));
// HERE is where i shoul add the label gigi
}
waiting = true;
}
System.out.println(Timer.update());
}
end();
return null;
}
};
}
};
Service cicle=新服务(){
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的Void调用()引发异常{
字符串[]映射={“10”,“10”,“logo”};
布尔等待=假;
Timer.start();
while(Timer.update()/1000
但是如果我使用layout.getChildrens().add(node)它不起作用,如果我完全为舞台重建场景,它会停止程序刚刚使用的Platform.runLater,它会起作用,我无法翻译标签,因为现在我添加它是为了调试,但将来它会读取字符串中要添加的内容[]mapYou不应该从后台线程更新GUI。使用
Platform.runLater
执行GUI更新(在本例中,添加标签
s)。顺便问一下:延迟end()
调用的目的是什么?为什么不将循环重写为if(Timer.update()/1000
@fabian我延迟end()的调用,因为我必须在特定的时间限制(Timer.update