我可以在JFrame中嵌入JavaFX Application类吗?

时间:2015-03-06 14:27:56

标签: java swing javafx jframe extends

我正在制作一个气象站'应用程序,我正在使用JavaFX绘制流图。我想知道我是否可以将图形与我希望显示的其他组件一起放在JFrame中,如果是,如何。

我想要的课程JFrame

public class JavaFXApplication6 extends Application
{

    private XYChart.Series<Number, Number> hourDataSeries;
    private XYChart.Series<Number, Number> minuteDataSeries;
    private NumberAxis xAxis;
    private Timeline animation;

    // more variables handling data manipulation based on time passing

    private void init(Stage primaryStage)
    {
        Group root = new Group();
        primaryStage.setScene(new Scene(root));
        root.getChildren().add(createChart());

        // create timeline to add new data every 60th of second
        animation = new Timeline();
        animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000 / 60), 
                                     new EventHandler<ActionEvent>()
                                     {
                                        @Override
                                        public void handle(ActionEvent actionEvent)
                                        {
                                            // 6 minutes data per frame
                                            for (int count = 0; count < 6; count++)
                                            {
                                                nextTime();
                                                plotTime();
                                            }
                                        }
                                     }));

        animation.setCycleCount(Animation.INDEFINITE);
    }

    protected LineChart<Number, Number> createChart()
    {

        // Code to setup chart and starting data and return it - substitute
        // with:
        return new LineChart(new NumberAxis(0, 24, 3), new NumberAxis(0, 100, 10));
    }

    private void nextTime()
    {
        // Code to advance time variables
    }

    private void plotTime() {
        // update data series' based on time passing
    }

    public void play()
    {
        animation.play();
    }

    @Override
    public void stop()
    {
        animation.pause();
    }

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        init(primaryStage);
        primaryStage.show();
        play();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

GUI类是标准摆动JFrame

1 个答案:

答案 0 :(得分:3)

您可以将Scene放在JFXPanel中,在Swing应用程序中添加ApplicationThis tutorial有更多详情。

您不能将Application子类直接嵌入到Swing应用程序中; public class AnimatedChart { private XYChart.Series<Number, Number> hourDataSeries; private XYChart.Series<Number, Number> minuteDataSeries; private NumberAxis xAxis; private Timeline animation; private Group view ; // more variables handling data manipulation based on time passing public AnimatedChart() { view = new Group(); primaryStage.setScene(new Scene(view)); view.getChildren().add(createChart()); // create timeline to add new data every 60th of second animation = new Timeline(); animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000 / 60), new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { // 6 minutes data per frame for (int count = 0; count < 6; count++) { nextTime(); plotTime(); } } })); animation.setCycleCount(Animation.INDEFINITE); } public Parent getView() { return view ; } protected LineChart<Number, Number> createChart() { // Code to setup chart and starting data and return it - substitute // with: return new LineChart(new NumberAxis(0, 24, 3), new NumberAxis(0, 100, 10)); } private void nextTime() { // Code to advance time variables } private void plotTime() { // update data series' based on time passing } public void play() { animation.play(); } public void pause() { animation.pause(); } } 子类是启动类;它代表实际的应用程序,而不是UI元素。因此,您需要稍微重构一下JavaFX代码才能使其正常工作。

具体来说,我会定义一个类,如:

Application

然后你可以定义一个public void AnimatedChartApp extends Application { private AnimatedChart animatedChart ; @Override public void start(Stage primaryStage) { animatedChart = new AnimatedChart(); Scene scene = new Scene(animatedChart.getView()); primaryStage.setScene(scene); animatedChart.play(); primaryStage.show(); } @Override public void stop() { animatedChart.stop(); } } 类:

JFrame frame = new JFrame();
JFXPanel jfxPanel = new JFXPanel();
frame.add(jfxPanel);
frame.setVisible(true);
Platform.runLater(() -> {
    AnimatedChart animatedChart = new AnimatedChart();
    Scene scene = new Scene(animatedChart.getView());
    jfxPanel.setScene(scene);
    animatedChart.play();
});

或者您可以在Swing应用程序中使用它:

{{1}}