将Javafx嵌入到swing应用程序中

时间:2013-06-03 07:19:33

标签: java swing javafx-2

我有一个巨大的摇摆应用程序,我想嵌入javafx。 我尝试了很多次(通过遵循oracle教程等)并且我只在我声明一个新的JFrame使用JFXPanel组件时才成功。但是,我不想使用新的Frame,我想将我的Javafx代码合并到swing应用程序的根JFrame中。

我们可以将javaFX组件嵌入JPanel而不是JFrame吗? 如果答案是肯定的,我为什么不成功?

代码示例可能有误:

该类直接扩展JPanel,并在EDT中调用initialize方法

private void initialize(){

  setLayout(new BorderLayout());

  final JFXPanel fxPanel = new JFXPanel();
  JPanel jp = new JPanel();

  jp.add(fxPanel);
  jp.setVisible(true);
  jp.setSize(500, 300);
  jp.setBackground(Color.CYAN);

  Platform.runLater(new Runnable() {
     @Override
     public void run() {
     initFX(fxPanel);
     }
  });

  add(createButtonsPanel(), BorderLayout.NORTH);
  add(jp,BorderLayout.CENTER);
}


private static void initFX(JFXPanel fxPanel) {
  Scene scene = initScene();
  fxPanel.setScene(scene);
}


private static Scene initScene(){

  Group  root  =  new  Group();
  Scene  scene  =  new  Scene(root, javafx.scene.paint.Color.ALICEBLUE );
  Text  text  =  new  Text();
  text.setX(40);
  text.setY(100);
  text.setFont(new Font(25));
  text.setText("Welcome JavaFX!");
  root.getChildren().add(text);
  return (scene);

}

2 个答案:

答案 0 :(得分:2)

将代码转换为SSCCE - 一切似乎都有效,这表明问题出现在代码的其他地方。你能用这段代码重现你的问题吗?

import java.awt.Color;
import java.awt.Container;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.*;
import javafx.scene.text.*;
import javax.swing.*;

public class SwingJFXCombo {

    public static Container initialize(){

        final JFXPanel fxPanel = new JFXPanel();
        JPanel jp = new JPanel();

        jp.add(fxPanel);
        jp.setVisible(true);
        // Really shouldn't do this, so commented it out
        //jp.setPreferredSize(new Dimension(500, 300));
        jp.setBackground(Color.CYAN);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
        });

        return jp;
    }

    private static void initFX(JFXPanel fxPanel) {
        Scene scene = initScene();
        fxPanel.setScene(scene);
    }

    private static Scene initScene(){
        Group  root  =  new  Group();
        Scene  scene  =  new  Scene(root, javafx.scene.paint.Color.ALICEBLUE );
        Text  text  =  new  Text();
        text.setX(40);
        text.setY(100);
        text.setFont(new Font(25));
        text.setText("Welcome JavaFX!");
        root.getChildren().add(text);
        return (scene);
    }

    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.add(SwingJFXCombo.initialize());
        frame.pack();
    }
}

答案 1 :(得分:1)

最后,我找到了一种让javafx工作的方法。我使我的SwingJFXCombo类继承自抽象类SimplePullerPanel,它提供了处理pull的方法(SimplePullerPanel扩展了扩展JPanel的抽象类PullerPanel。 为了完成这项工作,我在buildContentPanel

中覆盖了SwingJFXCombo方法
public abstract class SimplePullerPanel extends PullerPanel implements PropertyChangeListener
{
     ...

    protected abstract JComponent buildContentPanel();

    @Override
    protected void buildPanel()
    {

        JComponent oContentPanel = buildContentPanel();
        JPanel panel = new JPanel();
        panel.add(oContentPanel);

        setLayout(new BorderLayout());
        add(panel, BorderLayout.CENTER);
    }

     ...
}

public class SwingJFXCombo extends SimplePullerPanel{

    final JFXPanel fxPanel = new JFXPanel();

    public SwingJFXCombo(){
        setName("fx sample"); 
    }

    private static void initFX(JFXPanel fxPanel) {
        Scene scene = createScene();
        fxPanel.setScene(scene);
    }

    private static Scene createScene(){
        Group  root  =  new  Group();
        Scene  scene  =  new  Scene(root, javafx.scene.paint.Color.ALICEBLUE );

        Text  text  =  new  Text();
        text.setX(40);
        text.setY(100);
        text.setFont(new Font(25));
        text.setText("Welcome JavaFX!");
        root.getChildren().add(text);
        return (scene);
    }

    @Override
    protected JComponent buildContentPanel()
    {
        JPanel frame = new JPanel();
        frame.add(fxPanel);
        frame.setSize(800, 600);
        Platform.runLater(new Runnable() {
            @Override
            public void run()
            {
                initFX(fxPanel);
            }
        });
        return frame;
    }
}

它没有解释为什么以前的代码不起作用,我注意到如果我想通过删除一些我不使用的方法来创建我的自定义SimplePullerPanel我面临同样的问题。 很奇怪

相关问题