2014-01-15 83 views

回答

2

假設您在Java應用程序中使用Swing,爲了使用JavaFX,您不會使用應用程序類本身,而應該在應用程序start方法中執行以構建JavaFX場景圖並將其嵌入場景圖內擺動JFXPanel

我做了一個小例子:

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

只能推出的Java虛擬機的單個應用程序。方法Application::launch()的Javadoc指定:

它不能被多次調用,否則將拋出異常。


如果您的應用程序是一個Swing應用程序,你可以改用在塞巴斯蒂安的答案中概述的技術,同樣一個SWT應用程序可以使用FXCanvas

如果您的應用程序是您希望用作其他應用程序的「啓動程序」的JavaFX應用程序,則啓動程序可以在某些信號上創建一個新的Stage(例如按下啓動應用程序X按鈕),創建一個新的實例目標應用程序(使用new),並在目標應用程序上調用start方法,傳入目標階段。

相關問題