如何在现有的WindowBuilder项目中实现JavaFX?

时间:2012-11-03 19:33:47

标签: javafx-2 javafx windowbuilder

我试图了解JavaFX的内容...... 我的程序是基于WindowBuilder的Gui,我想要一个JavaFX图表,以及我的应用程序中显示的JavaFX实时视频源。

如何在我的代码中实现它?我试过了this,但我无法让它奔跑。 数据馈送不是问题。我只需要在JFrame中查看它作为小方块......

现在感到困惑:(

这是我的代码:(很抱歉这有点长,但我责怪JavaFX的示例代码:p

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Canvas;
import java.awt.SystemColor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import javax.swing.UIManager.*;

/**
 * @author 
 * 
 */

public class MyClientApp extends JFrame {

/**
*
*/
private static final long serialVersionUID = 1L;
protected static final String BufferedWriter = null;
JFrame frame;
private JTextField textFieldUsername;

/**
 * Create the application.
 */
public MyClientApp(BufferedWriter serverDataOut, BufferedReader serverDataIn) {

    initialize();
}

/**
 * Initialize the contents of the frame.
 * 
 * @param serverDataOut
 * 
 */

private void initialize() {

    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // Nimbus Theme not avaliable
}

    frame = new JFrame();
        frame.setResizable(false);
    frame.setTitle("*********** My Program ***********");
    frame.setBounds(320, 130, 730, 570); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    Canvas canvasTemp = new Canvas();
    canvasTemp.setBackground(SystemColor.window);
    canvasTemp.setBounds(6, 277, 380, 255);
    frame.getContentPane().add(canvasTemp);

    Canvas canvasLevel = new Canvas();
    canvasLevel.setBackground(SystemColor.window);
    canvasLevel.setBounds(6, 10, 380, 255);
    frame.getContentPane().add(canvasLevel);

}

public JFrame frame() {
    return frame;
}


}

这是让你们为它们运行的​​主要文件......:

Client.java

import java.io.IOException;


public class Client {

public static void main(String[] args) throws IOException, InterruptedException {

    MyClientApp window = new MyClientApp(null, null);

    window.frame.setVisible(true);

}

}

我想要实现的代码是: AdvancedLineChartSample.java(来自JavaFX)

/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.util.Duration;

/**
 * An advanced line chart with a variety of actions and settable properties.
 *
 * @see javafx.scene.chart.LineChart
 * @see javafx.scene.chart.Chart
 * @see javafx.scene.chart.NumberAxis
 * @see javafx.scene.chart.XYChart
 */
public class AdvancedLineChartSample extends Application {

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

protected LineChart<Number, Number> createChart() {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    final LineChart<Number,Number> lc = new LineChart<Number,Number>(xAxis,yAxis);
    // setup chart
    lc.setTitle("Temp Chart");
    xAxis.setLabel("tid");
    yAxis.setLabel("temp");
    // add starting data
    XYChart.Series<Number,Number> series = new XYChart.Series<Number,Number>();
    series.setName("Dataset 1");
    series.getData().add(new XYChart.Data<Number,Number>(20d, 50d));
    series.getData().add(new XYChart.Data<Number,Number>(40d, 80d));
    series.getData().add(new XYChart.Data<Number,Number>(50d, 90d));
    series.getData().add(new XYChart.Data<Number,Number>(70d, 30d));
    series.getData().add(new XYChart.Data<Number,Number>(170d, 122d));
    lc.getData().add(series);
    return lc;
}

@Override public void start(Stage primaryStage) throws Exception {
    init(primaryStage);
    primaryStage.show();
}
public static void main(String[] args) { launch(args); 

}

}

和StreamingMediaPlayer.java(来自JavaFX):

/**

* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.util.Duration;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.animation.ParallelTransition;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;

/**
* A media player with controls for play, pause, stop, seek, and volume. This media player is playing media via HTTP Live Streaming, also known as HLS. 
*
* @see javafx.scene.media.MediaPlayer
* @see javafx.scene.media.Media
*/
public class StreamingMediaPlayer extends Application {
private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/JavaRap/prog_index.m3u8";

private MediaPlayer mediaPlayer;

private void init(Stage primaryStage) {
    Group root = new Group();
    primaryStage.setScene(new Scene(root));
    mediaPlayer = new MediaPlayer(new Media(MEDIA_URL));
    mediaPlayer.setAutoPlay(true);
    PlayerPane playerPane = new PlayerPane(mediaPlayer);
    playerPane.setMinSize(480, 360);  
    playerPane.setPrefSize(480, 360);
    playerPane.setMaxSize(480, 360);
   // getStylesheets().add("ensemble/samples/media/OverlayMediaPlayer.css");
    root.getChildren().add(playerPane);
}

public void play() {
    Status status = mediaPlayer.getStatus();
    if (status == Status.UNKNOWN || status == Status.HALTED) {
        return;
    }
    if (status == Status.PAUSED || status == Status.STOPPED || status == Status.READY)     {
        mediaPlayer.play();
    }
}

@Override public void stop() {
    mediaPlayer.stop();
}

static class PlayerPane extends BorderPane {
    private MediaPlayer mp;
    private MediaView mediaView;
    private final boolean repeat = false;
    private boolean stopRequested = false;
    private boolean atEndOfMedia = false;
    private Duration duration;
    private Slider timeSlider;
    private Label playTime;
    private Slider volumeSlider;
    private HBox mediaTopBar;
    private HBox mediaBottomBar;
    private ParallelTransition transition = null;

    @Override protected void layoutChildren() {
        if (mediaView != null && getBottom() != null) {
            mediaView.setFitWidth(getWidth());
            mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1));
        }
        super.layoutChildren();
        if (mediaView != null) {
            mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
            mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2);
        }
    }

    @Override protected double computeMinWidth(double height) {
        return mediaBottomBar.prefWidth(-1);
    }

    @Override protected double computeMinHeight(double width) {
        return 200;
    }

    @Override protected double computePrefWidth(double height) {
        return Math.max(mp.getMedia().getWidth(), mediaBottomBar.prefWidth(height));
    }

    @Override protected double computePrefHeight(double width) {
        return mp.getMedia().getHeight() + mediaBottomBar.prefHeight(width);
    }

    @Override protected double computeMaxWidth(double height) { return Double.MAX_VALUE; }

    @Override protected double computeMaxHeight(double width) { return Double.MAX_VALUE; }

    public PlayerPane(final MediaPlayer mp) {
        this.mp = mp;
        setId("player-pane");

        mediaView = new MediaView(mp);
        Pane mvPane = new Pane() { };
        mvPane.setId("media-pane");
        mvPane.getChildren().add(mediaView);
        setCenter(mvPane);

        mediaTopBar = HBoxBuilder.create()
                .padding(new Insets(5, 10, 5, 10))
                .alignment(Pos.CENTER)
                .opacity(1)
                .build();
        BorderPane.setAlignment(mediaTopBar, Pos.CENTER);

        mediaBottomBar = HBoxBuilder.create()
                .padding(new Insets(5, 10, 5, 10))
                .alignment(Pos.CENTER)
                .opacity(1)
                .build();
        BorderPane.setAlignment(mediaBottomBar, Pos.CENTER);

        mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {
            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                updateValues();
            }
        });
        mp.setOnPlaying(new Runnable() {
            public void run() {
                if (stopRequested) {
                    mp.pause();
                    stopRequested = false;
                } 
            }
        });
        mp.setOnReady(new Runnable() {
            public void run() {
                duration = mp.getMedia().getDuration();
                updateValues();
            }
        });
        mp.setOnEndOfMedia(new Runnable() {
            public void run() {
                if (!repeat) {
                    stopRequested = true;
                    atEndOfMedia = true;
                }
            }
        });
        mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);

        // Time label
        Label timeLabel = LabelBuilder.create()
                .text("Time")
                .minWidth(Control.USE_PREF_SIZE)
                .textFill(Color.WHITE)
                .build();
        mediaTopBar.getChildren().add(timeLabel);

        // Time slider
        timeSlider = SliderBuilder.create()
                .id("media-slider")
                .minWidth(240)
                .maxWidth(Double.MAX_VALUE)
                .build();
        timeSlider.valueProperty().addListener(new InvalidationListener() {
            public void invalidated(Observable ov) {
                if (timeSlider.isValueChanging()) {
                    // multiply duration by percentage calculated by slider position
                    if (duration != null) {
                        mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
                    }
                    updateValues();

                }
            }
        });
        HBox.setHgrow(timeSlider, Priority.ALWAYS);
        mediaTopBar.getChildren().add(timeSlider);

        // Play label
        playTime = LabelBuilder.create()
                .prefWidth(130)
                .minWidth(50)
                .textFill(Color.WHITE)
                .build();
        mediaTopBar.getChildren().add(playTime);

        // Volume label
        Label volumeLabel = LabelBuilder.create()
                .text("Vol")
                .textFill(Color.WHITE)
                .minWidth(Control.USE_PREF_SIZE)
                .build();
        mediaTopBar.getChildren().add(volumeLabel);

        // Volume slider
        volumeSlider = SliderBuilder.create()
                .id("media-slider")
                .prefWidth(120)
                .maxWidth(Region.USE_PREF_SIZE)
                .minWidth(30)
                .build();
        volumeSlider.valueProperty().addListener(new InvalidationListener() {
            public void invalidated(Observable ov) {
            }
        });
        volumeSlider.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                if (volumeSlider.isValueChanging()) {
                    mp.setVolume(volumeSlider.getValue() / 100.0);
                }
            }
        });
        mediaTopBar.getChildren().add(volumeSlider);

        setTop(mediaTopBar);

        final EventHandler<ActionEvent> backAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.seek(Duration.ZERO);
            }
        };
        final EventHandler<ActionEvent> stopAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.stop();
            }
        };
        final EventHandler<ActionEvent> playAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.play();
            }
        };
        final EventHandler<ActionEvent> pauseAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                mp.pause();
            }
        };
        final EventHandler<ActionEvent> forwardAction = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                Duration currentTime = mp.getCurrentTime();
                mp.seek(Duration.seconds(currentTime.toSeconds() + 5.0));
            }
        };

        mediaBottomBar = HBoxBuilder.create()
                .id("bottom")
                .spacing(0)
                .alignment(Pos.CENTER)
                .children(
                    ButtonBuilder.create()
                        .id("back-button")
                        .text("Back")
                        .onAction(backAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("stop-button")
                        .text("Stop")
                        .onAction(stopAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("play-button")
                        .text("Play")
                        .onAction(playAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("pause-button")
                        .text("Pause")
                        .onAction(pauseAction)
                        .build(),
                    ButtonBuilder.create()
                        .id("forward-button")
                        .text("Forward")
                        .onAction(forwardAction)
                        .build()
                 )
                .build();

        setBottom(mediaBottomBar);
    }

    protected void updateValues() {
        if (playTime != null && timeSlider != null && volumeSlider != null && duration != null) {
            Platform.runLater(new Runnable() {
                public void run() {
                    Duration currentTime = mp.getCurrentTime();
                    playTime.setText(formatTime(currentTime, duration));
                    timeSlider.setDisable(duration.isUnknown());
                    if (!timeSlider.isDisabled() && duration.    greaterThan(Duration.ZERO) && !timeSlider.isValueChanging()) {
                        timeSlider.setValue(currentTime.divide(duration).toMillis() * 100.0);
                    }
                    if (!volumeSlider.isValueChanging()) {
                        volumeSlider.setValue((int) Math.round(mp.getVolume() * 100));
                    }
                }
            });
        }
    }

    private static String formatTime(Duration elapsed, Duration duration) {
        int intElapsed = (int)Math.floor(elapsed.toSeconds());
        int elapsedHours = intElapsed / (60 * 60);
        if (elapsedHours > 0) {
            intElapsed -= elapsedHours * 60 * 60;
        }
        int elapsedMinutes = intElapsed / 60;
        int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60;

        if (duration.greaterThan(Duration.ZERO)) {
            int intDuration = (int)Math.floor(duration.toSeconds());
            int durationHours = intDuration / (60 * 60);
            if (durationHours > 0) {
                intDuration -= durationHours * 60 * 60;
            }
            int durationMinutes = intDuration / 60;
            int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60;

            if (durationHours > 0) {
                return String.format("%d:%02d:%02d",
                                     elapsedHours, elapsedMinutes, elapsedSeconds);
            } else {
                return String.format("%02d:%02d",
                                     elapsedMinutes, elapsedSeconds);
            }
        } else {
            if (elapsedHours > 0) {
                return String.format("%d:%02d:%02d",
                                     elapsedHours, elapsedMinutes, elapsedSeconds);
            } else {
                return String.format("%02d:%02d",
                                     elapsedMinutes, elapsedSeconds);
            }
        }
    }
}

@Override public void start(Stage primaryStage) throws Exception {
    init(primaryStage);
    primaryStage.show();
    play();
}
public static void main(String[] args) { launch(args); 
}
}

