gpt4 book ai didi

java - 如何在 Swing 应用程序的对话框中提供 JavaFX 组件?

转载 作者:行者123 更新时间:2023-11-30 07:00:21 24 4
gpt4 key购买 nike

我在场景中创建了一组 JavaFX 组件,我希望将其显示在 swing 应用程序中可调整大小的模式对话框中。 JavaFX 组件包括 ImageView用于扫描图像,根据缩放级别,扫描图像可能会变得相当大,因此精确布局是一个问题。我的选择是afaik

  • 显示 JavaFX DialogshowAndWaitPlatform.runLater并用不可见的 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();
}
}

显示运行良好 ScrollPane对于ImageView而在 JFXPanelJDialog滚动/布局不起作用:

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);
});
}
}

最佳答案

不要将Scene作为根一个Group , 组的大小不可调整。

只需删除该组并为场景根使用可调整大小的布局(示例代码中已经有一个可调整大小的布局,它是 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。

snapshot

请注意,在您的纯 JavaFX NewMain1 应用程序中,您已经使用可调整大小的 Pane 作为根(StackPane),因此这就是您在纯 JavaFX 版本和 Swing 嵌入式版本之间观察到的差异的原因.

关于java - 如何在 Swing 应用程序的对话框中提供 JavaFX 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41034366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com