相对大小在JavaFx的GridPane中的应用

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

Relative sizing in JavaFx GridPane

问题

您好,以下是您提供的内容的翻译:

如何使用GridPane上的权重来设置行/列大小?

例如,如果我想要五行,其中三行的大小是固定的未知大小,一行占剩余空间的三分之一,另一行占剩余空间的三分之二(见下图),我该如何实现?

相对大小在JavaFx的GridPane中的应用

我尝试使用RowConstraints,其中两行设置为row.setVgrow(Priority.ALWAYS);,但这只允许剩余空间平均分配。

我尝试使用百分比,因为我在文档中看到如果widthPercent(或heightPercent)值的总和大于100,则这些值将被视为权重。例如,如果有3列,每列的widthPercent都为50,则每列将被分配网格面板可用宽度的1/3(50/(50+50+50))。

问题是,当我尝试这样做时,我的应用程序部分消失了

相对大小在JavaFx的GridPane中的应用

我推测这是由于文档中的某些部分引起的,文档中写着应用程序可以自由混合行/列的大小类型(从内容计算、固定或百分比)。百分比行/列将始终根据其百分比分配网格面板的可用空间(大小减去插图和间隙)。剩余空间将根据行/列的最小、首选和最大大小以及增长优先级分配给行/列。

JavaFx MCVE

package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setGridLinesVisible(true);

        ColumnConstraints columnOneConstraints = new ColumnConstraints();
        columnOneConstraints.setHgrow(Priority.ALWAYS);
        root.getColumnConstraints().addAll(columnOneConstraints);

        RowConstraints rowOneConstraints = new RowConstraints();
        RowConstraints rowTwoConstraints = new RowConstraints();
        rowTwoConstraints.setVgrow(Priority.ALWAYS);
        rowTwoConstraints.setPercentHeight(2000);
        RowConstraints rowThreeConstraints = new RowConstraints();
        RowConstraints rowFourConstraints = new RowConstraints();
        rowFourConstraints.setVgrow(Priority.ALWAYS);
        rowFourConstraints.setPercentHeight(1000);
        RowConstraints rowFiveConstraints = new RowConstraints();
        root.getRowConstraints().addAll(
                rowOneConstraints,
                rowTwoConstraints,
                rowThreeConstraints,
                rowFourConstraints,
                rowFiveConstraints
        );

        Background red = new Background(new BackgroundFill(Color.RED, null, null));
        Background blue = new Background(new BackgroundFill(Color.BLUE, null, null));

        Label rowOne = new Label("R1: Unknown Fixed Size");
        rowOne.backgroundProperty().set(red);
        rowOne.setMaxWidth(Double.MAX_VALUE);
        rowOne.setAlignment(Pos.CENTER);
        rowOne.setTextFill(Color.WHITE);
        rowOne.setMaxHeight(Double.MAX_VALUE);
        root.add(rowOne, 0, 0, 1, 1);

        Label rowTwo = new Label("R2: Grow 2 parts of the remaining space");
        rowTwo.backgroundProperty().set(blue);
        rowTwo.setTextFill(Color.WHITE);
        rowTwo.setAlignment(Pos.CENTER);
        rowTwo.setMaxWidth(Double.MAX_VALUE);
        rowTwo.setMaxHeight(Double.MAX_VALUE);
        root.add(rowTwo, 0, 1, 1, 1);

        Label rowThree = new Label("R3: Unknown Fixed Size");
        rowThree.backgroundProperty().set(red);
        rowThree.setTextFill(Color.WHITE);
        rowThree.setAlignment(Pos.CENTER);
        rowThree.setMaxWidth(Double.MAX_VALUE);
        rowThree.setMaxHeight(Double.MAX_VALUE);
        root.add(rowThree, 0, 2, 1, 1);

        Label rowFour = new Label("R4: Grow 1 part of the remaining space");
        rowFour.backgroundProperty().set(blue);
        rowFour.setTextFill(Color.WHITE);
        rowFour.setAlignment(Pos.CENTER);
        rowFour.setMaxWidth(Double.MAX_VALUE);
        rowFour.setMaxHeight(Double.MAX_VALUE);
        root.add(rowFour, 0, 3, 1, 1);

        Label rowFive = new Label("R5: Unknown Fixed Size");
        rowFive.backgroundProperty().set(red);
        rowFive.setTextFill(Color.WHITE);
        rowFive.setAlignment(Pos.CENTER);
        rowFive.setMaxWidth(Double.MAX_VALUE);
        rowFive.setMaxHeight(Double.MAX_VALUE);
        root.add(rowFive, 0, 4, 1, 1);

        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
        primaryStage.setMinHeight(primaryStage.getHeight());
        primaryStage.setMinWidth(primaryStage.getWidth());
    }
}

