我使用场景生成器创建了这个计算器原型,它与设置的宽度和高度(376x752)完美配合,但在尝试调整窗口大小时遇到了很多问题。如何防止窗口不能小于我已经设置的宽度和高度?另外,当最大化窗口时,包含标签的 VBox 会调整大小以填充屏幕上的整个水平空间,但是操作按钮仍位于其位置。如何使按钮放大以填充最大化窗口后创建的水平空间? (我正在尝试实现类似于 Windows 计算器的行为)
<AnchorPane minHeight="752.0" minWidth="376.0" style="-fx-background-color: #17181a;" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="muri.calc.HelloController">
<VBox alignment="BOTTOM_RIGHT" layoutX="34.0" layoutY="138.0" minHeight="130.0" minWidth="312.0" AnchorPane.leftAnchor="34.0" AnchorPane.rightAnchor="32.0">
<children>
<Label fx:id="resultadoTexto" alignment="CENTER" contentDisplay="CENTER" text="82" textAlignment="RIGHT" textFill="WHITE">
<font>
<Font size="48.0" />
</font>
</Label>
</children>
</VBox>
<!-- Grid for Buttons -->
<GridPane hgap="20" layoutX="32" layoutY="670" prefWidth="310.0" vgap="14" AnchorPane.bottomAnchor="32.0" AnchorPane.leftAnchor="34.0" AnchorPane.rightAnchor="32.0">
<Button fx:id="quadradoBotao" minHeight="62.0" minWidth="62.0" mnemonicParsing="false" prefHeight="62.0" prefWidth="62.0" text="n²" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Button fx:id="porcentBotao" minHeight="62.0" minWidth="62.0" mnemonicParsing="false" prefHeight="62.0" prefWidth="62.0" text="\%" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button fx:id="raizBotao" minHeight="62.0" minWidth="62.0" mnemonicParsing="false" prefHeight="62.0" prefWidth="62.0" text="√" GridPane.rowIndex="1" />
<!-- First Row -->
<Button fx:id="acBotao" minHeight="31.0" minWidth="62" text="Ac" />
<Button fx:id="dividirBotao" minHeight="62" minWidth="62" text="/" GridPane.columnIndex="3" GridPane.rowIndex="1" />
<Button fx:id="multiplicarBotao" minHeight="62" minWidth="62" text="*" textAlignment="JUSTIFY" GridPane.columnIndex="3" GridPane.rowIndex="2" />
<Button fx:id="subtrairBotao" minHeight="62.0" minWidth="62" prefHeight="63.0" prefWidth="63.0" text="-" GridPane.columnIndex="3" GridPane.rowIndex="3" />
<!-- Second Row -->
<Button fx:id="seteBotao" minHeight="62" minWidth="62" text="7" GridPane.rowIndex="2" />
<Button fx:id="oitoBotao" minHeight="62" minWidth="62" text="8" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button fx:id="noveBotao" minHeight="62" minWidth="62" text="9" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<!-- Third Row -->
<Button fx:id="quatroBotao" minHeight="62" minWidth="62" text="4" GridPane.rowIndex="3" />
<Button fx:id="cincoBotao" minHeight="62" minWidth="62" text="5" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Button fx:id="seisBotao" minHeight="62" minWidth="62" text="6" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<Button fx:id="somarBotao" minHeight="62.0" minWidth="62" text="+" GridPane.columnIndex="3" GridPane.rowIndex="4" />
<!-- Fourth Row -->
<Button fx:id="umBotao" minHeight="62" minWidth="62" text="1" GridPane.rowIndex="4" />
<Button fx:id="doisBotao" minHeight="62" minWidth="62" text="2" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Button fx:id="tresBotao" minHeight="62" minWidth="62" text="3" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<!-- Fifth Row -->
<Button fx:id="alterarBotao" minHeight="62" minWidth="62" text="+/-" GridPane.rowIndex="5" />
<Button fx:id="zeroBotao" minHeight="62" minWidth="62" text="0" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Button fx:id="decimalBotao" minHeight="62" minWidth="62" text="." GridPane.columnIndex="2" GridPane.rowIndex="5" />
<Button fx:id="igualBotao" minHeight="62.0" minWidth="62" text="=" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Button fx:id="apagarBotao" layoutX="258.0" layoutY="54.0" minHeight="31.0" minWidth="62" prefHeight="31.0" prefWidth="62.0" text="⌫" GridPane.columnIndex="3" />
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
</GridPane>
</AnchorPane>
我已经尝试更改网格窗格行的百分比宽度和 hgrow,但它不起作用。 抱歉我的英语不好顺便说一句
我已经修改了你的FXML,我相信你需要调整的属性都在所有按钮上。您应该将
hgrow
和 vgrow
设置为 SOMETIMES
,并将 maxWidth
和 maxHeight
设置为 MAX_VALUE
。
此外,我还修改了
GridPane
的顶部锚点,以阻止按钮在 VBox
上方移动,并删除了按钮的硬编码宽度和高度。
这是修改后的 FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane minHeight="752.0" minWidth="376.0" style="-fx-background-color: #17181a;" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="muri.calc.HelloController">
<VBox alignment="BOTTOM_RIGHT" layoutX="34.0" layoutY="138.0" minHeight="130.0" minWidth="312.0" AnchorPane.leftAnchor="34.0" AnchorPane.rightAnchor="32.0" AnchorPane.topAnchor="138.0">
<children>
<Label fx:id="resultadoTexto" alignment="CENTER" contentDisplay="CENTER" text="82" textAlignment="RIGHT" textFill="WHITE">
<font>
<Font size="48.0" />
</font>
</Label>
</children>
</VBox>
<!-- Grid for Buttons -->
<GridPane hgap="20" layoutX="32" layoutY="670" prefWidth="310.0" vgap="14" AnchorPane.bottomAnchor="32.0" AnchorPane.leftAnchor="34.0" AnchorPane.rightAnchor="32.0" AnchorPane.topAnchor="295.0">
<Button fx:id="quadradoBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62.0" minWidth="62.0" mnemonicParsing="false" text="n²" GridPane.columnIndex="2" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1" GridPane.vgrow="SOMETIMES" />
<Button fx:id="porcentBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62.0" minWidth="62.0" mnemonicParsing="false" text="\%" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1" GridPane.vgrow="SOMETIMES" />
<Button fx:id="raizBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62.0" minWidth="62.0" mnemonicParsing="false" text="√" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1" GridPane.vgrow="SOMETIMES" />
<!-- First Row -->
<Button fx:id="acBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="31.0" minWidth="62" text="Ac" GridPane.hgrow="SOMETIMES" GridPane.vgrow="SOMETIMES" />
<Button fx:id="dividirBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="/" GridPane.columnIndex="3" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="1" GridPane.vgrow="SOMETIMES" />
<Button fx:id="multiplicarBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="*" textAlignment="JUSTIFY" GridPane.columnIndex="3" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="2" GridPane.vgrow="SOMETIMES" />
<Button fx:id="subtrairBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62.0" minWidth="62" text="-" GridPane.columnIndex="3" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="3" GridPane.vgrow="SOMETIMES" />
<!-- Second Row -->
<Button fx:id="seteBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="7" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="2" GridPane.vgrow="SOMETIMES" />
<Button fx:id="oitoBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="8" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="2" GridPane.vgrow="SOMETIMES" />
<Button fx:id="noveBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="9" GridPane.columnIndex="2" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="2" GridPane.vgrow="SOMETIMES" />
<!-- Third Row -->
<Button fx:id="quatroBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="4" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="3" GridPane.vgrow="SOMETIMES" />
<Button fx:id="cincoBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="5" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="3" GridPane.vgrow="SOMETIMES" />
<Button fx:id="seisBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="6" GridPane.columnIndex="2" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="3" GridPane.vgrow="SOMETIMES" />
<Button fx:id="somarBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62.0" minWidth="62" text="+" GridPane.columnIndex="3" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="4" GridPane.vgrow="SOMETIMES" />
<!-- Fourth Row -->
<Button fx:id="umBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="4" GridPane.vgrow="SOMETIMES" />
<Button fx:id="doisBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="2" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="4" GridPane.vgrow="SOMETIMES" />
<Button fx:id="tresBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="3" GridPane.columnIndex="2" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="4" GridPane.vgrow="SOMETIMES" />
<!-- Fifth Row -->
<Button fx:id="alterarBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="+/-" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="5" GridPane.vgrow="SOMETIMES" />
<Button fx:id="zeroBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="0" GridPane.columnIndex="1" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="5" GridPane.vgrow="SOMETIMES" />
<Button fx:id="decimalBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62" minWidth="62" text="." GridPane.columnIndex="2" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="5" GridPane.vgrow="SOMETIMES" />
<Button fx:id="igualBotao" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="62.0" minWidth="62" text="=" GridPane.columnIndex="3" GridPane.hgrow="SOMETIMES" GridPane.rowIndex="5" GridPane.vgrow="SOMETIMES" />
<Button fx:id="apagarBotao" layoutX="258.0" layoutY="54.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="31.0" minWidth="62" text="⌫" GridPane.columnIndex="3" GridPane.hgrow="SOMETIMES" GridPane.vgrow="SOMETIMES" />
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
</GridPane>
</AnchorPane>