问题 我想在运行时将通过javafx场景构建器构建的自定义面板添加到网格窗格.我自定义的面板存在按钮,标签等.
我的尝试 我试图从窗格扩展...
public class Celli extends Pane{
public Celli() throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("Cell.fxml"));
this.getChildren().add(root);
}
}
Run Code Online (Sandbox Code Playgroud)
...然后在控制器的添加方法中使用此面板
@FXML
private void textChange(KeyEvent event) {
GridPane g = new GridPane();
for (int i=0 : i<100; i++){
g.getChildren().add(new Celli());
}
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但表现非常差.
我正在寻找的 是有没有办法通过javafx场景构建器设计面板(因此在fxml中有这个面板),然后在运行时将它添加到gridpane而不为每个实例使用这个fxmlloader.我认为它因fxml加载器而表现不佳.当我添加一个标准按钮,例如whitout fxml时,速度要快得多.
所以,我想要做的是,使用fxml创建一个自定义元素,然后将该元素的几个实例添加到容器中,如GridPane."new"运算符对我不起作用,因为我想使用@fxml注释器来访问该元素.克隆会很好,但它不起作用.当在for()contruct中使用添加许多元素时,FXMLLoader非常慢.如果我可以写一个fxml parentnode的引用,它可以从控制器调用,那将是完美的.
对不起...这里伪...
public class Controller implements Initializable {
@FXML
private VBox stack;
@FXML
private Button button;
@FXML
private void Change(KeyEvent event) throws IOException {
for (int i=0; i<10; i++){
stack.getChildren().add(button);
}
}
}
Run Code Online (Sandbox Code Playgroud)
将THE按钮添加到VBox是没有问题的.但是在for-contruct中(添加多于一个按钮)它失败了.我可以在for构造中使用new运算符,但我想知道,如果这是唯一的可能性.我认为必须有另一种方式,例如使用@FXML注释器"获取"按钮然后复制它.