我想要的效果可以使用Swing的GridBagLayout来实现,但显然不能与GridPane一起使用。

package sample;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;

        JLabel rowOne = new JLabel("R1: Unknown Fixed Size", SwingConstants.CENTER);
        rowOne.setBackground(Color.RED);
        rowOne.setForeground(Color.WHITE);
        rowOne.setOpaque(true);
        constraints.gridy = 0;
        constraints.weighty = 0;
        panel.add(rowOne, constraints);

        JLabel rowTwo = new JLabel("R2: Grow 2 parts of remaining space", SwingConstants.CENTER);
        rowTwo.setBackground(Color.BLUE);
        rowTwo.setForeground(Color.WHITE);
        rowTwo.setOpaque(true);
        constraints.gridy = 1;
        constraints.weighty = 2;
        panel.add(rowTwo, constraints);

        JLabel rowThree = new JLabel("R3: Unknown Fixed Size", SwingConstants.CENTER);
        row

<details>
<summary>英文:</summary>

How can I set row / column sizes using weights on [`GridPane`](https://openjfx.io/javadoc/14/javafx.graphics/javafx/scene/layout/GridPane.html)?

For example, if I wanted five rows where three were unknown fixed sizes, one took a third of the remaining space, and the other took two thirds of the remaining space (see image below), how could I achieve this?

[![What I want][1]][1]

I looked at using `RowConstraints` where two had `row.setVgrow(Priority.ALWAYS);` but this only allowed the remaining space to be shared equally.

I tried using percentages, as I saw in the document `that if the sum of the widthPercent (or heightPercent) values total greater than 100, the values will be treated as weights. e.g. if 3 columns are each given a widthPercent of 50, then each will be allocated 1/3 of the gridpane&#39;s available width (50/(50+50+50)).`

The issue is, when I tried this, I lost part of my application

[![Application missing][2]][2]

I presume this is due too the part of the documentation that says `An application may freely mix the size-types of rows/columns (computed from content, fixed, or percentage). The percentage rows/columns will always be allocated space first based on their percentage of the gridpane&#39;s available space (size minus insets and gaps). The remaining space will be allocated to rows/columns given their minimum, preferred, and maximum sizes and grow priorities.`

___

JavaFx MCVE

````Java
package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setGridLinesVisible(true);

        ColumnConstraints columnOneConstraints = new ColumnConstraints();
        columnOneConstraints.setHgrow(Priority.ALWAYS);
        root.getColumnConstraints().addAll(columnOneConstraints);

        RowConstraints rowOneConstraints = new RowConstraints();
        RowConstraints rowTwoConstraints = new RowConstraints();
        rowTwoConstraints.setVgrow(Priority.ALWAYS);
        rowTwoConstraints.setPercentHeight(2000);
        RowConstraints rowThreeConstraints = new RowConstraints();
        RowConstraints rowFourConstraints = new RowConstraints();
        rowFourConstraints.setVgrow(Priority.ALWAYS);
        rowFourConstraints.setPercentHeight(1000);
        RowConstraints rowFiveConstraints = new RowConstraints();
        root.getRowConstraints().addAll(
                rowOneConstraints,
                rowTwoConstraints,
                rowThreeConstraints,
                rowFourConstraints,
                rowFiveConstraints
        );

        Background red = new Background(new BackgroundFill(Color.RED, null, null));
        Background blue = new Background(new BackgroundFill(Color.BLUE, null, null));

        Label rowOne = new Label(&quot;R1: Unknown Fixed Size&quot;);
        rowOne.backgroundProperty().set(red);
        rowOne.setMaxWidth(Double.MAX_VALUE);
        rowOne.setAlignment(Pos.CENTER);
        rowOne.setTextFill(Color.WHITE);
        rowOne.setMaxHeight(Double.MAX_VALUE);
        root.add(rowOne, 0, 0, 1, 1);

        Label rowTwo = new Label(&quot;R2: Grow 2 parts of the remaining space&quot;);
        rowTwo.backgroundProperty().set(blue);
        rowTwo.setTextFill(Color.WHITE);
        rowTwo.setAlignment(Pos.CENTER);
        rowTwo.setMaxWidth(Double.MAX_VALUE);
        rowTwo.setMaxHeight(Double.MAX_VALUE);
        root.add(rowTwo, 0, 1, 1, 1);

        Label rowThree = new Label(&quot;R3: Unknown Fixed Size&quot;);
        rowThree.backgroundProperty().set(red);
        rowThree.setTextFill(Color.WHITE);
        rowThree.setAlignment(Pos.CENTER);
        rowThree.setMaxWidth(Double.MAX_VALUE);
        rowThree.setMaxHeight(Double.MAX_VALUE);
        root.add(rowThree, 0, 2, 1, 1);

        Label rowFour = new Label(&quot;R4: Grow 1 part of the remaining space&quot;);
        rowFour.backgroundProperty().set(blue);
        rowFour.setTextFill(Color.WHITE);
        rowFour.setAlignment(Pos.CENTER);
        rowFour.setMaxWidth(Double.MAX_VALUE);
        rowFour.setMaxHeight(Double.MAX_VALUE);
        root.add(rowFour, 0, 3, 1, 1);

        Label rowFive = new Label(&quot;R5: Unknown Fixed Size&quot;);
        rowFive.backgroundProperty().set(red);
        rowFive.setTextFill(Color.WHITE);
        rowFive.setAlignment(Pos.CENTER);
        rowFive.setMaxWidth(Double.MAX_VALUE);
        rowFive.setMaxHeight(Double.MAX_VALUE);
        root.add(rowFive, 0, 4, 1, 1);

        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
        primaryStage.setMinHeight(primaryStage.getHeight());
        primaryStage.setMinWidth(primaryStage.getWidth());
    }
}

