如何使用以下方法修复HBox内TOP_LEFT上的Menubutton

时间:2019-06-06 06:20:01

标签: java javafx netbeans fxml scenebuilder

我正在使用场景构建器。我有一个HBox和一个MenuButton作为其中的子项,

当我调整Scene.fxml的大小时,MenuButton的位置会改变。我希望“菜单”按钮始终固定在Hbox的左上角。我该如何实现?

我尝试将按钮的边距和对齐方式设置到左上角。它没用。

这是我的Scene.fxml文件。

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="563.0" prefWidth="342.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="200.0" prefWidth="342.0" AnchorPane.bottomAnchor="363.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
            <HBox maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="46.0" prefWidth="346.0">
               <children>
                  <MenuButton alignment="TOP_LEFT" mnemonicParsing="false" prefHeight="46.0" prefWidth="56.0" HBox.hgrow="ALWAYS">
                    <items>
                      <MenuItem mnemonicParsing="false" text="Standard" />
                      <MenuItem mnemonicParsing="false" text="Scientifics" />
                    </items>
                  </MenuButton>
               </children>
            </HBox>
            <HBox prefHeight="100.0" prefWidth="200.0">
               <children>
                  <TextField editable="false" prefHeight="146.0" prefWidth="342.0" HBox.hgrow="ALWAYS" />
               </children>
            </HBox>
        </items>
      </SplitPane>
      <GridPane layoutX="-2.0" layoutY="227.0" prefHeight="363.0" prefWidth="342.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="199.8">
        <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>
        <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="70.0" text="-" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="1">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="70.0" prefWidth="71.0" text="+" textAlignment="CENTER" GridPane.columnIndex="4">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="69.0" text="X" textAlignment="CENTER" GridPane.columnIndex="3">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="74.0" text="AC" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="1">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="70.0" text="4" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="1">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="75.0" prefWidth="68.0" text="9" textAlignment="CENTER" GridPane.columnIndex="2">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="70.0" text="5" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="1">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="71.0" text="6" textAlignment="CENTER" GridPane.rowIndex="1">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="75.0" prefWidth="68.0" text="8" textAlignment="CENTER" GridPane.columnIndex="1">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="73.0" text="\%" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="3">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="68.0" text="." textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="3">
               <font>
                  <Font size="32.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="70.0" text="0" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="3">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="67.0" text="sqrt" textAlignment="CENTER" GridPane.rowIndex="3">
               <font>
                  <Font size="20.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="74.0" text="/" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="2">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="70.0" text="=" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="2">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="72.0" text="1" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="2">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="69.0" text="2" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="2">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="68.0" text="3" textAlignment="CENTER" GridPane.rowIndex="2">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="72.0" text="*" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="3">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="70.0" text="Sin" textAlignment="CENTER" GridPane.rowIndex="4">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="73.0" text="x^2" textAlignment="CENTER" GridPane.columnIndex="4" GridPane.rowIndex="4">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="74.0" text="1/x" textAlignment="CENTER" GridPane.columnIndex="3" GridPane.rowIndex="4">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="74.0" prefWidth="68.0" text="Tan" textAlignment="CENTER" GridPane.columnIndex="2" GridPane.rowIndex="4">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="70.0" prefWidth="68.0" text="Cos" textAlignment="CENTER" GridPane.columnIndex="1" GridPane.rowIndex="4">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
            <Button alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="75.0" prefWidth="68.0" text="7" textAlignment="CENTER">
               <font>
                  <Font size="22.0" />
               </font>
            </Button>
         </children>
      </GridPane>
   </children>
</AnchorPane>][1]

https://i.stack.imgur.com/WSKLO.png

https://imgur.com/SsDoTSv

2 个答案:

答案 0 :(得分:1)

问题不是<stdlib.h>的属性/布局约束,而是MenuButton的宽度不能超过首选宽度(HBox)的事实。

346.0的值更改为maxWidth(= USE_COMPUTED_SIZE)。或者,只需将属性删除为恰好为-1的默认值即可。

这允许USE_COMPUTED_SIZE增大到HBox的大小,从而防止较小的SplitPane水平居中。

HBox

答案 1 :(得分:0)

我已经阅读了您的问题,希望您查找MenuButton的对齐属性。 或者,您可以使用边框窗格以四种方式对齐组件。

1)TOP, 2)底部 还剩3个, 4)正确