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

在Java应用程序中是否可以多次调用JavaFX应用程序?,javafx,Javafx,由于launch()不能被多次调用,因此如何调用(比如)类 public class Graphs extends Application { 不止一次?我的程序从GUI运行,并使用Graph.launch()调用JavaFX实用程序,我希望能够多次调用该程序。我如何做到这一点?假设您在Java应用程序中使用Swing,为了使用JavaFX,您不会使用application类本身,而是使用Applicationsstart方法来构造JavaFX场景图并将此场景图嵌入Swing中 我举了一个小例

由于launch()不能被多次调用,因此如何调用(比如)类

public class Graphs extends Application {

不止一次?我的程序从GUI运行,并使用Graph.launch()调用JavaFX实用程序,我希望能够多次调用该程序。我如何做到这一点?

假设您在Java应用程序中使用Swing,为了使用JavaFX,您不会使用application类本身,而是使用Applications
start
方法来构造JavaFX场景图并将此场景图嵌入Swing中

我举了一个小例子:

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);
      }
    });
  }
}

假设您在Java应用程序中使用Swing,为了使用JavaFX,您不会使用应用程序类本身,而是使用Applications
start
方法来构造JavaFX场景图并将此场景图嵌入Swing中

我举了一个小例子:

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);
      }
    });
  }
}

假设您在Java应用程序中使用Swing,为了使用JavaFX,您不会使用应用程序类本身,而是使用Applications
start
方法来构造JavaFX场景图并将此场景图嵌入Swing中

我举了一个小例子:

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);
      }
    });
  }
}

假设您在Java应用程序中使用Swing,为了使用JavaFX,您不会使用应用程序类本身,而是使用Applications
start
方法来构造JavaFX场景图并将此场景图嵌入Swing中

我举了一个小例子:

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);
      }
    });
  }
}

您只能在Java虚拟机中启动单个应用程序。该方法的Javadoc指定:

不能多次调用它,否则将引发异常


如果您的应用程序是Swing应用程序,那么您可以使用Sebastian回答中概述的技术,类似地,SWT应用程序也可以使用


如果您的应用程序是希望用作其他应用程序“启动器”的JavaFX应用程序,则启动器可以在某些信号上创建新的阶段(例如按下启动应用程序X按钮),创建目标应用程序的新实例(使用
new
),并在目标应用程序上调用该方法,进入目标阶段。

您只能在Java虚拟机中启动单个应用程序。该方法的Javadoc指定:

不能多次调用它,否则将引发异常


如果您的应用程序是Swing应用程序,那么您可以使用Sebastian回答中概述的技术,类似地,SWT应用程序也可以使用


如果您的应用程序是希望用作其他应用程序“启动器”的JavaFX应用程序,则启动器可以在某些信号上创建新的阶段(例如按下启动应用程序X按钮),创建目标应用程序的新实例(使用
new
),并在目标应用程序上调用该方法,进入目标阶段。

您只能在Java虚拟机中启动单个应用程序。该方法的Javadoc指定:

不能多次调用它,否则将引发异常


如果您的应用程序是Swing应用程序,那么您可以使用Sebastian回答中概述的技术,类似地,SWT应用程序也可以使用


如果您的应用程序是希望用作其他应用程序“启动器”的JavaFX应用程序,则启动器可以在某些信号上创建新的阶段(例如按下启动应用程序X按钮),创建目标应用程序的新实例(使用
new
),并在目标应用程序上调用该方法,进入目标阶段。

您只能在Java虚拟机中启动单个应用程序。该方法的Javadoc指定:

不能多次调用它,否则将引发异常


如果您的应用程序是Swing应用程序,那么您可以使用Sebastian回答中概述的技术,类似地,SWT应用程序也可以使用


如果您的应用程序是希望用作其他应用程序“启动器”的JavaFX应用程序,则启动器可以在某些信号上创建新的阶段(例如按下启动应用程序X按钮),创建目标应用程序的新实例(使用
new
),并在目标应用程序上调用该方法,通过目标阶段。

请提供一个代码示例?start方法是否被重写了两次?请提供一个代码示例?start方法是否被重写了两次?请提供一个代码示例?start方法是否被重写了两次?请提供一个代码示例?start方法是否被重写两次?