What I'm after could be produced with Swing's GridBagLayout, but obviously this can't be used with GridPane.

package sample;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;

        JLabel rowOne = new JLabel(&quot;R1: Unknown Fixed Size&quot;, SwingConstants.CENTER);
        rowOne.setBackground(Color.RED);
        rowOne.setForeground(Color.WHITE);
        rowOne.setOpaque(true);
        constraints.gridy = 0;
        constraints.weighty = 0;
        panel.add(rowOne, constraints);

        JLabel rowTwo = new JLabel(&quot;R2: Grow 2 parts of remaining space&quot;, SwingConstants.CENTER);
        rowTwo.setBackground(Color.BLUE);
        rowTwo.setForeground(Color.WHITE);
        rowTwo.setOpaque(true);
        constraints.gridy = 1;
        constraints.weighty = 2;
        panel.add(rowTwo, constraints);

        JLabel rowThree = new JLabel(&quot;R3: Unknown Fixed Size&quot;, SwingConstants.CENTER);
        rowThree.setBackground(Color.RED);
        rowThree.setForeground(Color.WHITE);
        rowThree.setOpaque(true);
        constraints.gridy = 2;
        constraints.weighty = 0;
        panel.add(rowThree, constraints);

        JLabel rowFour = new JLabel(&quot;R4: Grow 1 part of the remaining space&quot;, SwingConstants.CENTER);
        rowFour.setBackground(Color.BLUE);
        rowFour.setForeground(Color.WHITE);
        rowFour.setOpaque(true);
        constraints.gridy = 3;
        constraints.weighty = 1;
        panel.add(rowFour, constraints);

        JLabel rowFive = new JLabel(&quot;R5: Unknown Fixed Size&quot;, SwingConstants.CENTER);
        rowFive.setBackground(Color.RED);
        rowFive.setForeground(Color.WHITE);
        rowFive.setOpaque(true);
        constraints.gridy = 4;
        constraints.weighty = 0;
        panel.add(rowFive, constraints);

        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setMinimumSize(frame.getSize());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

答案1

得分: 4

