如何在JavaFX中缩放窗口时自动居中窗格内容?

时间:2016-04-21 11:24:59

标签: java javafx resize alignment fxml

我的JavaFX项目有点问题。 在这里,您有UI的屏幕截图:

The UI

当我缩放窗口时,它看起来像这样:

The scaled UI

我现在想要将窗口的内容居中,但是菜单栏和搜索栏的颜色应该在整个窗口上,就像它在第二张图片上一样。

以下是用户界面的FXML代码:

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

<?import javafx.scene.chart.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>


<StackPane prefHeight="900.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ezmovie_prototype.controller_home">
   <children>
      <GridPane alignment="TOP_CENTER">
         <columnConstraints>
            <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" prefWidth="1280.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints maxHeight="165.0" minHeight="165.0" prefHeight="165.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="735.0" minHeight="735.0" prefHeight="735.0" vgrow="SOMETIMES" />
         </rowConstraints>
         <children>
            <GridPane alignment="TOP_CENTER" prefHeight="165.0" prefWidth="1280.0" GridPane.halignment="CENTER">
               <columnConstraints>
                  <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints maxHeight="100.0" minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <GridPane prefHeight="100.0" prefWidth="1080.0" style="-fx-background-color: #C60F25;" GridPane.halignment="CENTER">
                    <columnConstraints>
                        <ColumnConstraints fillWidth="false" hgrow="SOMETIMES" maxWidth="160.0" minWidth="160.0" percentWidth="0.0" prefWidth="160.0" />
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="920.0" minWidth="920.0" percentWidth="0.0" prefWidth="920.0" />
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints maxHeight="100.0" minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <TextField maxHeight="-Infinity" maxWidth="920.0" minHeight="-Infinity" minWidth="760.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="40.0" prefWidth="920.0" promptText="Suche nach Film, Serien..." GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.valignment="TOP">
                           <font>
                              <Font size="20.0" />
                           </font>
                           <GridPane.margin>
                              <Insets top="30.0" />
                           </GridPane.margin>
                        </TextField>
                        <Button fx:id="btnSuchen" maxHeight="-Infinity" maxWidth="160.0" minHeight="-Infinity" minWidth="160.0" mnemonicParsing="false" onAction="#test" prefHeight="40.0" prefWidth="160.0" style="-fx-background-color: #FFFFFF;" text="Suchen" GridPane.columnIndex="2" GridPane.halignment="LEFT" GridPane.valignment="TOP">
                           <GridPane.margin>
                              <Insets left="20.0" top="30.0" />
                           </GridPane.margin>
                           <font>
                              <Font name="System Bold" size="20.0" />
                           </font>
                        </Button>
                        <Button maxHeight="70.0" maxWidth="130.0" minHeight="70.0" minWidth="130.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="130.0" text="Button" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/logo.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                     </children>
                  </GridPane>
                  <GridPane prefHeight="65.0" prefWidth="1280.0" style="-fx-background-color: #585858;" GridPane.halignment="CENTER" GridPane.rowIndex="1">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="90.0" prefWidth="90.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="580.0" minWidth="580.0" prefWidth="580.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="0.0" prefWidth="0.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Button fx:id="btnHome" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/home.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                           <GridPane.margin>
                              <Insets />
                           </GridPane.margin>
                        </Button>
                        <Button fx:id="btnFavoriten" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/favoriten.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnListen" alignment="CENTER" contentDisplay="RIGHT" maxHeight="0.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/list.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnSerien" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/series.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnFilme" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/movies.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnLogout" alignment="CENTER" contentDisplay="RIGHT" maxHeight="0.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#logout" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="7" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/logout.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnSettings" alignment="CENTER" contentDisplay="RIGHT" maxHeight="0.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/settings.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                     </children>
                  </GridPane>
               </children>
            </GridPane>
            <BorderPane prefHeight="715.0" prefWidth="1280.0" style="-fx-background-color: FFFFFF;" GridPane.halignment="CENTER" GridPane.rowIndex="1">
               <center>
                  <GridPane style="-fx-background-color: #FFFFFF;" BorderPane.alignment="CENTER">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="1280.0" minWidth="1280.0" prefWidth="1280.0" />
                    </columnConstraints>
                    <rowConstraints>
                        <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="280.0" minHeight="280.0" prefHeight="280.0" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="280.0" minHeight="280.0" prefHeight="280.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <BorderPane prefHeight="230.0" prefWidth="1130.0" style="-fx-border-color: #000000; -fx-border-radius: 10px;" GridPane.rowIndex="1">
                           <opaqueInsets>
                              <Insets bottom="10.0" top="10.0" />
                           </opaqueInsets>
                           <GridPane.margin>
                              <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                           </GridPane.margin>
                           <center>
                              <GridPane maxHeight="200.0" maxWidth="1200.0" minHeight="200.0" minWidth="1200.0" prefHeight="200.0" prefWidth="1200.0" BorderPane.alignment="CENTER">
                                <columnConstraints>
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <BorderPane.margin>
                                    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                 </BorderPane.margin>
                              </GridPane>
                           </center>
                           <bottom>
                              <ScrollBar BorderPane.alignment="CENTER">
                                 <BorderPane.margin>
                                    <Insets bottom="5.0" left="10.0" right="10.0" />
                                 </BorderPane.margin>
                              </ScrollBar>
                           </bottom>
                        </BorderPane>
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Beliebte Filme" GridPane.halignment="LEFT" GridPane.valignment="TOP">
                           <GridPane.margin>
                              <Insets left="20.0" top="20.0" />
                           </GridPane.margin>
                           <font>
                              <Font name="System Bold" size="36.0" />
                           </font>
                        </Text>
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Beliebte Filme" GridPane.halignment="LEFT" GridPane.rowIndex="2" GridPane.valignment="TOP">
                           <font>
                              <Font name="System Bold" size="36.0" />
                           </font>
                           <GridPane.margin>
                              <Insets left="20.0" top="20.0" />
                           </GridPane.margin>
                        </Text>
                        <BorderPane prefHeight="230.0" prefWidth="1130.0" style="-fx-border-color: #000000; -fx-border-radius: 10px;" GridPane.rowIndex="3">
                           <GridPane.margin>
                              <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                           </GridPane.margin>
                           <center>
                              <GridPane maxHeight="200.0" maxWidth="1200.0" minHeight="200.0" minWidth="1200.0" prefHeight="200.0" prefWidth="1200.0" BorderPane.alignment="CENTER">
                                <columnConstraints>
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <BorderPane.margin>
                                    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                 </BorderPane.margin>
                              </GridPane>
                           </center>
                           <bottom>
                              <ScrollBar BorderPane.alignment="CENTER">
                                 <BorderPane.margin>
                                    <Insets bottom="5.0" left="10.0" right="10.0" />
                                 </BorderPane.margin>
                              </ScrollBar>
                           </bottom>
                        </BorderPane>
                     </children>
                  </GridPane>
               </center>
            </BorderPane>
         </children>
      </GridPane>
   </children>
