单击按钮或调整窗口大小时,布局会“跳转”。

时间:2015-12-27 14:22:02

标签: javafx javafx-8

这个问题与FXML - Text Field Moves To The Right Upon Button Click类似,但对我的情况没有帮助。

我有一个BorderPane,顶部有徽标/文字,中间有主要内容。

viewDidAppear

主要内容是在完成一些加载后显示的几个按钮:

<BorderPane id="mainpane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="900.0" prefWidth="1200.0" stylesheets="@/css/biomat.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <HBox id="maintop" alignment="CENTER" prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER_LEFT">
         <children>
            <Label text="Header">
               <font>
                  <Font name="Verdana" size="48.0" />
               </font>
            </Label>
         </children>
      </HBox>
   </top>
   <center>
      <VBox id="maincenter" alignment="CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <ProgressIndicator prefHeight="97.0" prefWidth="800.0" />
            <Label text="Loading...">
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
            </Label>
         </children>
      </VBox>
   </center>
</BorderPane>

现在是我不理解的有趣部分。单击除第一个(#button1)按钮以外的所有按钮,整个布局向上移动几个像素,它发生一次,然后布局已“稳定”。

我发现的一种解决方法是在使用按钮添加内容后强制进行布局传递。

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<VBox fx:id="mainmenupane" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="0" prefWidth="0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MainMenuController">
   <children>
      <GridPane maxHeight="1.7976931348623157E308" VBox.vgrow="ALWAYS">
        <columnConstraints>
          <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" valignment="CENTER" vgrow="ALWAYS" />
        </rowConstraints>
         <children>
            <Button id="button1" alignment="CENTER" maxHeight="100.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button 1" GridPane.columnIndex="0">
               <font>
                  <Font size="24.0" />
               </font>
               <GridPane.margin>
                  <Insets bottom="10.0" left="50.0" right="50.0" top="10.0" />
               </GridPane.margin>
            </Button>
            <Button id="button2" alignment="CENTER" maxHeight="100.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button 2" GridPane.columnIndex="1">
               <font>
                  <Font size="24.0" />
               </font>
               <GridPane.margin>
                  <Insets bottom="10.0" left="50.0" right="50.0" top="10.0" />
               </GridPane.margin>
            </Button>
         </children>
      </GridPane>
      <GridPane maxHeight="1.7976931348623157E308" VBox.vgrow="ALWAYS">
         <columnConstraints>
            <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints halignment="CENTER" hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints minHeight="10.0" vgrow="ALWAYS" valignment="CENTER" />
         </rowConstraints>
         <children>
            <Button fx:id="button3" alignment="CENTER" maxHeight="100.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button 3" GridPane.columnIndex="0">
               <font>
                  <Font size="24.0" />
               </font>
               <GridPane.margin>
                  <Insets bottom="10.0" left="50.0" right="50.0" top="10.0" />
               </GridPane.margin>
            </Button>
         </children>
      </GridPane>
      <GridPane alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS">
         <columnConstraints>
            <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints minHeight="10.0" vgrow="ALWAYS" valignment="CENTER" />
         </rowConstraints>
         <children>
            <Button fx:id="button4" alignment="CENTER" mnemonicParsing="false" text="Button 4" GridPane.halignment="CENTER" GridPane.hgrow="ALWAYS">
               <font>
                  <Font size="24.0" />
               </font>
            </Button>
         </children>
      </GridPane>
   </children>
</VBox>

显然我不喜欢上面的解决方案,相反,我想了解为什么布局首先没有正确解决。

0 个答案:

没有答案