JavaFX 场景生成器:使用托管=“假”和可见=“假”切换表单

问题描述 投票:0回答:1

我试图在 JavaFX Scene Builder 23.0.1 中找到“托管”属性,但找不到它。还有其他人有想法吗?以下是示例代码:

包名:包com.example.demo;

HelloApplication.java

public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
    Scene scene = new Scene(fxmlLoader.load(), 320, 240);
    stage.setTitle("Hello!");
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch();
}

}

HelloController.java

    public class HelloController {
    public VBox firstVBox;
    public Button startChat;
    public VBox secondBox;
    public TextArea chatArea;

    public void switchForm(ActionEvent actionEvent) {
        this.firstVBox.setManaged(false);
        this.startChat.setVisible(false);
        this.secondBox.setManaged(true);
        this.chatArea.setVisible(true);
    }
}

hello-view.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.control.TextArea?>
<StackPane
    xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.demo.HelloController">
    <VBox fx:id="firstVBox" alignment="CENTER" spacing="20.0">
        <Button fx:id="startChat" text="Start chat!" onAction="#switchForm"/>
    </VBox>
    <VBox fx:id="secondBox" alignment="CENTER" spacing="20.0" managed="false">
        <TextArea fx:id="chatArea" text="How are you doing?" visible="false"/>
    </VBox>
</StackPane>
java javafx fxml scenebuilder
1个回答
0
投票

通过阅读您的评论,我决定将 FXML 文件“hello-view.fxml”拆分为更小的文件(secondBox.fxml)。该解决方案很干净并且遵循良好的关注点分离原则。 UI 更加模块化且易于维护。我使用 StackPane 作为根布局,使用 BorderPane 作为主要内容布局。两者结合起来可以很好地实现居中和灵活更换。

hello-view.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.BorderPane?>
<StackPane
    xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.demo.HelloController">
    <BorderPane fx:id="borderPane">
        <center>
            <VBox fx:id="firstVBox" alignment="CENTER" spacing="20.0">
                <Button fx:id="startChat" text="Start chat!" onAction="#switchForm" VBox.vgrow="ALWAYS"/>
            </VBox>
        </center>
    </BorderPane>
</StackPane>

第二个Box.fxml

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox
    xmlns="http://javafx.com/javafx"
    xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.example.demo.SecondBox" fx:id="secondBox" alignment="CENTER" spacing="20.0">
    <TextArea fx:id="chatArea" text="How are you doing?" visible="false" VBox.vgrow="ALWAYS"/>
</VBox>

HelloController.java 类现在如下所示:

import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

import java.io.IOException;

public class HelloController {
    public VBox firstVBox;
    public Button startChat;
    public BorderPane borderPane;

    public void switchForm() throws IOException {
        borderPane.setCenter(new FXMLLoader(HelloController.class.getResource("secondBox.fxml")).load());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.