JAVAFX - 仅使用 1 个窗口更改特定窗格

use*_*740 3 javafx pane

你能帮助我如何更改 1 个场景中的特定窗格吗?

描述

因此,当我想单击菜单 A 时,内容将更改为内容 A。当我单击菜单 B 时,内容将更改为内容 B

我尝试使用 2 FXML 并使用正常方法,例如加载屏幕 A 和屏幕 B

但结果只是改变了窗口。我只想更改 1 个窗口的内容。

有什么建议如何更改 1 个窗口中的特定窗格吗?

gea*_*ker 6

作为一种选择。为任何“内容”制作 FXML 和控制器,并在单击某个按钮时删除旧的“内容”并上传新的“内容”。

下面的工作示例(根据 James_D 评论编辑):

主程序.java

public class Main extends Application {
    Parent root;
    Stage stage;

    @Override
    public void start(Stage primaryStage) {
        try {
            root = FXMLLoader.load(getClass().getResource("Main.fxml"));
            stage = primaryStage;
            stage.setTitle("Stage");

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }   
}
Run Code Online (Sandbox Code Playgroud)

主文件.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<BorderPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <left>
      <ToolBar orientation="VERTICAL" BorderPane.alignment="CENTER">
        <items>
          <Button mnemonicParsing="false" onAction="#onBtnAClick" text="A" />
            <Button mnemonicParsing="false" onAction="#onBtnBClick" text="B" />
            <Button mnemonicParsing="false" onAction="#onBtnCClick" text="C" />
        </items>
      </ToolBar>
   </left>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

控制器.java

import javafx.fxml.FXML;
import javafx.scene.layout.BorderPane;

public class Controller {

    @FXML
    BorderPane mainPane;

    @FXML
    public void onBtnAClick(){
        ContentA contentA = new ContentA();
        mainPane.setCenter(contentA);
    }

    @FXML
    public void onBtnBClick(){
        ContentB contentB = new ContentB();
        mainPane.setCenter(contentB);
    }

    @FXML
    public void onBtnCClick(){
        ContentC contentC = new ContentC();
        mainPane.setCenter(contentC);
    }

}
Run Code Online (Sandbox Code Playgroud)

以及一些内容示例:

内容A.java

public class ContentA extends AnchorPane{

    public ContentA(){
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ContentA.fxml"));

        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

内容A.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<fx:root prefHeight="200.0" prefWidth="300.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ListView layoutX="50.0" layoutY="-26.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</fx:root>
Run Code Online (Sandbox Code Playgroud)