我对长代码感到抱歉。它只是来自JavaFX的示例代码。您会发现它herehere

1 个答案:

答案 0 :(得分:5)

这应该很容易解决。让我用您展示的图表示例来解释它。

  1. JFXPanel添加JFrame的实例。在您的示例中,所有组件都添加到Stage,这是表示窗口的JavaFX类。所以你在这里不需要它。而是将要使用的组件添加到JFXPanel。另请参阅here(函数initAndShowGUI)如何执行此操作。

  2. 在示例的init函数中,创建了Scene以及图表本身。要让图表显示,您需要做的就是创建一个Scene,用内容填充并将其传递给您已创建的JFXPanel

  3. 只需付出最少的努力,您就可以运行图表示例:确保AdvancedLineChartSample.java位于您的构建路径中,并且可以通过JFrame以某种方式访问​​函数createChart。然后使用类似于以下代码段的代码将图表添加到您的代码中。

  4.     Group root=new Group(); 
        Scene scene=new Scene(root);
        myJFXPanel.setScene(scene);
        root.getChildren().add(createChart());
    

    这只是一个非常快速和肮脏的解决方案来运行您的示例没有任何漂亮的代码,我也没有测试它。但希望它能让您基本了解正在进行的进一步实验。根据我自己的经验,我可以告诉你,从这一步开始,JavaFX 2带来了很多乐趣。