我在使用动态创建的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和progress对象应该扩展GridPane,以便它填充GridPane所在的HBox的宽度。
所以基本上,我想要的是:
更改col.setPercentageWidth(RandomNumber)
中的数字感觉完全是随机的,因为有时候我的HBox会完全脱离屏幕,有时它根本不会缩放百分比。有谁知道如何解决我的问题?
答案 0 :(得分:0)
由于2个可调整大小的列应该增长到相同的大小,因此您只需使用hgrow
属性,并将Priority.NEVER
和Priority.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"));