JFXPanel不会调整大小以适应FlowPane

时间:2016-07-13 09:29:38

标签: java swing javafx javafx-8 jfxpanel

除非另有说明,否则以下任何组件均不具有最小,最大或首选高度: 我有一个带有BorderLayout的JPanel。 在BorderLayout的中心有一个JPanel。 在PAGE_START有一个JFXPanel。 JFXPanel已经设置了一个包含BorderPane的场景,而BorderPane又包含另一个BorderPane(由FXML定义的最小,最大和首选高度)和一个FlowPane。

但是,当元素添加到FlowPane时,JFXPanel不会调整大小。 FlowPane的高度保持为其顶部和底部填充的总和 - 可能是因为这是在JFXPanel中首次设置场景时的正确高度。

因此,FlowPane的元素被切断(或在包装后完全隐藏)。 (如果我手动将JFXPanel设置得更大,我可以看到FlowPane正在调整大小,所以这不是问题。)

为什么我的JFXPanel没有调整大小? JFXPanels是不是设计用于调整场景大小,还是需要一些其他方法调用(例如invalidate())?或者问题的根源是LayoutManagers之一?

MCVE

以下代码生成如下所示的框架:

enter image description here

然而,如果我在设置JFXPanel场景之前将元素添加到FlowPane ,它看起来像这样(这是可取的):

enter image description here

显然,在实际代码中,随着我们的进展,新元素将被添加到FlowPane中或从FlowPane中删除,因此不能等到设置场景之后。希望JFXPanel像FlowPane一样动态调整大小。

public static void main(String[] args)
{
    JFXPanel topPanel = new JFXPanel();

    JPanel centerPanel = new JPanel();
    centerPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN));

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.PAGE_START);
    mainPanel.add(centerPanel, BorderLayout.CENTER);

    JFrame frame = new JFrame("Main Frame");
    frame.setContentPane(mainPanel);
    frame.setUndecorated(true);
    frame.setSize(new Dimension(300, 300));

    BorderPane root = new BorderPane();
    BorderPane borderPane = new BorderPane();
    borderPane.setMinHeight(100);
    borderPane.setMaxHeight(100);
    borderPane.setStyle("-fx-border-color: red;");

    FlowPane flowPane = new FlowPane();
    flowPane.setStyle("-fx-border-color: blue;");

    root.setCenter(borderPane);
    root.setBottom(flowPane);

    Platform.runLater(() -> topPanel.setScene(new Scene(root)));

    SwingUtilities.invokeLater(() -> frame.setVisible(true));

    // Now add some elements to the FlowPane
    Platform.runLater(() ->
    {
        for (int i = 0; i < 5; i++)
        {
            flowPane.getChildren().add(new PinkSquare());
        }
    });
}

private static class PinkSquare extends HBox
{
    private PinkSquare()
    {
        super();
        setMinHeight(50);
        setMinWidth(50);
        setStyle("-fx-background-color: pink;");
    }
}

0 个答案:

没有答案