使用JavaFX构建动态用户界面

huangapple go评论101阅读模式
英文:

Building dynamic UI using JavaFX

问题

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity"
    minHeight="-Infinity" minWidth="-Infinity" prefHeight="437.0"
    prefWidth="736.0" xmlns="http://javafx.com/javafx/11.0.1"
    xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <StackPane layoutX="14.0" layoutY="27.0" prefHeight="405.0"
            prefWidth="223.0" style="-fx-background-color: white;"
            AnchorPane.bottomAnchor="12.0" AnchorPane.leftAnchor="15.0"
            AnchorPane.topAnchor="20.0">
            <effect>
                <DropShadow />
            </effect>
        </StackPane>
        <StackPane layoutX="251.0" layoutY="21.0" prefHeight="119.0"
            prefWidth="470.0" style="-fx-background-color: #ffffff;"
            AnchorPane.leftAnchor="251.0" AnchorPane.rightAnchor="15.0"
            AnchorPane.topAnchor="21.0">
            <effect>
                <DropShadow />
            </effect>
        </StackPane>
        <StackPane layoutX="251.0" layoutY="150.0"
            prefHeight="269.0" prefWidth="470.0"
            style="-fx-background-color: #ffffff;" AnchorPane.bottomAnchor="13.0"
            AnchorPane.leftAnchor="251.0" AnchorPane.rightAnchor="15.0">
            <effect>
                <DropShadow />
            </effect>
        </StackPane>
    </children>
</AnchorPane>

使用JavaFX构建动态用户界面

英文:

I'm new to JavaFx and trying to build the following screen with three StackPane that can dynamicall grow and shrink the width and height depending on the window size. I tried different ways but was not able to achieve this. I also tried using AnchorPane constraints. here is the image of what i'm trying to achieve and corresponding FXML. here is are the constraints I'm looking between these panes

  • Left Pane to have a max width (300px) and min 150
  • distance between left and right(2) stack panes should remain constant
  • distance between right top and bottom stack panes should remain constant
  • left top stack pane to have max height of 250px

<!-- language: lang-xml -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.effect.DropShadow?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;?import javafx.scene.layout.StackPane?&gt;

&lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot;
	minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;437.0&quot;
	prefWidth=&quot;736.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot;
	xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
	&lt;children&gt;
		&lt;StackPane layoutX=&quot;14.0&quot; layoutY=&quot;27.0&quot; prefHeight=&quot;405.0&quot;
			prefWidth=&quot;223.0&quot; style=&quot;-fx-background-color: white;&quot;
			AnchorPane.bottomAnchor=&quot;12.0&quot; AnchorPane.leftAnchor=&quot;15.0&quot;
			AnchorPane.topAnchor=&quot;20.0&quot;&gt;
			&lt;effect&gt;
				&lt;DropShadow /&gt;
			&lt;/effect&gt;
		&lt;/StackPane&gt;
		&lt;StackPane layoutX=&quot;251.0&quot; layoutY=&quot;21.0&quot; prefHeight=&quot;119.0&quot;
			prefWidth=&quot;470.0&quot; style=&quot;-fx-background-color: #ffffff;&quot;
			AnchorPane.leftAnchor=&quot;251.0&quot; AnchorPane.rightAnchor=&quot;15.0&quot;
			AnchorPane.topAnchor=&quot;21.0&quot;&gt;
			&lt;effect&gt;
				&lt;DropShadow /&gt;
			&lt;/effect&gt;
		&lt;/StackPane&gt;
		&lt;StackPane layoutX=&quot;251.0&quot; layoutY=&quot;150.0&quot;
			prefHeight=&quot;269.0&quot; prefWidth=&quot;470.0&quot;
			style=&quot;-fx-background-color: #ffffff;&quot; AnchorPane.bottomAnchor=&quot;13.0&quot;
			AnchorPane.leftAnchor=&quot;251.0&quot; AnchorPane.rightAnchor=&quot;15.0&quot;&gt;
			&lt;effect&gt;
				&lt;DropShadow /&gt;
			&lt;/effect&gt;
		&lt;/StackPane&gt;
	&lt;/children&gt;
&lt;/AnchorPane&gt;

使用JavaFX构建动态用户界面

答案1

得分: 2

你只需要设置一些最大宽度和最大高度。

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <StackPane layoutX="14.0" layoutY="27.0" maxWidth="400.0" prefHeight="405.0" prefWidth="223.0" style="-fx-background-color: white;" AnchorPane.bottomAnchor="12.0" AnchorPane.leftAnchor="15.0" AnchorPane.topAnchor="20.0" HBox.hgrow="ALWAYS">
            <effect>
                <DropShadow />
            </effect>
            <HBox.margin>
                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
            </HBox.margin>
        </StackPane>
        <VBox maxHeight="1.7976931348623157E308" HBox.hgrow="ALWAYS">
            <children>
                <StackPane layoutX="251.0" layoutY="21.0" maxHeight="1.7976931348623157E308" prefHeight="119.0" prefWidth="470.0" style="-fx-background-color: #ffffff;" AnchorPane.leftAnchor="251.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="21.0" VBox.vgrow="ALWAYS">
                    <effect>
                        <DropShadow />
                    </effect>
                    <VBox.margin>
                        <Insets bottom="5.0" left="10.0" right="10.0" top="10.0" />
                    </VBox.margin>
                </StackPane>
                <StackPane layoutX="251.0" layoutY="150.0" maxHeight="1.7976931348623157E308" prefHeight="269.0" prefWidth="470.0" style="-fx-background-color: #ffffff;" AnchorPane.bottomAnchor="13.0" AnchorPane.leftAnchor="251.0" AnchorPane.rightAnchor="15.0" VBox.vgrow="ALWAYS">
                    <effect>
                        <DropShadow />
                    </effect>
                    <VBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                    </VBox.margin>
                </StackPane>
            </children>
        </VBox>
    </children>
