JavaFX 收缩按钮可调整窗口大小

标签 java javafx

我有一个带有九个方形按钮的 GridPane,我希望它可以调整大小。事实上,当我使窗口变大时,它确实会变大,但是当我使窗口变小时,按钮不会改变大小,因此窗口看起来像这样:

enter image description here

这是我的 .fxml 文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextArea?>
<HBox prefHeight="750.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
    <children>
        <GridPane fx:id="boardGridPane" layoutX="50" layoutY="25" maxHeight="Infinity" maxWidth="Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700" prefWidth="700" HBox.hgrow="ALWAYS">
            <columnConstraints>
                <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" percentWidth="33.3" prefWidth="100.0" />
                <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" percentHeight="33.3" prefHeight="100.0" vgrow="ALWAYS" />
                <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="ALWAYS" />
                <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="ALWAYS" />
            </rowConstraints>
            <children>
                <Button fx:id="button00" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  />
                <Button fx:id="button01" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="1" />
                <Button fx:id="button02" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="2" GridPane.rowIndex="0" />
                <Button fx:id="button10" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.rowIndex="1" />
                <Button fx:id="button11" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="1" GridPane.rowIndex="1" />
                <Button fx:id="button12" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="2" GridPane.rowIndex="1" />
                <Button fx:id="button20" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="0" GridPane.rowIndex="2" />
                <Button fx:id="button21" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="1" GridPane.rowIndex="2" />
                <Button fx:id="button22" maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false"  GridPane.columnIndex="2" GridPane.rowIndex="2" />
            </children>
        </GridPane>
        <VBox fx:id="menuVBox" layoutX="786.0" layoutY="336.0" prefHeight="77.0" prefWidth="178.0">
            <children>
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="Hot Seat Mode" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="Easy Bot Mode" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="Hard Bot Mode" />
                <Button maxHeight="Infinity" maxWidth="Infinity" mnemonicParsing="false" text="Statistics" />
                <TextArea fx:id="menuTextArea"/>
            </children>
        </VBox>
    </children>
</HBox>

最佳答案

如果您正在编辑FXML你自己删除 minHeight="-Infinity" minWidth="-Infinity"来自<GridPane>标签。

SceneBuilder您正在使用USE_PRE_SIZE关于GridPane :

enter image description here

将值更改为 USE_COMPUTER_SIZE :

enter image description here

关于JavaFX 收缩按钮可调整窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027769/

相关文章:

java - 带套接字的 DataInputStream...缺少字节?

java - 如何使用restTemplate获取真实的http代码和响应正文?

java - 在 Avro 中使用数组序列化对象

java - 在 JavaFX 中创建第一人称相机

java - 无法理解 Spring Boot 如何使用 maven 管理集成测试。它是否使用故障安全插件?

java - 使用 REST 和 JSON Web token 上传/下载文件

java - 如何为JavaFX异常对话框的按钮设置操作事件

JavaFX 进度条不更新

licensing - JavaFX 2.0 可以重新分发吗?

Java:如何从主类调用GUI类(没有Swing/JFrame)