我正在尝试将JFXPanel添加到我的JFrame中。我正在使用Netbeans和Netbeans的Swing GUI Builder。当我运行它时,我没有得到任何错误,并且JFrame已成功创建,但带有文本的JFXPanel未出现。
这是我的代码:
public class TestingFX {
private static void initJFrame() {
NewJFrame frame = new NewJFrame();
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setVisible(true);
Platform.runLater(new Runnable() {
@Override
public void run() {
fxPanel.setScene(createScene());
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initJFrame();
}
});
}
public static Scene createScene() {
Text text = new Text("Hello World");
text.setFont(new Font(24));
text.setEffect(new Reflection());
BorderPane pane = new BorderPane();
pane.setCenter(text);
Scene scene = new Scene(pane);
return scene;
}
}
为什么不起作用?我做错了吗?
答案 0 :(得分:3)
也许您应该阅读Integrating JavaFX into Swing Applications,然后相应地重构您的代码。
尝试
public class Demo extends Application{
private static JFXPanel javafxPanel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
public static void initAndShowGUI() {
JFrame frame = new JFrame("Swing application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create JavaFX panel.
javafxPanel = new JFXPanel();
frame.getContentPane().add(javafxPanel, BorderLayout.CENTER);
// Create JavaFX scene.
Application.launch (Demo.class, null);
// Show frame.
frame.pack();
frame.setVisible(true);
}
@Override
public void start (Stage mainStage) {
// Add scene to panel
javafxPanel.setScene(createScene());
}
private static Scene createScene() {
Text text = new Text("Hello World");
text.setFont(new Font(24));
text.setEffect(new Reflection());
BorderPane pane = new BorderPane();
pane.setCenter(text);
Scene scene = new Scene(pane);
return scene;
}
}
创建场景时,此示例假定您包含的代码有效。
答案 1 :(得分:0)
我试图将JFXPanel添加到JPanel,这解决了我的问题,也可以帮助你
jpanel1.setLayout(new FlowLayout());