如何将JPanel嵌入JavaFX窗格? [英] How to embed JPanel into JavaFX pane?

查看:229
本文介绍了如何将JPanel嵌入JavaFX窗格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在specific窗格中添加swingNode?

我实际上是在尝试添加一个JPanel来将 applet 加载到以下内容的透明区域中,我不确定该怎么做.

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

推荐答案

SwingNode javafx场景布局.

SwingNode is a javafx scene node and can be added to any javafx scene layouts.

要将JPanel添加到窗格并在JavaFX舞台上显示它,

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

  • 将JPanel添加到SwingNode
  • 将swingnode作为子级分配给任何布局(包括Pane).
  • 将布局设置为场景的根
  • 将场景设置为舞台并显示

一个非常简单的代码示例显示了如何将其添加到窗格中(来自SwingNode

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

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

这篇关于如何将JPanel嵌入JavaFX窗格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