By default, the code template creates an applet contained within a JFrame. However, we can change to code to subclass from JFrame instead of applet. The modified code is shown below:
JavaVideoPlayer.java |
1 package JPRG; 2 3 import javafx.application.Platform; 4 import javafx.embed.swing.JFXPanel; 5 import javafx.event.*; 6 import javafx.scene.Scene; 7 import javafx.scene.control.Button; 8 import javafx.scene.layout.StackPane; 9 import java.awt.*; 10 import javax.swing.*; 11 12 public class JavaVideoPlayer extends JFrame { 13 14 private static final int JFXPANEL_WIDTH_INT = 640; 15 private static final int JFXPANEL_HEIGHT_INT = 480; 16 private static JFXPanel fxContainer; 17 18 public static void main(String[] args) { 19 SwingUtilities.invokeLater(new Runnable() { 20 @Override 21 public void run() { 22 JavaVideoPlayer frame = new JavaVideoPlayer(); 23 frame.initialize(); 24 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); 25 frame.pack(); 26 frame.setLocationRelativeTo(null); 27 frame.setVisible(true); 28 } 29 }); 30 } 31 32 public void initialize() { 33 fxContainer = new JFXPanel(); 34 fxContainer.setPreferredSize( 35 new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT)); 36 add(fxContainer, BorderLayout.CENTER); 37 // create JavaFX scene 38 Platform.runLater(new Runnable() { 39 @Override 40 public void run() { 41 createScene(); 42 } 43 }); 44 } 45 46 private void createScene() { 47 Button btn = new Button(); 48 btn.setText("Say 'Hello World'"); 49 btn.setOnAction(new EventHandler<ActionEvent>() { 50 @Override 51 public void handle(ActionEvent event) { 52 System.out.println("Hello World!"); 53 } 54 }); 55 StackPane root = new StackPane(); 56 root.getChildren().add(btn); 57 fxContainer.setScene(new Scene(root)); 58 } 59 } 60
No comments:
Post a Comment