</StackPane>

我是JavaFX的新手,我需要为学校项目编程。

任何建议表示赞赏, 谢谢,蒂莫

编辑: 它应该像使用此命令在css中一样工作:

margin: auto;

谢谢:)

2 个答案:

答案 0 :(得分:1)

你的fxml中应该改变几件事:

  • 不要使用那么多GridPane个。在将GridPane放入GridPane之前请三思而后行。还有其他布局更适合此目的。
  • 您基本上重复了两次 Beliebte Filme 部分,这不仅会增加fxml文件的大小,而且如果您需要更改它们,则需要编辑2个节点。请改用<fx:include>和嵌套控制器。
  • 您使用图像的绝对文件路径。这适用于您的PC,但是一旦您提交了项目,这很可能会停止工作。将这些图像放入您提交的jar中,并使用相对于fxml
  • 的URL
  • 如果使用css文件设置内容的样式,则fxml文件中的重复次数会减少。

关于内容的展示位置:将其放在Group内(如果它不是一个Node)并使用alignment="CENTER"alignment="TOP_CENTER"

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

<?import java.net.URL?>
<?import javafx.scene.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.Font?>

<BorderPane xmlns:fx="http://javafx.com/fxml/1" id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" fx:controller="ezmovie_prototype.controller_home">
    <stylesheets>
        <URL value="@style.css" />
    </stylesheets>
    <top>
        <VBox fillWidth="true">
            <children>
                <HBox styleClass="search-bar">
                    <ImageView fitWidth="130" fitHeight="70" preserveRatio="true">
                        <Image url="@data/icons/logo.png" />
                    </ImageView>
                    <TextField maxHeight="-Infinity" maxWidth="920.0" minHeight="-Infinity" minWidth="760.0" prefHeight="40.0" prefWidth="920.0" promptText="Suche nach Film, Serien..." VBox.vgrow="SOMETIMES" >
                        <font>
                            <Font size="20.0" />
                        </font>
                        <HBox.margin>
                            <Insets top="30.0" />
                        </HBox.margin>
                    </TextField>
                    <Button fx:id="btnSuchen" maxHeight="-Infinity" maxWidth="160.0" minHeight="-Infinity" minWidth="160.0" mnemonicParsing="false" onAction="#test" prefHeight="40.0" prefWidth="160.0" style="-fx-background-color: #FFFFFF;" text="Suchen">
                        <HBox.margin>
                            <Insets left="20.0" top="30.0" />
                        </HBox.margin>
                        <font>
                            <Font name="System Bold" size="20.0" />
                        </font>
                    </Button>
                </HBox>
                <HBox styleClass="tool-bar" maxHeight="80">
                    <children>
                        <Button fx:id="btnHome" mnemonicParsing="false" onAction="#test" text="Home">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/home.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                        <Button fx:id="btnFavoriten" mnemonicParsing="false" onAction="#test" text="Favoriten">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/favoriten.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                        <Button fx:id="btnFilme" mnemonicParsing="false" onAction="#test" text="Filme">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/movies.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                        <Button fx:id="btnSerien" mnemonicParsing="false" onAction="#test" text="Serien">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/series.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                        <Button fx:id="btnListen" mnemonicParsing="false" onAction="#test" text="Listen">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/list.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                        <Region VBox.vgrow="ALWAYS" prefWidth="9E10" prefHeight="1"></Region><!-- Just a empty placeholder for spacing -->
                        <Button fx:id="btnSettings" mnemonicParsing="false" onAction="#test" text="Settings">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/settings.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                        <Button fx:id="btnLogout" mnemonicParsing="false" onAction="#logout" text="Logout">
                            <graphic>
                                <ImageView fitHeight="30" preserveRatio="true">
                                    <image>
                                        <Image url="@data/icons/logout.png" />
                                    </image>
                                </ImageView>
                            </graphic>
                        </Button>
                    </children>
                </HBox>
            </children>
        </VBox>
    </top>
    <center>
        <VBox alignment="TOP_CENTER" fillWidth="false" >
            <children>
                <fx:include fx:id="film1" source="film.fxml" />
                <fx:include fx:id="film2" source="film.fxml" />
            </children>
        </VBox>
    </center>
