【问题标题】:Javafx Gridpanes content exceeds sizeJavafx Gridpanes 内容超出大小
【发布时间】:2016-01-20 19:58:52
【问题描述】:

我不知道为什么单元格内容会超过网格窗格行约束的限制,尽管设置了最大值。我试图以某种方式剪辑它,但我没有得到单元格的实际高度(当我动态更改它时)。 正如您在图像上看到的,绿色矩形太大了。如何动态限制它的高度?

这是一个小例子:

    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);
        }
    }

【问题讨论】:

  • 我编辑了你的代码。您可以使用剪辑使内容适合单元格的大小。但是,如果您考虑到这一点,内容会被剪掉,而不是缩小。
  • 我没有看到任何编辑?如果您使用了固定大小的剪切矩形,那么这不是我想要的。我希望根据给定的网格裁剪内容。
  • 是的,我的编辑评论告诉我 1. 编辑不是正确的方式 2. 如果单元格超过 100,您正在寻找调整单元格大小的可能性,但单元格不应该超过 110。对吗?

标签: layout javafx gridpane


【解决方案1】:

Meta:好的,下一次尝试,这次作为修改代码的答案。

主题:设置 RowConstraints 后,您可以遍历 GridPane 的子节点并查看其中一个是否超过首选高度。如果是的话,那么

  1. 剪辑节点
  2. 在 GridPane 中为该节点设置 Vgrow

这符合您的需要吗?

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);
    }
}

【讨论】:

  • 不,因为一旦剪辑被设置,它就被设置了。我想让剪辑跟随网格单元的大小。
  • 假设以下情况:3 个行约束,每个 (150,300,400)。现在我将第一个 rowconstraint.minheight 设置为 200。其他两个网格单元会变小。我希望相应地裁剪网格单元中的内容。
【解决方案2】:

您可以如下调整代码并使用剪切矩形:

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-25
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2012-11-23
    相关资源
    最近更新 更多