【发布时间】:2018-11-01 21:54:26
【问题描述】:
我想要一个带有方形单元格的矩形网格,它可以缩放和拖动(就像地图一样)。所以我把它放在一个 ScrollPane 中,尽管它在某些尺寸下表现得相当奇怪。网格的大小会动态变化。
- 并非所有间隙都显示在网格内。请注意,它应该是 20 个正方形 但不是。 Vgap 和 Hgap 均为 1。
- 网格本身比内容大,这不好,因为当它被缩放时,我可能会拖到网格的边界之外。
- 当 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();
}
}
注意:我选择了一个窗格作为单元格,因为我想对它进行子类化并最终用不同的颜色填充它(可能是形状?),将正方形分成 1-4 个部分。想象两个或四个三角形组成一个正方形。我还想优雅地调整它的大小(AFAIK 形状不可调整大小),也许以某种方式将内部形状的大小绑定到窗格的大小会产生魔力。我还没有那么远,但也可以就此提出建议。
【问题讨论】:
标签: java javafx scrollpane gridpane