使用集成Swing,JavaFX在onmouseover上闪烁(重新粉刷)

时间:2014-01-24 09:41:35

标签: java swing javafx integration

我尝试将javaFX应用程序集成到Swing应用程序中,并成功完成。

但问题是,当鼠标悬停在按钮上时,我无法解决javaFX相关面板闪烁(重绘速度太快)的问题。

有没有解决方案,还是我会尝试将Swing集成到javaFX中?哪个会烦人。

private void initFX(JFXPanel fxPanel) throws IOException {
    // This method is invoked on the JavaFX thread
    scene = createScene();
    fxPanel.setScene(scene);
}

public MainFrame()  {

    Panel container = new Panel();
    container.setLayout(null);
    try {
        final JFXPanel panel = new JFXPanel();
        Insets insets = container.getInsets();
        container.add(panel);

        JApplet mapApplet = (JApplet)Class.forName("samples.mainApp.MainApplet").newInstance();
        container.add(mapApplet); //Display Applet
        add(container);

        mapApplet.setBounds(800, 0, 566, 720);
        panel.setBounds(0, 0, 800, 720);

        mapApplet.init();
        mapApplet.start();  

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    initFX(panel);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
       });
    } catch (InstantiationException | IllegalAccessException
            | ClassNotFoundException e) {
        e.printStackTrace();
    }
    //button.setVisible(false);
    catch (Exception e) {
        e.printStackTrace();
    }
}
private Scene createScene() throws IOException {
            root = new  Group();
            scene  =  new  Scene(root, Color.ALICEBLUE);
            Button btn1 = new Button("Connect");
            root.getChildren().addAll(btn1);
}

1 个答案:

答案 0 :(得分:2)

  但问题是,我无法解决javaFX相关的问题   将鼠标悬停在按钮上时,面板闪烁(重新绘制速度很慢)。

     

有没有解决方案,还是我会尝试将Swing集成到javaFX中?   哪个会烦人

  • 从未见过,也没有闪烁(不知道怎么运行,从你这里发布的代码中模拟出来)

  • SSCCE / MCVE发布更快的帮助,简短,可运行,可编辑,由上午和描述的问题引起(请注意测试并查看SwingUtilities.invokeLater的使用情况Platform.runLater

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JavaFX_And_Swing extends JApplet {

    private final int WIDTH = 300;
    private final int HEIGHT = 250;
    private static JFXPanel fxContainer;
    private static JFXPanel fxContainerTwo;
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                }
                JFrame frame = new JFrame("JavaFX embeded in Swing");
                frame.setLayout(new BorderLayout(5, 5));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JApplet applet = new JavaFX_And_Swing();
                applet.init();
                frame.setContentPane(applet.getContentPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                applet.start();
            }
        });
    }

    @Override
    public void init() {
        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(WIDTH / 5, HEIGHT / 5));
        add(fxContainer, BorderLayout.NORTH);
        fxContainerTwo = new JFXPanel();
        fxContainerTwo.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        add(fxContainerTwo, BorderLayout.CENTER);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
                createScene2();
            }
        });
    }

    private void createScene() {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, Color.BLUEVIOLET);
        fxContainer.setScene(scene);
    }

    private void createScene2() {
        Button btn = new Button();
        btn.setText("Say 'Hello World' Two");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, Color.ALICEBLUE);
        fxContainerTwo.setScene(scene);
    }
}