JavaFX修复窗格布局

时间:2017-11-15 17:14:29

标签: java javafx

我希望通过向窗格添加gridPanes来使用JavaFX建模一个简单的微波。  Microwave

上面是结果应该是什么样子但是我无法正确布局gridPanes。

任何有关如何改善这一点的帮助将不胜感激。 以下是我的结果以及伴随它的代码。

my output

Map

1 个答案:

答案 0 :(得分:0)

以下是SceneBuilder中创建的示例。您可以使用FXML找出如何在代码中执行此操作。

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<HBox alignment="CENTER" prefHeight="300.0" prefWidth="500.0" style="-fx-border-color: black; -fx-border-width: 3px;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label alignment="CENTER" text="Place Food Here">
         <HBox.margin>
            <Insets left="10.0" right="10.0" />
         </HBox.margin>
      </Label>
      <VBox style="-fx-border-color: black; -fx-border-width: 3px;" HBox.hgrow="ALWAYS">
         <children>
            <TextField prefHeight="50.0" promptText="Time to be displayed here">
               <VBox.margin>
                  <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
               </VBox.margin>
            </TextField>
            <Pane prefHeight="10.0" style="-fx-background-color: black;" />
            <GridPane VBox.vgrow="ALWAYS">
               <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>
               <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>
               <children>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="1">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="2" GridPane.columnIndex="1">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="3" GridPane.columnIndex="2">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="4" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="7" GridPane.rowIndex="2">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="8" GridPane.columnIndex="1" GridPane.rowIndex="2">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="9" GridPane.columnIndex="2" GridPane.rowIndex="2">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="0" GridPane.rowIndex="3">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Start" GridPane.columnIndex="1" GridPane.rowIndex="3">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
                  <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Stop" GridPane.columnIndex="2" GridPane.rowIndex="3">
                     <GridPane.margin>
                        <Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
                     </GridPane.margin>
                  </Button>
               </children>
            </GridPane>
         </children>
      </VBox>
   </children>
</HBox>

enter image description here