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

Javafx 2 如何在现有的WindowBuilder项目中实现JavaFX?,javafx-2,javafx,windowbuilder,Javafx 2,Javafx,Windowbuilder,我正试着了解一下JavaFX的东西。。。 我的程序是一个基于WindowBuilder的Gui,我希望在我的应用程序中显示一个JavaFX图形和一个JavaFX实时视频提要 如何在代码中实现它?我试过了,但没能让它运行。 数据馈送不是问题所在。我只需要在我的JFrame内将其视为小正方形 现在感到困惑:( 这是我的代码:(很抱歉,它有点长,但我谴责来自JavaFX:p的示例代码 import javax.swing.JFrame; import javax.swing.JTextField; i

我正试着了解一下JavaFX的东西。。。 我的程序是一个基于WindowBuilder的Gui,我希望在我的应用程序中显示一个JavaFX图形和一个JavaFX实时视频提要

如何在代码中实现它?我试过了,但没能让它运行。 数据馈送不是问题所在。我只需要在我的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)

/**
*版权所有(c)2008、2012 Oracle和/或其附属公司。
*保留所有权利。使用受许可条款约束。
*/
导入javafx.application.application;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.stage.stage;
导入java.util.ArrayList;
导入java.util.List;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Side;
导入javafx.scene.Node;
导入javafx.scene.chart.LineChart;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.chart.XYChart;
导入javafx.scene.chart.XYChart.Series;
导入javafx.util.Duration;
/**
*具有各种操作和可设置属性的高级折线图。
*
*@请参阅javafx.scene.chart.LineChart
*@see javafx.scene.chart.chart
*@see javafx.scene.chart.NumberAxis
*@请参阅javafx.scene.chart.XYChart
*/
公共类AdvancedLineChartSample扩展应用程序{
私有void init(阶段primaryStage){
组根=新组();
primaryStage.setScene(新场景(根));
root.getChildren().add(createChart());
}
受保护的折线图createChart(){
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
最终线形图lc=新线形图(xAxis,yAxis);
//设置图
立法会:设定标题(“临时图表”);
xAxis.setLabel(“tid”);
yAxis.设置标签(“温度”);
//添加起始数据
XYChart.Series系列=新的XYChart.Series();
series.setName(“数据集1”);
series.getData().add(新的XYChart.Data(20d,50d));
series.getData().add(新的XYChart.Data(40d,80d));
series.getData().add(新的XYChart.Data(50d,90d));
series.getData().add(新的XYChart.Data(70d,30d));
series.getData().add(新的XYChart.Data(170d,122d));
lc.getData().add(系列);
返回信用证;
}
@覆盖公共无效开始(阶段primaryStage)引发异常{
初始阶段;
primaryStage.show();
}
公共静态void main(字符串[]args){launch(args);
}
}
以及StreamingMediaPlayer.java(来自JavaFX):

/**
*版权所有(c)2008、2012 Oracle和/或其附属公司。
*保留所有权利。使用受许可条款约束。
*/
导入javafx.application.application;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.stage.stage;
导入javafx.application.Platform;
导入javafx.beans.InvalizationListener;
导入javafx.beans.Observable;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.control.Label;
导入javafx.scene.control.Slider;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.Pane;
导入javafx.scene.layout.Priority;
导入javafx.scene.layout.Region;
导入javafx.scene.media.media;
导入javafx.scene.media.MediaPlayer;
导入javafx.scene.media.MediaPlayer.Status;
导入javafx.scene.media.MediaView;
导入javafx.util.Duration;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.animation.ParallelTransition;
导入javafx.scene.control.*;
导入javafx.scene.layout.*;
导入javafx.scene.paint.Color;
/**
*具有播放、暂停、停止、搜索和音量控制的媒体播放器。此媒体播放器通过HTTP实时流媒体(也称为HLS)播放媒体。
*
*@see javafx.scene.media.MediaPlayer
*@see javafx.scene.media.media
*/
公共类StreamingMediaPlayer扩展应用程序{
私有静态最终字符串媒体\u URL=”http://download.oracle.com/otndocs/products/javafx/JavaRap/prog_index.m3u8";
私人媒体播放器;
私有void init(阶段primaryStage){
组根=新组();
primaryStage.setScene(新场景(根));
mediaPlayer=新mediaPlayer(新媒体(媒体URL));
mediaPlayer.setAutoPlay(true);
PlayerPane PlayerPane=新的PlayerPane(mediaPlayer);
playerPane.setMinSize(480360);
playerPane.setPrefSize(480360);
playerPane.setMaxSize(480360);
//getStylesheets().add(“emose/samples/media/overlymediaplayer.css”);
root.getChildren().add(playerPane);
}
公共游戏{
Status Status=mediaPlayer.getStatus();
if(status==status.UNKNOWN | | status==status.stopped){
返回;
}
if(status==status.PAUSED | | status==status.STOPPED | | status==status.READY){
mediaPlayer.play();
}
}
@覆盖公共无效停止(){
mediaPlayer.stop();
}
静态类PlayerPane扩展边框窗格{
私人媒体播放器;
私有媒体视图;
私有最终布尔重复=false;
私有布尔stopRequested=false;
私有布尔值atEndOfMedia=false;
私人期限;
专用滑条时间滑块;
自有品牌游戏时间;
私人滑杆滑杆;
私人HBox mediaTopBar;
私人HBox mediaBottomBar;
私有并行转换=null;
@重写受保护的void layoutChildren(){
if(mediaView!=null&&getBottom()!=null){
setFitWidth(getWidth());
setFitHeight(getHeight()-getBottom().prefHeight(-1));
}
super.layoutChild
/**
* 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); 

}

}
/**

* 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); 
}
}
Group root=new Group(); Scene scene=new Scene(root); myJFXPanel.setScene(scene); root.getChildren().add(createChart());