java 如何将 JPanel 嵌入到 JavaFX 窗格中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29271239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:58:01  来源:igfitidea点击:

How to embed JPanel into JavaFX pane?

javaswingjavafxjpanelpane

提问by Jordan Florchinger

How can I add a swingNodeto a specificpane?

如何将 a 添加swingNodespecific窗格?

I'm actually trying to add a JPanelthat loads an appletto the transparent area of the following and I'm not sure how to do it.

我实际上正在尝试添加一个JPanel小程序加载到以下透明区域的小程序,但我不知道该怎么做。

enter image description here

在此处输入图片说明

回答by ItachiUchiha

SwingNodeis a javafx scene nodeand can be added to any javafx scene layouts.

SwingNode是一个javafx 场景节点,可以添加到任何javafx 场景布局中

To add a JPanel to a Pane and display it on JavaFX stage:

要将 JPanel 添加到 Pane 并将其显示在 JavaFX 舞台上:

  • Add JPanel to a SwingNode
  • Assign the swingnode as a child to any of the layouts (which includes Pane).
  • Set the layout as the root of the scene
  • Set the scene to the stage and display it
  • 将 JPanel 添加到 SwingNode
  • 将 Swingnode 作为子节点分配给任何布局(包括 Pane)。
  • 将布局设置为场景的根
  • 将场景设置到舞台并显示

A very simple code sample to show how you can add it to a Pane is (from SwingNodeJavadoc):

一个非常简单的代码示例,用于展示如何将其添加到窗格中(来自SwingNodeJavadoc):

public class SwingNodeExample extends Application {

     @Override
     public void start(Stage stage) {
         final SwingNode swingNode = new SwingNode();
         createAndSetSwingContent(swingNode);

         Pane pane = new Pane();
         pane.getChildren().add(swingNode); // Adding swing node

         stage.setScene(new Scene(pane, 100, 50));
         stage.show();
     }

     private void createAndSetSwingContent(final SwingNode swingNode) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 JPanel panel = new JPanel();
                 panel.add(new JButton("Click me!"));
                 swingNode.setContent(panel);
             }
         });
     }

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