如何在 JavaFX GridPane 中最大化多个网格中的一个指定网格

How to maximize one specified grid of several grids in JavaFX GridPane

我使用 GridPane,并且 f.g。里面有四个格子。但有时每个网格对用户来说不够大,所以我打算通过双击最大化其中一个网格。

但是由于grid的内容是SwingNode类的,没有setWidth()、setHeight()之类的方法,所以如何实现这样的功能

您可以通过双击操作 GridPanecolumnConstraintsrowConstraintshgrowvgrow 属性。这是一个非常简单的例子:

import java.awt.Color;
import java.util.Random;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ExpandingSwingNodesInGridPane extends Application {

    private static final int NUM_ROWS = 3 ;
    private static final int NUM_COLS = 3 ;

    private static final double PREF_WIDTH = 0 ;
    private static final double MAX_WIDTH = Double.MAX_VALUE ;
    private static final double PREF_HEIGHT = 0 ;
    private static final double MAX_HEIGHT = Double.MAX_VALUE ;

    private static final Random RNG = new Random();

    // coordinates in grid pane of currently expanded node:
    private IntegerProperty expandedRowProperty = new SimpleIntegerProperty(-1);
    private IntegerProperty expandedColumnProperty = new SimpleIntegerProperty(-1);

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        for (int x = 0 ; x < NUM_COLS; x++) {
            ColumnConstraints colConstraints = new ColumnConstraints(PREF_WIDTH, PREF_WIDTH, MAX_WIDTH);
            root.getColumnConstraints().add(colConstraints);

            // change hgrow property according to expanded column:
            colConstraints.hgrowProperty().bind(
                Bindings.when(expandedColumnProperty.isEqualTo(x))
                    .then(Priority.ALWAYS)
                    .otherwise(Priority.SOMETIMES));
        }
        for (int y = 0 ; y < NUM_ROWS; y++) {
            RowConstraints rowConstraints = new RowConstraints(PREF_HEIGHT, PREF_HEIGHT, MAX_HEIGHT);
            root.getRowConstraints().add(rowConstraints);

            // change vgrow property according to expanded row:
            rowConstraints.vgrowProperty().bind(Bindings.when(expandedRowProperty.isEqualTo(y))
                    .then(Priority.ALWAYS)
                    .otherwise(Priority.SOMETIMES));
        }

        for (int x = 0 ; x < NUM_COLS; x++) {
            for (int y = 0 ; y < NUM_ROWS; y++) {
                addSwingNode(root, x, y);
            }
        }

        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void addSwingNode(GridPane root, int x, int y) {
        SwingNode swingNode = new SwingNode();

        // expand or collapse on double click:
        swingNode.setOnMouseClicked(e -> {
            if (e.getClickCount() == 2) {
                if (expandedColumnProperty.get() == x && expandedRowProperty.get() == y) {
                    // currently expanded, unexpand:
                    expandedColumnProperty.set(-1);
                    expandedRowProperty.set(-1);
                } else {
                    expandedColumnProperty.set(x);
                    expandedRowProperty.set(y);
                }
            }
        });
        root.add(swingNode, x, y);

       SwingUtilities.invokeLater(() -> {
           JComponent comp = new JPanel();
           comp.setBackground(new Color(RNG.nextInt(0xFFFFFF)));
           swingNode.setContent(comp);
        });

    }

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