簡體   English   中英

在JavaFX中縮放窗口時,如何自動將窗格的內容居中?

[英]How can i automatically center the content of a pane while scaling the window in JavaFX?

我的JavaFX項目有一個小問題。 這里有用戶界面的屏幕截圖:

The UI

當我縮放窗口時,它看起來像這樣:

縮放的用戶界面

我現在想將窗口的內容居中,但是菜單欄和搜索欄的顏色應該在整個窗口上,就像在第二張圖片上一樣。

這是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;

謝謝 :)

在fxml中應更改幾件事:

  • 不要使用那么多的GridPane 在將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;

我取代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>

這為我做到了。

並且請記住我對您的問題的評論。

編輯:我需要刪除到控制器的鏈接,否則我將無法對其進行測試,所以不要只是將其復制並粘貼回您的應用程序中,好嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM