JavaFX - 如何使用 ColumnConstraints 的缩放宽度和静态宽度来调整 GridPane 以填充整个 HBox?

JavaFX - How to fit a GridPane to fill the entire HBox using both scaling width and static width of ColumnConstraints?

我在使用动态创建的 GridPane 填充整个自定义 HBox 时遇到问题。 HBox 存在于 ListView 中,并且已经可以毫无问题地随 ListView 动态缩放。问题是当我用鼠标扩展 window 大小时,我希望我的 GridPane 中的一些东西可以缩放,但有些部分需要保持相同的大小。

有两个按钮和一个填充标签需要保持相同的大小,这不是问题。我的模拟 class 总结了我在做什么:

控制器:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import static javafx.scene.layout.Region.USE_PREF_SIZE;


public class FXMLDocumentController implements Initializable {

private Label label;
@FXML
private ListView<ConversionJob> listJobs;
@FXML
private Button btnAction;

@FXML
private void handleButtonAction(ActionEvent event) {
    // Generates a new ConversionJob in the ListView.
listJobs.getItems().add(new ConversionJob());

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // EMPTY
}

public class ConversionJob extends HBox {

    public ConversionJob() {
        super();

        // Creates all elements
        Label lblConversionName = new Label();
        ProgressBar progress = new ProgressBar();
        Button btnPause = new Button();
        Button btnCancel = new Button();
        GridPane grid = new GridPane();

        setGridInfo(grid, lblConversionName, progress, btnPause, btnCancel);

        this.getChildren().addAll(grid);
    }

    private void setGridInfo(GridPane grid, Label lblConversionName, ProgressBar progress, Button btnPause, Button btnCancel) {
        this.setHgrow(grid, Priority.ALWAYS);

    Label filler = new Label();
    filler.setText("  ");

    grid.addColumn(0, filler);
    grid.add(lblConversionName, 1, 0);
    grid.add(progress, 2, 0);
    grid.add(btnPause, 3, 0);
    grid.add(btnCancel, 4, 0);
    grid.setGridLinesVisible(true);


    grid.setMaxWidth(this.getMaxWidth());

    ColumnConstraints col1 = new ColumnConstraints();
    col1.setMinWidth(USE_PREF_SIZE);

    ColumnConstraints col2 = new ColumnConstraints();
    col2.setPercentWidth(40);

    ColumnConstraints col3 = new ColumnConstraints();
    col3.setPercentWidth(40);

    ColumnConstraints col4 = new ColumnConstraints();
    col4.setMinWidth(USE_PREF_SIZE);
    col4.setMaxWidth(USE_PREF_SIZE);

    ColumnConstraints col5 = new ColumnConstraints();
    col5.setMinWidth(USE_PREF_SIZE);
    col5.setMaxWidth(USE_PREF_SIZE);

    grid.getColumnConstraints().addAll(col1, col2, col3, col4, col5);
    }
}
}

FXML 文档:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="500.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testieclass.FXMLDocumentController">
   <children>
      <ListView fx:id="listJobs" layoutX="54.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
      <Button fx:id="btnAction" layoutX="53.0" layoutY="160.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="460.0" />
   </children>
</AnchorPane>

我知道我应该为此使用 ColumnConstraits class,但我希望第一个标签不要大于它的最大/首选宽度,最后两个按钮也是如此.然而,lblConversionName 和 progress 对象应该扩展 GridPane,以便它填充 GridPane 所在的 HBox 的宽度。

所以基本上,我想要的是:

更改 col.setPercentageWidth(RandomNumber) 中的数字感觉完全是随机的,因为有时它会完全脱离我的 HBox 屏幕,有时它根本不会缩放百分比。有谁知道如何解决我的问题?

由于 2 个可调整大小的列应该增长到相同的大小,您可以简单地使用 hgrow 属性并将 Priority.NEVERPriority.ALWAYS 分配给不应调整大小的列和那些应该分别调整大小的:

GridPane grid = new GridPane();

ColumnConstraints neverGrow = new ColumnConstraints();
neverGrow.setHgrow(Priority.NEVER);

ColumnConstraints alwaysGrow = new ColumnConstraints();
alwaysGrow.setHgrow(Priority.ALWAYS);

grid.getColumnConstraints().addAll(
        neverGrow,
        alwaysGrow,
        alwaysGrow,
        neverGrow,
        neverGrow);

// just use the most basic resizable nodes to demonstrate this
Region region1 = new Region();
region1.setStyle("-fx-background-color: red;");
region1.setMinHeight(50);
Region region2 = new Region();
region2.setStyle("-fx-background-color: blue;");
region2.setMinHeight(50);

// fill the first row with some nodes
grid.addRow(0,
        new Label("Some Text:"),
        region1,
        region2,
        new Button("Click me"),
        new Button("me too"));