JavaFX:选项卡内的锚定 Pane 约束

标签 java javafx tabs

我有一个包含多个 fxml 文件的 JavaFX 应用程序,MainPanel 设置如下:

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

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import java.net.URL?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainPanelController">
   <children>
      <TabPane fx:id="mainTabPane" prefHeight="570.0" prefWidth="880.0" tabClosingPolicy="UNAVAILABLE" tabMaxHeight="20.0" tabMaxWidth="3520.0" tabMinHeight="20.0" tabMinWidth="50.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <tabs>
                <Tab fx:id="tab1" text="Tab 1" />
                <Tab fx:id="tab2" text="Tab 2" />
                <Tab fx:id="tab3" text="Tab 3" />
                <Tab fx:id="tab4" text="Tab 4" />
                <Tab fx:id="tab5" text="Tab 5" />
                <Tab fx:id="tab6" text="Tab 6" />
                <Tab fx:id="tab7" text="Tab 7" />
            </tabs>
        </TabPane>
   </children>
</AnchorPane>

整个过程是这样的:

  1. 应用程序的启动方式为:

    Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
    primaryStage.setScene(new Scene(root, 880, 570));
    primaryStage.setTitle("Login");
    primaryStage.show();
    

登录 fxml 为:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<AnchorPane fx:id="mainAnchorPane" prefHeight="550.0" prefWidth="880.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
   <children>
      <VBox layoutX="340.0" layoutY="186.0" spacing="5.0" AnchorPane.bottomAnchor="186.0" AnchorPane.leftAnchor="340.0" AnchorPane.rightAnchor="340.0" AnchorPane.topAnchor="186.0">
         <children>
              <Label alignment="CENTER" prefHeight="73.0" prefWidth="200.0" text="GM-SIS">
                  <font>
                      <Font name="Helvetica" size="60.0" />
                  </font>
              </Label>
              <TextField fx:id="userField" prefHeight="30.0" prefWidth="200.0" />
              <PasswordField fx:id="passwordField" prefHeight="30.0" prefWidth="200.0" />
              <Button fx:id="loginButton" mnemonicParsing="false" onAction="#login" prefHeight="30.0" prefWidth="200.0" text="Login">
                  <font>
                      <Font name="Helvetica" size="13.0" />
                  </font>
              </Button>
         </children>
      </VBox>
        <Label alignment="CENTER" layoutX="270.0" layoutY="265.0" prefHeight="30.0" prefWidth="60.0" text="User">
         <font>
            <Font name="Helvetica" size="13.0" />
         </font>
      </Label>
        <Label alignment="CENTER" layoutX="270.0" layoutY="300.0" prefHeight="30.0" prefWidth="60.0" text="Password" />
   </children>
