GridPane与ScrollPane内部的间隙呈现错误

问题描述 投票:0回答:2

我希望有一个带方形单元格的矩形网格,可以缩放和拖动(就像你的地图一样)。所以我把它放在ScrollPane中,尽管它在某些尺寸上表现得相当奇怪。网格的大小将动态变化。

在图片上有一个大小为20x20的网格。 Grid of size 20x20

  1. 并非所有间隙都显示在网格内。请注意,它应该是20个方格,但不是。 Vgap和Hgap都是1。
  2. 网格本身比内容大,这是不好的,因为当它将被缩放时,我可以拖动超出网格的边界。
  3. 我想将GridPane放在ScrollPane中,当它小于滚动窗格时。 (Gridpane alignment="CENTER"显然没有做任何事情)

最小,完整和可验证的例子

FXML:

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.GridPane?>

<ScrollPane hbarPolicy="NEVER" hvalue="0.5" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" pannable="true" prefHeight="400.0" prefWidth="600.0" vbarPolicy="NEVER" vvalue="0.5" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bug.app.Bug">
    <GridPane fx:id="gridPane" alignment="CENTER" hgap="1.0" style="-fx-background-color: red;" vgap="1.0">
    </GridPane>
</ScrollPane>

控制器+主要:

package bug.app;

import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Bug extends Application {
    private static final int HEIGHT = 20;
    private static final int WIDTH = 20;
    private static final double MIN_CELL_SIZE = 2;
    @FXML private GridPane gridPane;
    private DoubleProperty zoomRatio = new SimpleDoubleProperty(5);

    @FXML
    private void initialize() {
        final DoubleBinding cellSizeBinding = zoomRatio.multiply(MIN_CELL_SIZE);

        List<Node> cells = new ArrayList<>(HEIGHT * WIDTH);
        for (int row = 0; row < HEIGHT; row++) {
            for (int col = 0; col < WIDTH; col++) {
                final Pane cell = new Pane();
                cell.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));

                cell.setMinSize(MIN_CELL_SIZE, MIN_CELL_SIZE);
                cell.setMaxWidth(Region.USE_PREF_SIZE);
                cell.setMaxHeight(Region.USE_PREF_SIZE);
                cell.prefWidthProperty().bind(cellSizeBinding);
                cell.prefHeightProperty().bind(cellSizeBinding);

                GridPane.setConstraints(cell, col, row, 1, 1);

                cells.add(cell);
            }
        }
        gridPane.getChildren().clear();
        gridPane.getChildren().addAll(cells);
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(
                getClass().getResource("/view/bugView.fxml"));
        final Scene scene = new Scene(root, 600, 400);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

注意:我选择了一个Pane作为Cell,因为我想将它子类化并最终填充它(可能是Shapes?),用不同的颜色将正方形分成1-4个部分。想象一下,两个或四个三角形形成一个正方形。我也想优雅地调整大小(AFAIK Shapes不可调整大小),也许以某种方式将内部形状的大小绑定到Panes的大小将会起到魔力。我还没有那么远,但也可以随意提出建议。

java javafx scrollpane gridpane
2个回答
0
投票

将网格窗格包裹在堆叠窗格内,这将使网格窗格居中


0
投票

问题1和2

我找到了问题1和2的解决方法,省略了Vgap和Hgap,因为它显然是错误的。

我没有使用GridPane的gap属性,而是在每个单元格上设置边距:

GridPane.setMargin(cell, new Insets(0.5));

然而,由于GridPane上的默认snapToPixel="true",因此将边距舍入为1,从而导致2px的间隙。这是不方便的,因为我希望方块小到2x2px,所以2px的间隙太大了。我可能会在GridPane上将snapToPixel设置为false。需要更多测试。

问题3

我尝试按照@Silas Gyeme的建议将GridPane包装在StackPane中。

简单地将它包装在StackPane中是不够的。我不得不在ScrollPane上设置fitToHeight="true"fitToWidth="true"

还必须将单元格的minSize绑定到cellSizeBinding,因为当我缩小窗口时,它正在调整方块的大小。

cell.minWidthProperty().bind(cellSizeBinding);
cell.minHeightProperty().bind(cellSizeBinding);

编辑:我试图省略StackPane,我实际上并不需要它!只设置fitToHeightfitToWidth就足够了,GridPane将会很好地延伸。

© www.soinside.com 2019 - 2024. All rights reserved.