我想使用此类来仅使用构造函数来运行媒体文件,而不使用main方法。
(我从GUI运行此Player类)如何在没有主要方法的情况下使用luanch()?
public class Player extends Application {
File file = null; // a file to play.
public Player (File file){
this.file = file;
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws MalformedURLException {
URL url = file.getAbsoluteFile().toURI().toURL(); // URL path of the file.
final Media m = new Media(url.toString());
final MediaPlayer mp = new MediaPlayer(m);
final MediaView mv = new MediaView(mp);
final DoubleProperty width = mv.fitWidthProperty();
final DoubleProperty height = mv.fitHeightProperty();
width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
mv.setPreserveRatio(true);
StackPane root = new StackPane();
root.getChildren().add(mv);
final Scene scene = new Scene(root, 960, 540);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
mp.play();
}
}
最佳答案
您可以通过调用扩展Application的类的静态方法来实现。因此,调用Player.launch(Player.class)
应该可以。还要注意,它不能被多次调用,否则将引发异常。
关于java - JavaFX从构造函数运行。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34809267/