</AnchorPane>
  • 用户单击登录按钮,MainPanel 加载如下:

    Node mainNode = FXMLLoader.load(Login.class.getResource(fxmlPath));
    mainAnchorPane.getChildren().setAll(mainNode);
    
  • 然后,MainPanel Controller 以类似的方式将每个单独的 fxml 加载到每个选项卡中。我尝试过这样的事情:

    tab1.setContent(loadedNode);
    
  • 还有这个:

    tab1AnchorPane.getChildren().setAll(tab1Node);
    

    我尝试过让选项卡没有子项,然后直接加载到其中。我尝试在选项卡内添加 AnchorPane。我尝试了 HBoxes/VBoxes 与 AnchorPanes 的组合,但没有成功。

    如果我单独加载 fxml 文件,AnchorPane 约束将起作用。但是,当我尝试将其加载到选项卡中时,它不起作用。

    这是一个 fxml,如果单独加载,它可以工作,但在加载到选项卡中时则不起作用:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    
    
    <AnchorPane fx:id="tab1AnchorPane" prefHeight="550.0" prefWidth="880.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Tab1Controller">
        <children>
            <VBox prefHeight="550.0" prefWidth="880.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                <children>
                    <TableView fx:id="table" editable="true" prefHeight="450.0" prefWidth="474.0" VBox.vgrow="ALWAYS">
                        <columns>
                            <TableColumn fx:id="column1" prefWidth="75.0" text="Column 1" />
                            <TableColumn fx:id="column2" prefWidth="75.0" text="Column 2" />
                        </columns>
                        <VBox.margin>
                            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                        </VBox.margin>
                    </TableView>
                    <HBox alignment="CENTER" prefHeight="30.0" prefWidth="880.0" spacing="50.0" VBox.vgrow="ALWAYS">
                        <children>
                            <VBox spacing="50.0">
                                <children>
                                    <Button fx:id="button1" mnemonicParsing="false" onAction="#action1" prefHeight="30.0" prefWidth="150.0" text="Button 1" />
                                    <Button fx:id="button2" mnemonicParsing="false" onAction="#action2" prefHeight="30.0" prefWidth="150.0" text="Button 2" />
                                </children>
                            </VBox>
                            <VBox spacing="10.0">
                                <children>
                                    <Button fx:id="button3" mnemonicParsing="false" onAction="#action3" prefHeight="30.0" prefWidth="150.0" text="Button 3" />
                                    <Button fx:id="button4" mnemonicParsing="false" onAction="#action4" prefHeight="30.0" prefWidth="150.0" text="Button 4" />
                                    <Button fx:id="button5" mnemonicParsing="false" onAction="#action5" prefHeight="30.0" prefWidth="150.0" text="Button 5" />
                                </children>
                            </VBox>
                            <VBox spacing="10.0">
                                <children>
                                    <Button fx:id="button6" mnemonicParsing="false" onAction="#action6" prefHeight="30.0" prefWidth="150.0" text="Button 6" />
                                    <Button fx:id="button7" mnemonicParsing="false" onAction="#action7" prefHeight="30.0" prefWidth="150.0" text="Button 7" />
                                    <Button fx:id="button8" mnemonicParsing="false" onAction="#action8" prefHeight="30.0" prefWidth="150.0" text="Button 8" />
                                </children>
                            </VBox>
                            <VBox spacing="50.0">
                                <children>
                                    <Button fx:id="button9" maxWidth="150.0" mnemonicParsing="false" onAction="#" prefHeight="30.0" text="Button 9" />
                                    <Button fx:id="button10" mnemonicParsing="false" onAction="#action9" prefHeight="30.0" prefWidth="150.0" text="Button 10" />
                                </children>
                            </VBox>
                        </children>
                        <VBox.margin>
                            <Insets bottom="20.0" left="10.0" right="10.0" top="10.0" />
                        </VBox.margin>
                    </HBox>
                </children>
            </VBox>
        </children>
    </AnchorPane>
    

    甚至 MainPanel 内的 TabPane 也无法正确调整大小。我在这里做错了什么?我感觉有点失落。

    最佳答案

    我也遇到了你提到的类似情况。

    当在 fxml 加载的根 Pane 上添加某些节点时,根 Pane 根本没有根据阶段调整大小。 (我怀疑它可能在 JavaFx 上出现错误。)

    因此,我在舞台的 widthProperty(或 heightProperty)上添加监听器,以同时调整舞台根 Pane 的大小。

        Scene      scene = stage.getScene();
        AnchorPane root  = (AnchorPane) scene.getRoot();
        scene.widthProperty().addListener( ( observable, oldValue, newValue ) -> root.setPrefWidth( newValue.doubleValue() ) );
        scene.heightProperty().addListener( ( observable, oldValue, newValue ) -> root.setPrefHeight( newValue.doubleValue() ) );
    

    关于JavaFX:选项卡内的锚定 Pane 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43080997/

    相关文章:

    java - Android、Java - 使用位图帧渲染视频以反转视频 (Xuggler)

    JavaFX |用户名和密码检查无法正常工作

    Javascript 选项卡使用数据属性而不是 ID 来链接按钮和选项卡

    javascript - jQuery 移动选项卡和 anchor

    java - 如何在鼠标拖动时在舞台上设置事件?

    android - 如何在 android studio 1.5.x 中使用选项卡式 Activity ?

    java - Hybris 在单独的浏览器选项卡中打开外部链接

    java - 水平向JPanel添加一个又一个组件

    eclipse - eclipse中导出创建war时的JDK版本

    css - 如何在没有 FXML 的情况下更改 JavaFX 中的场景