JavaFx选项卡窗格:想要一个控制器用于2个或更多选项卡吗

JavaFx选项卡窗格:想要一个控制器用于2个或更多选项卡吗,java,javafx,javafx-2,fxml,Java,Javafx,Javafx 2,Fxml,我不熟悉javafx。我有一个有3个标签的标签面板。每个选项卡都有许多控件(文本、按钮等),我想为所有选项卡分配一个控制器。SceneBuilder只允许我为整个视图分配一个控制器,我的意思是,只有顶部面板(根)有“控制器类”选项,因此如何为一个类中的所有选项卡编写代码 我将.fxml文件设置为: <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import

我不熟悉javafx。我有一个有3个标签的标签面板。每个选项卡都有许多控件(文本、按钮等),我想为所有选项卡分配一个控制器。SceneBuilder只允许我为整个视图分配一个控制器,我的意思是,只有顶部面板(根)有“控制器类”选项,因此如何为一个类中的所有选项卡编写代码

我将.fxml文件设置为:

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

 <?import javafx.scene.control.*?>
 <?import java.lang.*?>
 <?import javafx.scene.layout.*?>
 <?import javafx.scene.layout.AnchorPane?>
<Pane lns="http://javafx.com/javafx/8"xmlns:fx="http://javafx.com/fxml/1"  
 fx:controller="Application.LoginController">
    <children>   
 <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight=" -
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
tabClosingPolicy="UNAVAILABLE">
        <tabs>
      <Tab text="Register">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" 
    prefWidth="200.0">
                 <children>
                    <Label layoutX="27.0" layoutY="45.0" text="Name" />
                    <Label layoutX="27.0" layoutY="102.0" text="Password" 
/>
                    <Label layoutX="27.0" layoutY="151.0" text="City" />
                    <Label layoutX="27.0" layoutY="204.0" text="Email" />
                    <Label layoutX="27.0" layoutY="246.0" text="Phone" />
                    <TextField fx:id="name" 
 layoutX="164.0"layoutY="41.0"/>
                    <TextField fx:id="passwd" layoutX="164.0" 
  layoutY="98.0" />
                    <TextField fx:id="city" layoutX="164.0" 
 layoutY="147.0" />
                    <TextField fx:id="email" layoutX="164.0" 
 layoutY="200.0" />
                    <TextField fx:id="phone" layoutX="164.0" 
 layoutY="242.0" />
                    <Button fx:id="register" layoutX="129.0" 
 layoutY="308.0" mnemonicParsing="false" text="Register" />
                    <Button fx:id="cancle" cancelButton="true" 
 layoutX="274.0" layoutY="308.0" mnemonicParsing="false" text="Cancle" />
                 </children>
              </AnchorPane>
        </content>
      </Tab>
      <Tab text="Login">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" 
    prefWidth="200.0">
                 <children>
                    <Label layoutX="26.0" layoutY="57.0" text="User Name" 
 />
                    <Label layoutX="26.0" layoutY="103.0" text="Password" 
 />
                    <Button fx:id="myLogin" layoutX="145.0" 
  layoutY="186.0" mnemonicParsing="false" text="Login" />
                    <Button fx:id="cancle" cancelButton="true" 
 layoutX="274.0" layoutY="186.0" mnemonicParsing="false" text="Cancle" />
                    <TextField fx:id="uName" layoutX="145.0" 
 layoutY="53.0" prefHeight="25.0" prefWidth="205.0" />
                    <TextField fx:id="pwd" layoutX="148.0" layoutY="99.0" 
 prefHeight="25.0" prefWidth="200.0" />
                 </children>
              </AnchorPane>
        </content>
      </Tab>
    </tabs>
      </TabPane>
   </children>
 </Pane>

您必须为选项卡创建一个新的
.fxml
文件,然后设置
fx:controller=TabController
,然后才能
因此,您可以包括这3次,然后您将有三个标签与sam控制器

因此,主
.fxml

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

<?import javafx.scene.control.TabPane?>
<TabPane xmlns="http://javafx.com/javafx"
         xmlns:fx="http://javafx.com/fxml"
        fx:controller="TabPaneController">
    <tabs>
        <fx:include fx:id="FirstTab" source="CustomTab.fxml"/>
        <fx:include fx:id="SecondTab" source="CustomTab.fxml"/>
        <fx:include fx:id="ThirdTab" source="CustomTab.fxml"/>
    </tabs>
</TabPane

现在,您有三个选项卡使用相同的
控制器

此应用程序演示如何使用控制器与
选项卡窗格
中不同
选项卡
中的不同
节点
交互

主要

控制器

FXML



的可能重复,但我希望在单个FXML中为多个选项卡使用单个控制器您当前的控制器可以处理与其关联的所有选项卡。不需要为不同的选项卡创建不同的控制器。在几乎所有情况下,FXML文件和控制器类之间都应该一一对应,并且不能将单个FXML拆分为两个控制器。因此,如果您想要一个控制器,请使用一个FXML文件。如果您想要为每个选项卡的内容创建一个控制器,那么您需要为每个选项卡的布局创建一个单独的FXML文件。Thank@sunflame我不想包含另一个。FXML我有一个单独的。FXML文件包含有3个选项卡的选项卡窗格,但当我生成TabController时,它只包含第一个(根)选项卡的控件。所以我想从第二个和第三个选项卡添加控件。我已经编辑了我的答案,检查它是否适合你。
<?xml version="1.0" encoding="UTF-8"?>

        <?import javafx.scene.control.Tab?>
<Tab xmlns="http://javafx.com/javafx"
     xmlns:fx="http://javafx.com/fxml"
     fx:controller="TabController">
<!--content-->
</Tab>
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication151 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{
    //Tab1 nodes
    @FXML private Label lblTab1;
    @FXML private TextField tfTab1;

    //Tab2 nodes
    @FXML private Label lblTab2;
    @FXML private TextField tfTab2;


    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        //This code set the Tab1 label's text to what is show in the TextField on Tab1
        tfTab1.setOnKeyReleased((event)->{
            lblTab1.setText(tfTab1.getText());
        });

        //This code set the Tab2 label's text to what is show in the TextField on Tab2
        tfTab2.setOnKeyReleased((event)->{
            lblTab2.setText(tfTab2.getText());
        });
    }    

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>

<AnchorPane id="AnchorPane" prefHeight="353.0" prefWidth="588.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication151.FXMLDocumentController">
    <children>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <TabPane layoutX="87.0" layoutY="20.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab text="Untitled Tab 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextField fx:id="tfTab1" layoutX="184.0" layoutY="84.0" prefHeight="25.0" prefWidth="220.0" />
                        <StackPane layoutX="27.0" layoutY="143.0" prefHeight="150.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                           <children>
                              <Label fx:id="lblTab1" text="Label" />
                           </children>
                        </StackPane>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="Untitled Tab 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TextField fx:id="tfTab2" layoutX="220.0" layoutY="86.0" />
                        <StackPane layoutX="195.0" layoutY="140.0" prefHeight="150.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                           <children>
                              <Label fx:id="lblTab2" text="Label" />
                           </children>
                        </StackPane>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
        </tabs>
      </TabPane>
    </children>
</AnchorPane>