【发布时间】:2018-10-16 13:53:24
【问题描述】:
我在使用动态创建的 GridPane 填充整个自定义 HBox 时遇到问题。 HBox 存在于 ListView 中,并且已经与 ListView 一起动态缩放,没有任何问题。问题是我希望 GridPane 中的某些内容在使用鼠标扩展窗口大小时进行缩放,但某些部分需要保持相同大小。
有两个按钮和一个填充标签需要保持相同的大小,这不是问题。我的模拟课总结了我在做什么:
控制器:
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 类,但我希望第一个标签不要大于它的最大/首选宽度,最后两个按钮也是如此。但是,lblConversionName 和进度对象应该扩展 GridPane,以便填充 GridPane 所在的 HBox 的宽度。
所以基本上,我想要的是:
- 第一列中的填充标签在您增加窗口大小时不会缩放,并且包含文本“”。
- 第二列中的标签随窗口大小而增加。
- 第三列中的进度条也会根据窗口大小进行缩放。
- 最后 2 列中的两个按钮根本不缩放,就像第一个填充标签一样。最后一个按钮需要固定在最右边,这样 GridPane 才能填满整个 HBox。
更改col.setPercentageWidth(RandomNumber) 中的数字感觉完全是随机的,因为有时它会完全脱离我的 HBox 屏幕,有时它根本不会缩放百分比。有谁知道如何解决我的问题?
【问题讨论】:
标签: javafx gridpane column-width hbox