JavaFX中的子场景不显示从FXML文件加载的元素
我使用FXML文件(在SceneBuilder中创建)创建了一个程序,其中包含四个子场景:JavaFX中的子场景不显示从FXML文件加载的元素,java,javafx,Java,Javafx,我使用FXML文件(在SceneBuilder中创建)创建了一个程序,其中包含四个子场景: @FXML public SubScene p1sub; @FXML public SubScene p2sub; @FXML public SubScene p3sub; @FXML public SubScene p4sub; 这些子场景中的每一个都与其他子场景几乎相同 我可以让根节点(包含这些节点)显示得很好,但是当我尝试添加子场景时,它们不会显示 //This is the code I use
@FXML
public SubScene p1sub;
@FXML
public SubScene p2sub;
@FXML
public SubScene p3sub;
@FXML
public SubScene p4sub;
这些子场景中的每一个都与其他子场景几乎相同
我可以让根节点(包含这些节点)显示得很好,但是当我尝试添加子场景时,它们不会显示
//This is the code I use to initialize one of the four.
Parent root2a = null;
try {
FXMLLoader loader2 = new FXMLLoader(getClass().getResource(
"PlayerConfigurationSubScreen.fxml"));
root2a = (Parent) loader2.load();
} catch (Exception e) {
/*code*/
}
/*code*/
if (root2a != null) {
System.out.println("root2 isn't null");
p1sub = new SubScene(root2a, 149, 260);
}
/*code*/
stage.show();
你知道怎么让他们出现吗?我是JavaFX的新手。我认为您处理问题的方式不对。根据我对代码的理解,您希望加载一个子场景,然后将其分配给预定义的“插槽”之一 但是,这将不起作用,因为预定义的窗与实际渲染的内容无关。在窗口中绘制的所有内容都必须指定为某种容器的子对象 我在这里举了一个例子,再现了您面临的问题:
public class ReplaceTest extends Application {
@FXML Button button;
@FXML Label oldLabel;
@Override
public void start(Stage primaryStage) throws IOException{
Parent root = FXMLLoader.load( getClass().getResource("ReplaceTest.fxml") );
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private static int i = 0;
@FXML protected void simpleClick(ActionEvent e){
Label newLabel = new Label("Hello! " + i++);
oldLabel = newLabel;
System.out.println("Nothing happens...");
}
}
以及FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="200.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="replaceTest.ReplaceTest">
<children>
<Label fx:id="oldLabel" text="TEXT TO BE REPLACED" />
<Button fx:id="button" onAction="#simpleClick" text="Replace label" />
</children>
<padding>
<Insets top="10.0" />
</padding>
</VBox>
以及FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="200.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="replaceTest.ReplaceTest">
<children>
<VBox fx:id="wrappingBox" alignment="TOP_CENTER">
<children>
<Label text="TEXT TO BE REPLACED" />
</children>
</VBox>
<Button fx:id="button" onAction="#simpleClick" text="Replace label" />
</children>
<padding>
<Insets top="10.0" />
</padding>
</VBox>
在fxml中设置子场景维度,只需将父场景添加到子场景,而不是“新建”
我有点困惑。
PlayerConfigurationSubScreen.fxml
是否包含所有4个子场景?如果是,您试图通过p1sub=newsubscene(root2a、149、260)实现什么代码>?
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="200.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="replaceTest.ReplaceTest">
<children>
<VBox fx:id="wrappingBox" alignment="TOP_CENTER">
<children>
<Label text="TEXT TO BE REPLACED" />
</children>
</VBox>
<Button fx:id="button" onAction="#simpleClick" text="Replace label" />
</children>
<padding>
<Insets top="10.0" />
</padding>
</VBox>
p1sub.setRoot(root2a);