java调用javafx,是否可以在Java应用程序中多次调用JavaFX应用程序?

Java多次调用JavaFX的解决方案
博客围绕Java中多次调用JavaFX的问题展开。因launch()不能多次调用,若在Java应用中使用Swing,可在Applications的start方法里构建JavaFX场景图,并嵌入Swing的JFXPanel中,还给出了具体示例代码。

As launch() cannot be called more than once, how do I call, say, a class

public class Graphs extends Application {

more than once? My program runs from a GUI and calls the JavaFX utility with Graph.launch(), and I would like to be able to call the program more than once. How can I do this?

解决方案

Assuming that you are using Swing in your Java application, to make use of JavaFX, you would not use the application class itself, but rather do the things you do in the Applications start method to construct a JavaFX scenegraph and embed this scene graph inside a Swing JFXPanel.

I made a small example:

import javafx.animation.FadeTransition;

import javafx.animation.RotateTransition;

import javafx.animation.Timeline;

import javafx.application.Platform;

import javafx.embed.swing.JFXPanel;

import javafx.scene.Scene;

import javafx.scene.layout.StackPane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.util.Duration;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Swing2Test extends JFrame {

public Swing2Test() {

setTitle("Simple example");

JPanel panel = new JPanel();

getContentPane().add(panel);

JButton button = new JButton("Show JavaFX Dialog");

button.setBounds(50, 60, 80, 30);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent event) {

final JDialog dialog = new JDialog(Swing2Test.this,

"JavaFX Dialog",

true);

final JFXPanel contentPane = new JFXPanel();

dialog.setContentPane(contentPane);

dialog.setDefaultCloseOperation(

JDialog.HIDE_ON_CLOSE);

// building the scene graph must be done on the javafx thread

Platform.runLater(new Runnable() {

@Override

public void run() {

contentPane.setScene(createScene());

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

dialog.pack();

dialog.setVisible(true);

}

});

}

});

}

private Scene createScene() {

System.out.println("creating scene");

StackPane root = new StackPane();

Rectangle rect1 = new Rectangle(10, 10, 50, 50);

rect1.setFill(Color.BLUE);

Rectangle rect2 = new Rectangle(0, 0, 30, 30);

rect2.setFill(Color.ORANGE);

root.getChildren().addAll(rect1, rect2);

FadeTransition ft = new FadeTransition(Duration.millis(1800), rect1);

ft.setFromValue(1.0);

ft.setToValue(0.1);

ft.setCycleCount(Timeline.INDEFINITE);

ft.setAutoReverse(true);

ft.play();

RotateTransition rt = new RotateTransition(Duration.millis(1500), rect2);;

rt.setByAngle(180f);

rt.setCycleCount(Timeline.INDEFINITE);

rt.setAutoReverse(true);

rt.play();

return new Scene(root, 150, 150);

}

});

panel.add(button);

setSize(300, 200);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

Swing2Test ex = new Swing2Test();

ex.setVisible(true);

}

});

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值