据我所知,仅使用默认的JavaFX GridPane 是不可能实现您示例中所展示的效果的。在这个论坛上,专业人士的标准答案可能是:创建自己的自定义(类似GridBag的)布局。;-P

如果您想使用JavaFX提供的默认选项,您可以结合使用BorderPaneGridPane。虽然不完全与您的示例相符,但也许可以接近。您需要决定是否对您的需求足够满足。

以下是示例内容的翻译:

FXML:

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

&lt;?import javafx.scene.control.Label?&gt;
&lt;?import javafx.scene.layout.BorderPane?&gt;
&lt;?import javafx.scene.layout.ColumnConstraints?&gt;
&lt;?import javafx.scene.layout.GridPane?&gt;
&lt;?import javafx.scene.layout.HBox?&gt;
&lt;?import javafx.scene.layout.RowConstraints?&gt;

&lt;BorderPane prefHeight=&quot;600.0&quot; prefWidth=&quot;400.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
   &lt;top&gt;
      &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: lightgreen;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
         &lt;children&gt;
            &lt;Label text=&quot;&amp;quot;R1&amp;quot;&quot; /&gt;
         &lt;/children&gt;
      &lt;/HBox&gt;
   &lt;/top&gt;
   &lt;bottom&gt;
      &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: lightgreen;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
         &lt;children&gt;
            &lt;Label text=&quot;&amp;quot;R5&amp;quot;&quot; /&gt;
         &lt;/children&gt;
      &lt;/HBox&gt;
   &lt;/bottom&gt;
   &lt;center&gt;
      &lt;GridPane BorderPane.alignment=&quot;CENTER&quot;&gt;
        &lt;columnConstraints&gt;
          &lt;ColumnConstraints halignment=&quot;CENTER&quot; hgrow=&quot;SOMETIMES&quot; /&gt;
        &lt;/columnConstraints&gt;
        &lt;rowConstraints&gt;
          &lt;RowConstraints percentHeight=&quot;60.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
          &lt;RowConstraints percentHeight=&quot;40.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
        &lt;/rowConstraints&gt;
         &lt;children&gt;
            &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: tomato;&quot;&gt;
               &lt;children&gt;
                  &lt;Label text=&quot;&amp;quot;R2&amp;quot;&quot; /&gt;
               &lt;/children&gt;
            &lt;/HBox&gt;
            &lt;BorderPane GridPane.rowIndex=&quot;1&quot;&gt;
               &lt;center&gt;
                  &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: tomato;&quot;&gt;
                     &lt;children&gt;
                        &lt;Label text=&quot;&amp;quot;R4&amp;quot;&quot; /&gt;
                     &lt;/children&gt;
                  &lt;/HBox&gt;
               &lt;/center&gt;
               &lt;top&gt;
                  &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: lightgreen;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
                     &lt;children&gt;
                        &lt;Label text=&quot;&amp;quot;R3&amp;quot;&quot; /&gt;
                     &lt;/children&gt;
                  &lt;/HBox&gt;
               &lt;/top&gt;
            &lt;/BorderPane&gt;
         &lt;/children&gt;
      &lt;/GridPane&gt;
   &lt;/center&gt;
&lt;/BorderPane&gt;

相对大小在JavaFx的GridPane中的应用

英文:

As far as I know it is not possible to achieve what your example shows with just the default JavaFX GridPane. The standard answer of the professionals in this forum would be: Create your own custom (GridBag like) layout. ;-P

If you want to work with the default options given by JavaFX you could use a combination of e. g. BorderPane and GridPane. It is not exactly what your example shows, but maybe it comes close to it. You need to decide, if it is good enough for your needs.

Here the example:

When the first and the last row should have their fixed height values, you can use a BorderPane as the root. For the center you can have a GridPane with two rows which have percentage based values. The second row of it can then contain e. g. a BorderPane (fixed height for top node) as well and so on.

FXML:

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

&lt;?import javafx.scene.control.Label?&gt;
&lt;?import javafx.scene.layout.BorderPane?&gt;
&lt;?import javafx.scene.layout.ColumnConstraints?&gt;
&lt;?import javafx.scene.layout.GridPane?&gt;
&lt;?import javafx.scene.layout.HBox?&gt;
&lt;?import javafx.scene.layout.RowConstraints?&gt;

