Java 动态扩展边框面板(中心元素)

Java 动态扩展边框面板(中心元素),java,javafx,fxml,Java,Javafx,Fxml,我想把FXML文件(AnchorPane)放在FXML文件(BorderPane)的中心,它可以工作,但它不会沿着程序边界延伸,而是有它的标准尺寸,我如何解决这个问题 谢谢 public class Main extends Application { private Stage primaryStage; private BorderPane rootLayout; public void initMenuBar() { try {

我想把FXML文件(AnchorPane)放在FXML文件(BorderPane)的中心,它可以工作,但它不会沿着程序边界延伸,而是有它的标准尺寸,我如何解决这个问题

谢谢

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菜单-我在这里编写菜单栏:

  <?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主窗口-我在这里编写主程序

    <?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>

你怎么知道的

在mainMzenu.fxml中,您有:

<AnchorPane fx:id="mainWindowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="750.0" ...
如果使用场景生成器,则可以选择
MAX\u 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>

.....

@charlesLgn是的,我补充道,我认为不需要他。你能提供完整的fxml-和导入吗?“从好到好总是好的,而不是强迫人们自己添加导入。”PrzemekKrysztofiak更新
<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>