【问题标题】:Resize content with window in FXML (JavaFX)在 FXML (JavaFX) 中使用窗口调整内容大小
【发布时间】:2016-08-07 13:09:08
【问题描述】:

在 FXML (JavaFX) 中按用户调整大小后如何获得新的窗口大小?我搜索了“onResizeWindow”方法,但什么也没找到。

我有一个简单的Pane,我希望它在用户调整窗口大小时自行调整大小。以及如何使用窗格调整工具栏的大小?

这是我的 FXML 代码:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testJavaFXML.Controller">
   <padding>
      <Insets bottom="200.0" left="200.0" right="200.0" top="200.0" />
   </padding>
   <children>
      <ToolBar prefWidth="600.0">
        <items>
            <Button fx:id="buttonPlay" mnemonicParsing="false" onAction="#handlePlayButtonAction" text="Play" />
            <Button fx:id="buttonPause" mnemonicParsing="false" onAction="#handlePauseButtonAction" text="Pause" />
            <Button fx:id="buttonStop" mnemonicParsing="false" onAction="#handleStopButtonAction" text="Stop" />
        </items>
      </ToolBar>
      <Label fx:id="labelHelloWorld" layoutX="250.0" layoutY="187.0" text="Hello World!" />
   </children>
</Pane>

我找到了如何做到这一点:我必须使用包含PaneStackPane,就像这样:

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Pane prefHeight="200.0" prefWidth="200.0">
         <children>
            <ToolBar prefWidth="600.0">
              <items>
                  <Button fx:id="buttonPlay" mnemonicParsing="false" onAction="#handlePlayButtonAction" text="Play" />
                  <Button fx:id="buttonPause" mnemonicParsing="false" onAction="#handlePauseButtonAction" text="Pause" />
                  <Button fx:id="buttonStop" mnemonicParsing="false" onAction="#handleStopButtonAction" text="Stop" />
              </items>
            </ToolBar>
         </children>
      </Pane>
       <Label fx:id="labelHelloWorld" text="Hello World!" />
   </children>
</StackPane>

【问题讨论】:

    标签: java user-interface javafx fxml window-resize


    【解决方案1】:

    你可以听听舞台的宽度和高度:

    stage.widthProperty().addListener((ov, oldWidth, newWidth) -> someAction);
    

    但更好的方法是使用适当的 LayoutPane,例如BorderPane 处理大小调整

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 2015-06-01
      • 2013-07-10
      • 1970-01-01
      • 2017-03-25
      相关资源
      最近更新 更多