&lt;BorderPane prefHeight=&quot;600.0&quot; prefWidth=&quot;400.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot;&gt;
   &lt;top&gt;
      &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: lightgreen;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
         &lt;children&gt;
            &lt;Label text=&quot;&amp;quot;R1&amp;quot;&quot; /&gt;
         &lt;/children&gt;
      &lt;/HBox&gt;
   &lt;/top&gt;
   &lt;bottom&gt;
      &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: lightgreen;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
         &lt;children&gt;
            &lt;Label text=&quot;&amp;quot;R5&amp;quot;&quot; /&gt;
         &lt;/children&gt;
      &lt;/HBox&gt;
   &lt;/bottom&gt;
   &lt;center&gt;
      &lt;GridPane BorderPane.alignment=&quot;CENTER&quot;&gt;
        &lt;columnConstraints&gt;
          &lt;ColumnConstraints halignment=&quot;CENTER&quot; hgrow=&quot;SOMETIMES&quot; /&gt;
        &lt;/columnConstraints&gt;
        &lt;rowConstraints&gt;
          &lt;RowConstraints percentHeight=&quot;60.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
          &lt;RowConstraints percentHeight=&quot;40.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
        &lt;/rowConstraints&gt;
         &lt;children&gt;
            &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: tomato;&quot;&gt;
               &lt;children&gt;
                  &lt;Label text=&quot;&amp;quot;R2&amp;quot;&quot; /&gt;
               &lt;/children&gt;
            &lt;/HBox&gt;
            &lt;BorderPane GridPane.rowIndex=&quot;1&quot;&gt;
               &lt;center&gt;
                  &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: tomato;&quot;&gt;
                     &lt;children&gt;
                        &lt;Label text=&quot;&amp;quot;R4&amp;quot;&quot; /&gt;
                     &lt;/children&gt;
                  &lt;/HBox&gt;
               &lt;/center&gt;
               &lt;top&gt;
                  &lt;HBox alignment=&quot;CENTER&quot; style=&quot;-fx-background-color: lightgreen;&quot; BorderPane.alignment=&quot;CENTER&quot;&gt;
                     &lt;children&gt;
                        &lt;Label text=&quot;&amp;quot;R3&amp;quot;&quot; /&gt;
                     &lt;/children&gt;
                  &lt;/HBox&gt;
               &lt;/top&gt;
            &lt;/BorderPane&gt;
         &lt;/children&gt;
      &lt;/GridPane&gt;
   &lt;/center&gt;
&lt;/BorderPane&gt;

相对大小在JavaFx的GridPane中的应用

答案2

得分: 3

如果您能够使用第三方库,在我的情况下,MigLayout 是一个不错的选择。

它使得实现我想要的布局变得简单,同时也支持FXML。

package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.tbee.javafx.scene.layout.MigPane;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        MigPane root = new MigPane(
                "fill, debug, wrap, gap 0, insets 0",
                "[fill, grow]",
                "[fill][fill, grow 2][fill][fill, grow 1][fill]"
        );

        Background red = new Background(new BackgroundFill(Color.RED, null, null));
        Background blue = new Background(new BackgroundFill(Color.BLUE, null, null));

        Label rowOne = new Label("R1: Unknown Fixed Size");
        rowOne.backgroundProperty().set(red);
        rowOne.setAlignment(Pos.CENTER);
        rowOne.setTextFill(Color.WHITE);
        root.add(rowOne);

        Label rowTwo = new Label("R2: Grow 2 parts of the remaining space");
        rowTwo.backgroundProperty().set(blue);
        rowTwo.setTextFill(Color.WHITE);
        rowTwo.setAlignment(Pos.CENTER);
        root.add(rowTwo);

        Label rowThree = new Label("R3: Unknown Fixed Size");
        rowThree.backgroundProperty().set(red);
        rowThree.setTextFill(Color.WHITE);
        rowThree.setAlignment(Pos.CENTER);
        root.add(rowThree);

        Label rowFour = new Label("R4: Grow 1 part of the remaining space");
        rowFour.backgroundProperty().set(blue);
        rowFour.setTextFill(Color.WHITE);
        rowFour.setAlignment(Pos.CENTER);
        root.add(rowFour);

        Label rowFive = new Label("R5: Unknown Fixed Size");
        rowFive.backgroundProperty().set(red);
        rowFive.setTextFill(Color.WHITE);
        rowFive.setAlignment(Pos.CENTER);
        root.add(rowFive);

        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
        primaryStage.setMinHeight(primaryStage.getHeight());
        primaryStage.setMinWidth(primaryStage.getWidth());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import org.tbee.javafx.scene.layout.fxml.MigPane?>
