簡體   English   中英

如何在 Swing 應用程序的對話框中提供 JavaFX 組件?

[英]How to provide JavaFX components in a dialog in a Swing application?

我在一個場景中創建了一組 JavaFX 組件,我想在 swing 應用程序的可調整大小的模式對話框中顯示這些組件。 JavaFX 組件包括用於掃描圖像的ImageView s,它可以得到相當大的依賴於縮放級別,因此在問題中進行精確布局。 我的選擇是afaik

  • Platform.runLater中顯示帶有showAndWait的 JavaFX Dialog ,並使用不可見的JDialog停止 Swing EDT。 這顯然會導致死鎖並且非常不雅。
  • 將 JavaFX 組件放入JFXPanel並在JDialog中顯示。 這在模態方面有效,但我不知道如何在JFXPanel中布局組件,因為在GroupLayout中,面板只是無限增長(JavaFX ScrollPane沒有任何效果)。

例如:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class NewMain1 extends Application {
    private final ImageView imageView;

    public NewMain1() {
        this.imageView = new ImageView(NewMain.class.getResource("/File_CC-BY-SA_3_icon_88x31.png").toString());
    }

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

    @Override
    public void start(Stage stage) throws Exception {
        BorderPane borderPane = new BorderPane();
        Button bottomButton = new Button("Some button");
        ScrollPane imageViewScrollPane = new ScrollPane(imageView);
        borderPane.setCenter(imageViewScrollPane);
        borderPane.setBottom(bottomButton);
        imageView.setSmooth(true);
        imageView.setFitHeight(400);
        StackPane  root  =  new  StackPane();
        root.getChildren().add(borderPane);
        stage.setScene(new Scene(root, 800, 600));
        stage.show();
    }
}

ImageView顯示了一個運行良好的ScrollPane ,而在JDialogJFXPanel中,滾動/布局不起作用:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class NewMain extends JFrame {
    private static final long serialVersionUID = 1L;
    private final JFXPanel mainPanel = new JFXPanel();
    private final ImageView imageView;
    private final JButton closeButton = new JButton("Close");

    public NewMain() throws HeadlessException {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(0, 0, 800, 600);
        setPreferredSize(new Dimension(800, 600));
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(layout);
        layout.setAutoCreateContainerGaps(true);
        layout.setAutoCreateGaps(true);
        this.imageView = new ImageView(NewMain.class.getResource("/File_CC-BY-SA_3_icon_88x31.png").toString());
        Platform.runLater(() -> {
            BorderPane borderPane = new BorderPane();
            Button bottomButton = new Button("Some button");
            ScrollPane imageViewScrollPane = new ScrollPane(imageView);
            borderPane.setCenter(imageViewScrollPane);
            borderPane.setBottom(bottomButton);
            imageView.setSmooth(true);
            imageView.setFitHeight(400);
            Group  root  =  new  Group();
            Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
            root.getChildren().add(borderPane);
            mainPanel.setScene(scene);
        });
        closeButton.addActionListener((event) -> {
            setVisible(false);
        });
        layout.setHorizontalGroup(layout.createParallelGroup()
                .addComponent(mainPanel)
                .addComponent(closeButton));
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addComponent(mainPanel)
                .addComponent(closeButton));
        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new NewMain().setVisible(true);
        });
    }
}

不要將場景的根設為Group , Groups 不可調整大小。

只需刪除該組並為場景根使用可調整大小的布局(您的示例代碼中已經有一個可調整大小的布局,它是 BorderPane,因此您可以使用它)。

代替:

Group  root  =  new  Group();
Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
root.getChildren().add(borderPane);

寫:

Scene  scene  =  new  Scene(borderPane, Color.ALICEBLUE);

Swing JFrame 內的 JFXPanel 內的 JavaFX 場景內的 ScrollPane。

快照

Note, that in your pure JavaFX NewMain1 application, you use are already using a resizable pane as a root (a StackPane), so that is the reason for the discrepancy that you observed between the pure JavaFX version and the Swing embedded version.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM