[英]SubScene in JavaFX does not show elements loaded from an FXML file
我有一個使用 FXML 文件(在 SceneBuilder 中制作)創建的程序,其中包含四個子場景:
@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 的新手。
在 fxml 中設置子場景尺寸,而不是“新”,只需將父級添加到子場景。
....
p1sub.setRoot(root2a);
....
我認為您以錯誤的方式處理問題。 從我從您的代碼中了解到,您想加載一個子場景,然后將其分配給您預定義的“插槽”之一。
然而,這不起作用,因為預定義的插槽與實際呈現的內容無關。 在窗口中繪制的所有內容都必須指定為某種容器的子項。
我在這里做了一個例子來重現您面臨的問題:
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>
如果你試試這個例子,你會看到覆蓋 oldLabel 不會影響渲染窗口。 相反,您可以嘗試創建將新子場景添加為子場景的區域。 這將更新渲染窗口並(希望)產生您想要的行為。 同樣,我做了一個例子,你可以參考:
public class ReplaceTest extends Application {
@FXML Button button;
@FXML VBox wrappingBox;
@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++);
wrappingBox.getChildren().clear();
wrappingBox.getChildren().add(newLabel);
System.out.println("Someting 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>
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.