我在场景中创建了一组JavaFX组件,我希望在swing应用程序中以可调整大小的模式对话框显示。 JavaFX组件包括用于扫描图像的ImageView
s,它们可以根据缩放级别变得非常大,因此在问题中进行精确布局。我的选择是afaik
Dialog
中显示带有showAndWait
的JavaFX Platform.runLater
,并使用不可见的JDialog
停止Swing EDT。这显然会造成死锁并且非常不优雅。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
,而JFXPanel
JDialog
滚动/布局无法正常工作:
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);
});
}
}
该示例位于https://github.com/krichter722/javafx-in-jdialog-in-swing。
答案 0 :(得分:1)
只需删除组并为场景根使用可调整大小的布局(您的示例代码中已经有可调整大小的布局,它是BorderPane,因此您可以使用它。)
而不是:
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
root.getChildren().add(borderPane);
写:
Scene scene = new Scene(borderPane, Color.ALICEBLUE);
ScrollPane在Swing JFrame内的JFXPanel内的JavaFX场景内。
请注意,在纯JavaFX NewMain1应用程序中,您使用的是已经使用可调整大小的窗格作为根(StackPane),因此这是您在纯JavaFX版本和Swing嵌入版本之间观察到的差异的原因