</BorderPane>

的style.css

.tool-bar {
    -fx-background-color: #585858;
}

.tool-bar > .button {
    -fx-text-fill: white;
    -fx-background-color: transparent;
    -fx-content-display: top;
    -fx-max-width: 60;
    -fx-min-width: 60;
    -fx-max-height: 60;
    -fx-min-height: 60;
    -fx-shape: "M0,0H1V1H0Z";
}

.tool-bar > .button:hover {
    -fx-background-color: #777;
}

.search-bar {
    -fx-alignment: center-left;
    -fx-background-color: #C60F25;
    -fx-max-height: 100;
    -fx-min-height: 100;
}

.search-bar > * {
    -fx-margin: 100;
}

.films > .content {
    -fx-border-color: black;
    -fx-border-radius: 10px;
}

film.fxml

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

<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.text.*?>

<VBox styleClass="films" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ezmovie_prototype.FilmController">
    <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Beliebte Filme">
        <VBox.margin>
            <Insets left="20.0" top="20.0" />
        </VBox.margin>
        <font>
            <Font name="System Bold" size="36.0" />
        </font>
    </Text>
    <BorderPane styleClass="content" prefHeight="230.0" prefWidth="1130.0">
        <VBox.margin>
            <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
        </VBox.margin>
        <center>
            <GridPane maxHeight="200.0" maxWidth="1200.0" minHeight="200.0" minWidth="1200.0" prefHeight="200.0" prefWidth="1200.0" BorderPane.alignment="CENTER">
                <columnConstraints>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                </columnConstraints>
                <rowConstraints>
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                </rowConstraints>
                <BorderPane.margin>
                    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                </BorderPane.margin>
            </GridPane>
        </center>
        <bottom>
            <ScrollBar BorderPane.alignment="CENTER">
                <BorderPane.margin>
                    <Insets bottom="5.0" left="10.0" right="10.0" />
                </BorderPane.margin>
            </ScrollBar>
        </bottom>
    </BorderPane>
