1

我正在尝试使用 JavaFX 创建一个包含在窗格内的矩形网格。我希望根据窗格的大小自动调整这些矩形的大小以占用可用空间,矩形之间有一个小间隙。

我现在使用的代码是这样的,调用 createVisual 函数来生成包含窗格的组:

public class Visualizer extends NodePart {
    private VBox m_vbox;
    private AnchorPane m_pane;

    public static class ResizableRectanble extends javafx.scene.shape.Rectangle {
        public ResizableRectangle(double w, double h) {
            super(w,h);
        }
        @Override
        public boolean isResizable() {
            return true;
        }
        @Override
        public void resize(double width, double height) {
            setWidth(width);
            setHeight(height);
        }
    }

    @Override
    protected Group createVisual() {
        final Group group = new Group() {
            @Override
            public boolean isResizable() {
                return true;
            }
            @Override
            public void resize(double w, double h) {
                m_pane.setPrefSize(w,h);
            }
        };
        m_pane = new AnchorPane();
        m_pane.setStyle("-fx-background-color : lightcyan; -fx-border-color: silver; -fx-border-width: 3;");
        m_pane.setPrefSize(100, 100);
        m_pane.setMouseTransparent(false);

        m_vbox = new VBox(3.0);
        m_vbox.setFillWidth(true);
        m_vbox.setMouseTransparent(false);

        for(int i=0; i<16; i++) {
            HBox hbox = new HBox(3.0);
            hbox.setAlignment(Pos.CENTER);
            for(int j=0; j<4; j++) {
                Rectangle rect = new ResizableRectangle(50.0,50.0);
                rect.setStyle("-fx-fill: lime; -fx-border-color: red; -fx-border-width: 3;");
                hbox.setHgrow(rect, Priority.ALWAYS);
                hbox.getChildren().add(rect);   
                hbox.setFillHeight(true);
            }
        }
        m_vbox.setVGrow(hbox, Priority.ALWAYS); 
        m_pane.setTopAnchor(m_vbox, 5.0);
        m_pane.setBottomAnchor(m_vbox, 5.0);
        m_pane.getChildren().add(m_vbox);
        group.getChildren().add(m_pane);
        return group;
    }
}

这实际上不起作用,因为当组大小更改时,矩形的宽度不会从初始值更改。矩形的高度也非常小,它们之间的距离超过了 3.0 像素。

我也尝试在 m_vbox 上设置左右锚点,但它似乎不起作用,因为矩形被调整到小于窗格大小的一半。

我曾尝试在列和行上使用 GridPane 和约束,但由于存在重叠,它也不起作用。如果可能的话,我更喜欢使用 VBox 和 HBox 组合。

Eclipse GEF(图形编辑框架)调用此函数以在图形中创建节点。我在 Eclipse Neon 中使用 GEF 版本 5。

我是JavaFX的初学者,任何帮助将不胜感激!

编辑,我尝试使用 GridPane 的代码:

public class Visualizer extends NodePart {
    private GridPane m_gridPane;

    public static class ResizableRectanble extends javafx.scene.shape.Rectangle {
        public ResizableRectangle(double w, double h) {
            super(w,h);
        }
        @Override
        public boolean isResizable() {
            return true;
        }
        @Override
        public void resize(double width, double height) {
            setWidth(width);
            setHeight(height);
        }
    }

    @Override
    protected Group createVisual() {
        final Group group = new Group() {
            @Override
            public boolean isResizable() {
                return true;
            }
            @Override
            public void resize(double w, double h) {
                m_gridPane.setPrefSize(w,h);
            }
        };
        m_gridPane = new GridPane();
        m_gridPane.setStyle("-fx-background-color : lightcyan; -fx-border-color: silver; -fx-border-width: 3;");
        m_gridPane.setPrefSize(100, 100);
        m_gridPane.setMouseTransparent(false);

        for(int i=0; i<16; i++) {
            for(int j=0; j<4; j++) {
                if(i == 0) {
                    ColumnConstraints cc = new ColumnConstraints();
                    cc.setFillWidth(true);
                    cc.setHgrow(Priority.ALWAYS);
                    m_gridPane.getColumnConstraints().add(cc);
                }

                Rectangle rect = new ResizableRectangle(50.0,50.0);
                rect.setStyle("-fx-fill: lime; -fx-border-color: red; -fx-border-width: 3;");
                m_gridPane.add(rect, j, i);
            }
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            m_gridPane.getRowConstraints().add(rc);
        }
        group.getChildren().add(m_gridPane);
        return group;
    }
}

编辑 2:使用 GridPane 的代码有效,节点不重叠。但是,没有显示矩形的边界,这使我相信存在重叠。

4

0 回答 0