动态扩展BorderPanel(中心元素)

时间:2018-12-18 08:16:07

标签: java javafx fxml

我想将FXML文件(AnchorPane)放在FXML文件(BorderPane)的中心并且可以工作,但是它不能沿程序边界延伸,但是具有标准尺寸,如何解决此问题? / p>

谢谢。

public class Main extends Application {
    private Stage primaryStage;
    private BorderPane rootLayout;

    public void initMenuBar() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("../view/fxml/menu.fxml"));
            rootLayout = loader.load();

            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initMainWindow() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("../view/fxml/mainWindow.fxml"));
            AnchorPane personOverview = loader.load();

            rootLayout.setCenter(personOverview);

            MainWindowController controller = loader.getController();
            controller.setMain(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("AddressApp");

        initMenuBar();

        initMainWindow();
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

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

FXML菜单-我在这里编写menuBar:

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

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="325.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #ccccff;" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

FXML mainWindow-我在这里编写主程序

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

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

<AnchorPane fx:id="mainWindowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" style="-fx-background-color: #ccccff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
   <children>
      <SplitPane fx:id="mainWindowPane2" dividerPositions="0.7591973244147158" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <TableView prefHeight="398.0" prefWidth="451.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn prefWidth="75.0" text="Сайт" />
                      <TableColumn prefWidth="75.0" text="Почта" />
                        <TableColumn prefWidth="75.0" text="Логин" />
                        <TableColumn prefWidth="75.0" text="Пароль" />
                    </columns>
                     <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                     </columnResizePolicy>
                  </TableView>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Label layoutX="5.0" layoutY="6.0" text="Дополнительная информация" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="6.0" />
                  <Label layoutX="5.0" layoutY="33.0" text="Label" />
               </children>
            </AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

  

怎么办?

the result

在mainMzenu.fxml中,您具有:

<AnchorPane fx:id="mainWindowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" ...

是什么意思?

minHeight="-Infinity"的意思是minHeight=prefHeight

所以您将最小值设为整块,这没有问题。问题是您还用maxHeight="-Infinity" maxWidth="-Infinity"塞住了蜡,所以您需要做的就是更改该值。

maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
  

如果您使用场景生成器,则可以选择MAX_VAL

结果:

<AnchorPane fx:id="mainWindowPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" style="-fx-background-color: #ccccff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
  <children>
       .....
  </children>
</AnchorPane>

您还可以删除值:

<AnchorPane fx:id="mainWindowPane" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" style="-fx-background-color: #ccccff;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="model.MainWindowController">
  <children>
       .....
  </children>
</AnchorPane>