</VBox>

控制器

@FXML
private FilmController film1Controller;
@FXML
private FilmController film2Controller;

答案 1 :(得分:0)

我在代码中的每个位置都用maxWidth="1280.0"替换了maxWidth="9999.0"

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

<?import javafx.scene.chart.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>


<StackPane prefHeight="900.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ezmovie_prototype.controller_home">
   <children>
      <GridPane alignment="TOP_CENTER">
         <columnConstraints>
            <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" prefWidth="1280.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints maxHeight="165.0" minHeight="165.0" prefHeight="165.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="735.0" minHeight="735.0" prefHeight="735.0" vgrow="SOMETIMES" />
         </rowConstraints>
         <children>
            <GridPane alignment="TOP_CENTER" prefHeight="165.0" prefWidth="1280.0" GridPane.halignment="CENTER">
               <columnConstraints>
                  <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints maxHeight="100.0" minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <GridPane prefHeight="100.0" prefWidth="1080.0" style="-fx-background-color: #C60F25;" GridPane.halignment="CENTER">
                    <columnConstraints>
                        <ColumnConstraints fillWidth="false" hgrow="SOMETIMES" maxWidth="160.0" minWidth="160.0" percentWidth="0.0" prefWidth="160.0" />
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="920.0" minWidth="920.0" percentWidth="0.0" prefWidth="920.0" />
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints maxHeight="100.0" minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <TextField maxHeight="-Infinity" maxWidth="920.0" minHeight="-Infinity" minWidth="760.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="40.0" prefWidth="920.0" promptText="Suche nach Film, Serien..." GridPane.columnIndex="1" GridPane.halignment="LEFT" GridPane.valignment="TOP">
                           <font>
                              <Font size="20.0" />
                           </font>
                           <GridPane.margin>
                              <Insets top="30.0" />
                           </GridPane.margin>
                        </TextField>
                        <Button fx:id="btnSuchen" maxHeight="-Infinity" maxWidth="160.0" minHeight="-Infinity" minWidth="160.0" mnemonicParsing="false" onAction="#test" prefHeight="40.0" prefWidth="160.0" style="-fx-background-color: #FFFFFF;" text="Suchen" GridPane.columnIndex="2" GridPane.halignment="LEFT" GridPane.valignment="TOP">
                           <GridPane.margin>
                              <Insets left="20.0" top="30.0" />
                           </GridPane.margin>
                           <font>
                              <Font name="System Bold" size="20.0" />
                           </font>
                        </Button>
                        <Button maxHeight="70.0" maxWidth="130.0" minHeight="70.0" minWidth="130.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="130.0" text="Button" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/logo.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                     </children>
                  </GridPane>
                  <GridPane prefHeight="65.0" prefWidth="1280.0" style="-fx-background-color: #585858;" GridPane.halignment="CENTER" GridPane.rowIndex="1">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="90.0" prefWidth="90.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="580.0" minWidth="580.0" prefWidth="580.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="0.0" prefWidth="0.0" />
                        <ColumnConstraints hgrow="SOMETIMES" maxWidth="100.0" minWidth="100.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Button fx:id="btnHome" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/home.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                           <GridPane.margin>
                              <Insets />
                           </GridPane.margin>
                        </Button>
                        <Button fx:id="btnFavoriten" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/favoriten.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnListen" alignment="CENTER" contentDisplay="RIGHT" maxHeight="0.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/list.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnSerien" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/series.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnFilme" alignment="CENTER" contentDisplay="RIGHT" maxHeight="60.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/movies.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnLogout" alignment="CENTER" contentDisplay="RIGHT" maxHeight="0.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#logout" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="7" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/logout.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                        <Button fx:id="btnSettings" alignment="CENTER" contentDisplay="RIGHT" maxHeight="0.0" maxWidth="60.0" minHeight="60.0" minWidth="60.0" mnemonicParsing="false" onAction="#test" prefHeight="60.0" prefWidth="60.0" text="Home" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
                           <effect>
                              <ImageInput>
                                 <source>
                                    <Image url="file:/C:/Users/Administrator/workspace/ezmovie_prototype/data/icons/settings.png" />
                                 </source>
                              </ImageInput>
                           </effect>
                        </Button>
                     </children>
                  </GridPane>
               </children>
            </GridPane>
            <BorderPane prefHeight="715.0" prefWidth="1280.0" style="-fx-background-color: FFFFFF;" GridPane.halignment="CENTER" GridPane.rowIndex="1">
               <center>
                  <GridPane style="-fx-background-color: #FFFFFF;" BorderPane.alignment="CENTER">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" maxWidth="9999.0" minWidth="1280.0" prefWidth="1280.0" />
                    </columnConstraints>
                    <rowConstraints>
                        <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="280.0" minHeight="280.0" prefHeight="280.0" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="65.0" minHeight="65.0" prefHeight="65.0" vgrow="SOMETIMES" />
                      <RowConstraints maxHeight="280.0" minHeight="280.0" prefHeight="280.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <BorderPane prefHeight="230.0" prefWidth="1130.0" style="-fx-border-color: #000000; -fx-border-radius: 10px;" GridPane.rowIndex="1">
                           <opaqueInsets>
                              <Insets bottom="10.0" top="10.0" />
                           </opaqueInsets>
                           <GridPane.margin>
                              <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                           </GridPane.margin>
                           <center>
                              <GridPane maxHeight="200.0" maxWidth="1200.0" minHeight="200.0" minWidth="1200.0" prefHeight="200.0" prefWidth="1200.0" BorderPane.alignment="CENTER">
                                <columnConstraints>
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <BorderPane.margin>
                                    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                 </BorderPane.margin>
                              </GridPane>
                           </center>
                           <bottom>
                              <ScrollBar BorderPane.alignment="CENTER">
                                 <BorderPane.margin>
                                    <Insets bottom="5.0" left="10.0" right="10.0" />
                                 </BorderPane.margin>
                              </ScrollBar>
                           </bottom>
                        </BorderPane>
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Beliebte Filme" GridPane.halignment="LEFT" GridPane.valignment="TOP">
                           <GridPane.margin>
                              <Insets left="20.0" top="20.0" />
                           </GridPane.margin>
                           <font>
                              <Font name="System Bold" size="36.0" />
                           </font>
                        </Text>
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Beliebte Filme" GridPane.halignment="LEFT" GridPane.rowIndex="2" GridPane.valignment="TOP">
                           <font>
                              <Font name="System Bold" size="36.0" />
                           </font>
                           <GridPane.margin>
                              <Insets left="20.0" top="20.0" />
                           </GridPane.margin>
                        </Text>
                        <BorderPane prefHeight="230.0" prefWidth="1130.0" style="-fx-border-color: #000000; -fx-border-radius: 10px;" GridPane.rowIndex="3">
                           <GridPane.margin>
                              <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                           </GridPane.margin>
                           <center>
                              <GridPane maxHeight="200.0" maxWidth="1200.0" minHeight="200.0" minWidth="1200.0" prefHeight="200.0" prefWidth="1200.0" BorderPane.alignment="CENTER">
                                <columnConstraints>
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <BorderPane.margin>
                                    <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                 </BorderPane.margin>
                              </GridPane>
                           </center>
                           <bottom>
                              <ScrollBar BorderPane.alignment="CENTER">
                                 <BorderPane.margin>
                                    <Insets bottom="5.0" left="10.0" right="10.0" />
                                 </BorderPane.margin>
                              </ScrollBar>
                           </bottom>
                        </BorderPane>
                     </children>
                  </GridPane>
               </center>
            </BorderPane>
         </children>
      </GridPane>
   </children>
</StackPane>

这就是为我做的。

请记住我对你的评论。

编辑:我需要删除指向控制器的链接,否则我无法对其进行测试,因此请勿将其复制并粘贴回您的应用中, ?