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
How to embed JPanel into JavaFX pane?
提问by Jordan Florchinger
How can I add a swingNode
to a specific
pane?
如何将 a 添加swingNode
到specific
窗格?
I'm actually trying to add a JPanel
that loads an appletto the transparent area of the following and I'm not sure how to do it.
我实际上正在尝试添加一个JPanel
将小程序加载到以下透明区域的小程序,但我不知道该怎么做。
回答by ItachiUchiha
SwingNode
is 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 SwingNode
Javadoc):
一个非常简单的代码示例,用于展示如何将其添加到窗格中(来自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);
}
}