<MigPane layout="fill, gap 10 10, debug, wrap"
cols="fill, grow"
rows="[][grow 2][][grow 1][]">
<Label text="Row One"
alignment="CENTER" />
<Label text="Row Two"
alignment="CENTER" />
<Label text="Row Three"
alignment="CENTER" />
<Label text="Row Four"
alignment="CENTER" />
<Label text="Row Five"
alignment="CENTER" />
</MigPane>
英文:

If you are able to use 3rd party libraries, in my case I was, MigLayout is a good option.

It made it simple to implement the layout I wanted and it also supports FXML.

package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.tbee.javafx.scene.layout.MigPane;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        MigPane root = new MigPane(
                &quot;fill, debug, wrap, gap 0, insets 0&quot;,
                &quot;[fill, grow]&quot;,
                &quot;[fill][fill, grow 2][fill][fill, grow 1][fill]&quot;
        );

        Background red = new Background(new BackgroundFill(Color.RED, null, null));
        Background blue = new Background(new BackgroundFill(Color.BLUE, null, null));

        Label rowOne = new Label(&quot;R1: Unknown Fixed Size&quot;);
        rowOne.backgroundProperty().set(red);
        rowOne.setAlignment(Pos.CENTER);
        rowOne.setTextFill(Color.WHITE);
        root.add(rowOne);

        Label rowTwo = new Label(&quot;R2: Grow 2 parts of the remaining space&quot;);
        rowTwo.backgroundProperty().set(blue);
        rowTwo.setTextFill(Color.WHITE);
        rowTwo.setAlignment(Pos.CENTER);
        root.add(rowTwo);

        Label rowThree = new Label(&quot;R3: Unknown Fixed Size&quot;);
        rowThree.backgroundProperty().set(red);
        rowThree.setTextFill(Color.WHITE);
        rowThree.setAlignment(Pos.CENTER);
        root.add(rowThree);

        Label rowFour = new Label(&quot;R4: Grow 1 part of the remaining space&quot;);
        rowFour.backgroundProperty().set(blue);
        rowFour.setTextFill(Color.WHITE);
        rowFour.setAlignment(Pos.CENTER);
        root.add(rowFour);

        Label rowFive = new Label(&quot;R5: Unknown Fixed Size&quot;);
        rowFive.backgroundProperty().set(red);
        rowFive.setTextFill(Color.WHITE);
        rowFive.setAlignment(Pos.CENTER);
        root.add(rowFive);

        primaryStage.setScene(new Scene(root));
        primaryStage.sizeToScene();
        primaryStage.show();
        primaryStage.setMinHeight(primaryStage.getHeight());
        primaryStage.setMinWidth(primaryStage.getWidth());
    }
}
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.scene.control.Label?&gt;
&lt;?import org.tbee.javafx.scene.layout.fxml.MigPane?&gt;
&lt;MigPane layout=&quot;fill, gap 10 10, debug, wrap&quot;
cols=&quot;fill, grow&quot;
rows=&quot;[][grow 2][][grow 1][]&quot;&gt;
&lt;Label text=&quot;Row One&quot;
alignment=&quot;CENTER&quot; /&gt;
&lt;Label text=&quot;Row Two&quot;
alignment=&quot;CENTER&quot; /&gt;
&lt;Label text=&quot;Row Three&quot;
alignment=&quot;CENTER&quot; /&gt;
&lt;Label text=&quot;Row Four&quot;
alignment=&quot;CENTER&quot; /&gt;
&lt;Label text=&quot;Row Five&quot;
alignment=&quot;CENTER&quot; /&gt;
&lt;/MigPane&gt;

huangapple
  • 本文由 发表于 2020年8月15日 22:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426973.html
匿名

发表评论

匿名网友

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

确定