使用 JFXPanel 时如何解决对齐问题?

How do I fix aligning issues when using JFXPanel?

我的问题是,我有一个普通的 JavaFX fxml 文件,当我正常启动它并调整它的大小时,它会自动重新对齐它的元素。但是当我用 JFXPanel 启动它时,突然什么都没有重新对齐了。我不知道是什么原因。 (我必须用这个方法来启动我的gui,没有别的办法)

我的代码:

final JFXPanel panel = new JFXPanel();
panel.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        super.componentResized(e);
        // Maybe here some code?
    }
});

JFrame frame = new JFrame();

Platform.runLater(() -> {
    try {
        Group root = new Group();
        Scene scene = new Scene(root);

        FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
        Node node = loader.load();

        root.getChildren().add(node);
        panel.setScene(scene);
    } catch (IOException e) {
        e.printStackTrace();
    }
});

frame.add(panel);
frame.pack();
frame.setVisible(true);

我研究了几个小时,但没有真正找到解决问题的方法。也许我搜索错了?如果有人能帮助我,我将不胜感激。

我对 Group 对象有点怀疑 "root" 但我不知道它的作用。 (我从网站上获得了这段代码,因为我对 JFXPanels 没有经验,所以我不完全确定每一行的作用)

我的整个class:

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.startGui();
    }

    public void startGui() {
        final JFXPanel panel = new JFXPanel();
        panel.setPreferredSize(new Dimension(600, 400));
        panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                super.componentResized(e);
                // Maybe here some code?
            }
        });

        JFrame frame = new JFrame();

        Platform.runLater(() -> {
            try {
                Group root = new Group();
                Scene scene = new Scene(root);

                FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
                Node node = loader.load();

                root.getChildren().add(node);
                panel.setScene(scene);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

还有我的 fxml 文件

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
         <children>
            <Label text="left" />
         </children>
      </StackPane>
      <StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
         <children>
            <Label text="right" />
         </children>
      </StackPane>
   </children>
</HBox>

当您启动 gui 并调整它的大小时,左右标签应该更远。它们到 gui 边界的距离应该相同。

您正在将 JFXPanel 添加到默认使用 BorderLayoutJFrame
要更改 JFXPanel 设置其他布局管理器的大小调整和定位。
例如:

frame.setLayout(new GridBagLayout());