我不知道为什么单元格内容可以超出gridpanes rowconstraint的限制,尽管设置了最大值。我试图以某种方式剪辑它,但我没有得到单元格的实际高度(当我动态地改变它时)。 正如您在图像上看到的那样,绿色矩形太大了。我怎么能动态地限制它的高度?
这是一个小例子:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane gp = new GridPane();
gp.setGridLinesVisible(true);
gp.setVgap(10);
gp.add(new Pane(new Rectangle(100,100,Color.rgb(255, 0, 0, 0.5))), 0, 0);
gp.add(new Pane(new Rectangle(100,200,Color.rgb(0,255,0,0.5))), 0, 1);
gp.getRowConstraints().add(new RowConstraints(50,100,110));
gp.getRowConstraints().add(new RowConstraints(50,100,110));
Scene scene = new Scene(gp,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
Meta:好的,接下来尝试,这次作为修改代码的答案。
To topic:设置RowConstraints后,您可以迭代GridPane的子节点,看看它们是否超过了首选高度。如果是,那么
这是否符合您的需求?
public class RowConstraintsExample extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane gp = new GridPane();
gp.setGridLinesVisible(true);
int minHeight = 50;
int preferredHeight = 100;
int maxHeight = 110;
gp.setVgap(10);
gp.add(new Pane(new Rectangle(100,100,Color.rgb(255, 0, 0, 0.5))), 0, 0);
gp.add(new Pane(new Rectangle(100,200,Color.rgb(0,255,0,0.5))), 0, 1);
gp.getRowConstraints().add(new RowConstraints(minHeight, preferredHeight, maxHeight));
gp.getRowConstraints().add(new RowConstraints(minHeight, preferredHeight, maxHeight));
for (Node node: gp.getChildren()) {
Integer rowIndex = GridPane.getRowIndex(node);
if (rowIndex != null) {
RowConstraints rowConstraints = gp.getRowConstraints().get(rowIndex);
if (node.getBoundsInLocal().getHeight() > rowConstraints.getPrefHeight()) {
node.setClip(new Rectangle(100, rowConstraints.getMaxHeight()));
GridPane.setVgrow(node, Priority.SOMETIMES);
}
}
}
Scene scene = new Scene(gp,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 1 :(得分:0)
您可以按如下方式调整代码并使用剪切矩形:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane gp = new GridPane();
gp.setGridLinesVisible(true);
gp.setVgap(10);
gp.getRowConstraints().add(new RowConstraints(50,100,110));
gp.getRowConstraints().add(new RowConstraints(50,100,110));
Pane pane1 = new Pane(new Rectangle(100,100,Color.rgb(255, 0, 0, 0.5)));
Pane pane2 = new Pane(new Rectangle(100,200,Color.rgb(0, 255, 0, 0.5)));
Rectangle clipRectangle1 = new Rectangle();
pane1.setClip(clipRectangle1);
pane1.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
clipRectangle1.setWidth(newValue.getWidth());
clipRectangle1.setHeight(newValue.getHeight());
});
Rectangle clipRectangle2 = new Rectangle();
pane2.setClip(clipRectangle2);
pane2.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
clipRectangle2.setWidth(newValue.getWidth());
clipRectangle2.setHeight(newValue.getHeight());
});
gp.add(pane1, 0, 0);
gp.add(pane2, 0, 1);
Scene scene = new Scene(gp,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}