我绝不是 JavaFX 专家,但我能够想出这个解决方案。
这是一个 MCVE,因此您可以完全重新创建此示例应用程序并运行它以查看其实际情况。
该示例使用AnchorPane
对于可重用的 FXML 节点,但您可以修改它以使用Tab
反而。不过,我发现重用内容本身要简单一些。
我正在使用一个简单的Interface
定义我们的选项卡控制器;这允许我们编写一种方法来设置每个内容和单独的控制器Tab
.
主要.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
// Load the main layout
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainLayout.fxml"));
// Show the Stage
primaryStage.setWidth(700);
primaryStage.setHeight(500);
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FXML 文档:
MainLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.reusableTabs.MainController">
<VBox alignment="TOP_CENTER" prefHeight="400.0" prefWidth="600.0" spacing="10.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<Label style="-fx-font-size: 150%; -fx-font-weight: bold;" text="Reusable Tab Content Sample"/>
<Separator prefWidth="200.0"/>
<TabPane fx:id="tabPane" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE"
VBox.vgrow="ALWAYS">
<Tab fx:id="tab1" text="Tab #1"/>
<Tab fx:id="tab2" text="Tab #2"/>
<Tab fx:id="tab3" text="Tab #3"/>
</TabPane>
</VBox>
</AnchorPane>
选项卡内容.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<VBox alignment="CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<Label text="Look - reusable Tab contents!"/>
<Button fx:id="btnTestController" mnemonicParsing="false" text="Test Controller"/>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
</VBox>
</AnchorPane>
控制器:
主控制器.java:
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import java.io.IOException;
public class MainController {
@FXML
private TabPane tabPane;
@FXML
private Tab tab1;
@FXML
private Tab tab2;
@FXML
private Tab tab3;
@FXML
private void initialize() {
// Our TabPane has 3 Tabs. Let's populate them with content, reusing our TabContent.fxml file
setTabContent(tab1, new Tab1Controller());
setTabContent(tab2, new Tab2Controller());
setTabContent(tab3, new Tab3Controller());
}
/**
* Sets the content of Tab to the TabContent.fxml document
*
* @param tab The Tab whose content we wish to set
* @param controller The controller for this particular Tab's content
*/
private void setTabContent(Tab tab, TabController controller) {
// Load our TabContent.fxml file and set it as the content of the Tab that was passed to this method
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("TabContent.fxml"));
// Set the controller for this specific tab
loader.setController(controller);
// Set the content of the passed Tab to this FXML content
tab.setContent(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Tab1控制器:
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Tab1Controller implements TabController {
@FXML
private Button btnTestController;
@FXML
private void initialize() {
// Set our button to print out which controller is being used
btnTestController.setOnAction(event -> System.out.println("Hello, from Controller #1!"));
}
}
只需再重复此控制器两次即可Tab2Controller.java
和`Tab3Controller.java
结果:
单击Test Controller
按钮将打印出来Hello, from Controller #2!
,确认每个选项卡的内容都由它们自己的控制器控制。