我可以在 JFrame 中嵌入 JavaFX 应用程序 class 吗?
Can I embed a JavaFX Application class inside a JFrame?
我正在制作一个 'Weather Station' 应用程序,我正在使用 JavaFX 绘制流图。我想知道是否可以将图形与我希望显示的其他组件一起放在 JFrame 中,如果可以,如何。
class我要里面的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 class 是标准挥杆 JFrame
您可以将 Scene
放在 JFXPanel
中,从而将其包含在 Swing 应用程序中。 This tutorial 有更多详细信息。
您不能将 Application
subclass 直接嵌入到 Swing 应用程序中; Application
subclass 是启动 class;它代表实际应用程序,而不是 UI 元素。因此,您需要稍微重构 JavaFX 代码才能使其正常工作。
具体来说,我会定义一个 class 例如:
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();
}
}
然后你可以定义一个Application
class:
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();
}
}
或者您可以在 Swing 应用程序中使用它:
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();
});
我正在制作一个 'Weather Station' 应用程序,我正在使用 JavaFX 绘制流图。我想知道是否可以将图形与我希望显示的其他组件一起放在 JFrame 中,如果可以,如何。
class我要里面的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 class 是标准挥杆 JFrame
您可以将 Scene
放在 JFXPanel
中,从而将其包含在 Swing 应用程序中。 This tutorial 有更多详细信息。
您不能将 Application
subclass 直接嵌入到 Swing 应用程序中; Application
subclass 是启动 class;它代表实际应用程序,而不是 UI 元素。因此,您需要稍微重构 JavaFX 代码才能使其正常工作。
具体来说,我会定义一个 class 例如:
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();
}
}
然后你可以定义一个Application
class:
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();
}
}
或者您可以在 Swing 应用程序中使用它:
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();
});