</HBox>

你可能需要调整左侧面板的最大宽度。

英文:

You just needed to set some max-widths and max-heights.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.geometry.Insets?&gt;
&lt;?import javafx.scene.effect.DropShadow?&gt;
&lt;?import javafx.scene.layout.HBox?&gt;
&lt;?import javafx.scene.layout.StackPane?&gt;
&lt;?import javafx.scene.layout.VBox?&gt;
&lt;HBox maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
&lt;children&gt;
&lt;StackPane layoutX=&quot;14.0&quot; layoutY=&quot;27.0&quot; maxWidth=&quot;400.0&quot; prefHeight=&quot;405.0&quot; prefWidth=&quot;223.0&quot; style=&quot;-fx-background-color: white;&quot; AnchorPane.bottomAnchor=&quot;12.0&quot; AnchorPane.leftAnchor=&quot;15.0&quot; AnchorPane.topAnchor=&quot;20.0&quot; HBox.hgrow=&quot;ALWAYS&quot;&gt;
&lt;effect&gt;
&lt;DropShadow /&gt;
&lt;/effect&gt;
&lt;HBox.margin&gt;
&lt;Insets bottom=&quot;10.0&quot; left=&quot;10.0&quot; right=&quot;10.0&quot; top=&quot;10.0&quot; /&gt;
&lt;/HBox.margin&gt;
&lt;/StackPane&gt;
&lt;VBox maxHeight=&quot;1.7976931348623157E308&quot; HBox.hgrow=&quot;ALWAYS&quot;&gt;
&lt;children&gt;
&lt;StackPane layoutX=&quot;251.0&quot; layoutY=&quot;21.0&quot; maxHeight=&quot;1.7976931348623157E308&quot; prefHeight=&quot;119.0&quot; prefWidth=&quot;470.0&quot; style=&quot;-fx-background-color: #ffffff;&quot; AnchorPane.leftAnchor=&quot;251.0&quot; AnchorPane.rightAnchor=&quot;15.0&quot; AnchorPane.topAnchor=&quot;21.0&quot; VBox.vgrow=&quot;ALWAYS&quot;&gt;
&lt;effect&gt;
&lt;DropShadow /&gt;
&lt;/effect&gt;
&lt;VBox.margin&gt;
&lt;Insets bottom=&quot;5.0&quot; left=&quot;10.0&quot; right=&quot;10.0&quot; top=&quot;10.0&quot; /&gt;
&lt;/VBox.margin&gt;
&lt;/StackPane&gt;
&lt;StackPane layoutX=&quot;251.0&quot; layoutY=&quot;150.0&quot; maxHeight=&quot;1.7976931348623157E308&quot; prefHeight=&quot;269.0&quot; prefWidth=&quot;470.0&quot; style=&quot;-fx-background-color: #ffffff;&quot; AnchorPane.bottomAnchor=&quot;13.0&quot; AnchorPane.leftAnchor=&quot;251.0&quot; AnchorPane.rightAnchor=&quot;15.0&quot; VBox.vgrow=&quot;ALWAYS&quot;&gt;
&lt;effect&gt;
&lt;DropShadow /&gt;
&lt;/effect&gt;
&lt;VBox.margin&gt;
&lt;Insets bottom=&quot;10.0&quot; left=&quot;10.0&quot; right=&quot;10.0&quot; top=&quot;10.0&quot; /&gt;
&lt;/VBox.margin&gt;
&lt;/StackPane&gt;
&lt;/children&gt;
&lt;/VBox&gt;
&lt;/children&gt;
&lt;/HBox&gt;

使用JavaFX构建动态用户界面

使用JavaFX构建动态用户界面

You might need to play with the max-width of the left-panel

答案2

得分: 1

感谢大家的帮助。我找到了一种使用GridPane(根窗格)、VBox、Anchor和Stack Panes的方法。以下是相应的FXML代码:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<ScrollPane fitToHeight="true" fitToWidth="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <content>
        <GridPane hgap="10.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="700.0" minWidth="700.0" style="-fx-border-width: 20px; -fx-border-color: white;">
            <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 maxHeight="347.79999389648435" minHeight="10.0" prefHeight="139.0000061035156" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="309.79999389648435" minHeight="10.0" prefHeight="180.00003662109373" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="204.79996337890628" minHeight="10.0" prefHeight="204.79996337890628" vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <VBox prefHeight="479.0" prefWidth="274.0" spacing="10.0" style="-fx-background-color: blue; -fx-border-insets: 10;" GridPane.rowSpan="2147483647">
                    <effect>
                        <DropShadow />
                    </effect>
                </VBox>
                <VBox prefHeight="200.0" prefWidth="100.0" spacing="10.0" GridPane.columnIndex="1" GridPane.columnSpan="2147483647" GridPane.rowSpan="2147483647">
                    <children>
                        <AnchorPane prefHeight="178.0" prefWidth="547.0" style="-fx-background-color: green;">
                            <effect>
                                <DropShadow />
                            </effect>
                        </AnchorPane>
                        <StackPane maxHeight="1.7976931348623157E308" prefHeight="403.0" prefWidth="547.0" style="-fx-background-color: yellow;" VBox.vgrow="ALWAYS">
                            <effect>
                                <DropShadow />
                            </effect>
                        </StackPane>
                    </children>
                </VBox>
            </children>
        </GridPane>
    </content>
</ScrollPane>

添加了这个到我的BorderPane后,它的样子如下所示:使用JavaFX构建动态用户界面

英文:

Thanks everyone for the help. I found a way to do it using the GridPane(root pane), VBox, Anchor and stack panes . . Here is the fxml for that

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.scene.control.ScrollPane?&gt;
&lt;?import javafx.scene.effect.DropShadow?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;?import javafx.scene.layout.ColumnConstraints?&gt;
&lt;?import javafx.scene.layout.GridPane?&gt;
&lt;?import javafx.scene.layout.RowConstraints?&gt;
&lt;?import javafx.scene.layout.StackPane?&gt;
&lt;?import javafx.scene.layout.VBox?&gt;
&lt;ScrollPane fitToHeight=&quot;true&quot; fitToWidth=&quot;true&quot; maxHeight=&quot;1.7976931348623157E308&quot; maxWidth=&quot;1.7976931348623157E308&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
&lt;content&gt;
&lt;GridPane hgap=&quot;10.0&quot; maxHeight=&quot;1.7976931348623157E308&quot; maxWidth=&quot;1.7976931348623157E308&quot; minHeight=&quot;700.0&quot; minWidth=&quot;700.0&quot; style=&quot;-fx-border-width: 20px; -fx-border-color: white;&quot;&gt;
&lt;columnConstraints&gt;
&lt;ColumnConstraints hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; prefWidth=&quot;100.0&quot; /&gt;
&lt;ColumnConstraints hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; prefWidth=&quot;100.0&quot; /&gt;
&lt;ColumnConstraints hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; prefWidth=&quot;100.0&quot; /&gt;
&lt;/columnConstraints&gt;
&lt;rowConstraints&gt;
&lt;RowConstraints maxHeight=&quot;347.79999389648435&quot; minHeight=&quot;10.0&quot; prefHeight=&quot;139.0000061035156&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
&lt;RowConstraints maxHeight=&quot;309.79999389648435&quot; minHeight=&quot;10.0&quot; prefHeight=&quot;180.00003662109373&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
&lt;RowConstraints maxHeight=&quot;204.79996337890628&quot; minHeight=&quot;10.0&quot; prefHeight=&quot;204.79996337890628&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
&lt;/rowConstraints&gt;
&lt;children&gt;
&lt;VBox prefHeight=&quot;479.0&quot; prefWidth=&quot;274.0&quot; spacing=&quot;10.0&quot; style=&quot;-fx-background-color: blue; -fx-border-insets: 10;&quot; GridPane.rowSpan=&quot;2147483647&quot;&gt;
&lt;effect&gt;
&lt;DropShadow /&gt;
&lt;/effect&gt;
&lt;/VBox&gt;
&lt;VBox prefHeight=&quot;200.0&quot; prefWidth=&quot;100.0&quot; spacing=&quot;10.0&quot; GridPane.columnIndex=&quot;1&quot; GridPane.columnSpan=&quot;2147483647&quot; GridPane.rowSpan=&quot;2147483647&quot;&gt;
&lt;children&gt;
&lt;AnchorPane prefHeight=&quot;178.0&quot; prefWidth=&quot;547.0&quot; style=&quot;-fx-background-color: green;&quot;&gt;
&lt;effect&gt;
&lt;DropShadow /&gt;
&lt;/effect&gt;
&lt;/AnchorPane&gt;
&lt;StackPane maxHeight=&quot;1.7976931348623157E308&quot; prefHeight=&quot;403.0&quot; prefWidth=&quot;547.0&quot; style=&quot;-fx-background-color: yellow;&quot; VBox.vgrow=&quot;ALWAYS&quot;&gt;
&lt;effect&gt;
&lt;DropShadow /&gt;
&lt;/effect&gt;
&lt;/StackPane&gt;
&lt;/children&gt;
&lt;/VBox&gt;
&lt;/children&gt;
&lt;/GridPane&gt;
&lt;/content&gt;
&lt;/ScrollPane&gt;

One I added this this my borderpane here is how it looked

使用JavaFX构建动态用户界面

huangapple
  • 本文由 发表于 2020年5月4日 11:07:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61584548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定