哦! 我正在用javafx创建一个媒体播放器,而setFullScreen方法却遇到了一个烦人的问题:当我双击创建它时,视频并不能覆盖我的所有监视器,但是在屏幕的底部有一个可见的我的场景构建者布局的一部分 look at the bottom part, there is a part of the layout!
这是我的代码:
主要
package sample;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World");
scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent doubleClicked) {
if(doubleClicked.getClickCount() == 2) {
primaryStage.setFullScreen(true);
}
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
控制器
package sample;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.event.ActionEvent;
import javafx.util.Duration;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
public class Controller implements Initializable {
private String filepath;
private MediaPlayer mediaPlayer;
@FXML private MediaView mediaView;
@FXML private Slider slider;
@FXML
private void chooseFile(ActionEvent event){
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a file (mp4)", "*.mp4"); //checking the extension of the file
fileChooser.getExtensionFilters().add(filter);
File file = fileChooser.showOpenDialog(null); //opening the owner's window to choose the file from his pc
filepath = file.toURI().toString(); //taking the filepath
if(filepath != null) {
Media media = new Media(filepath);
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
DoubleProperty width = mediaView.fitWidthProperty();
DoubleProperty height = mediaView.fitHeightProperty();
width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height"));
//Slider
mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
slider.setValue(newValue.toSeconds());
}
});
slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mediaPlayer.seek(Duration.seconds(slider.getValue()));
}
});
//sync slider's length with the video's length
slider.maxProperty().bind(Bindings.createDoubleBinding(
() -> mediaPlayer.getTotalDuration().toSeconds(), mediaPlayer.totalDurationProperty()));
}
}
@FXML
private void play(ActionEvent event){
mediaPlayer.play();
}
@FXML
private void pause(ActionEvent event){
mediaPlayer.pause();
}
//mediaPlayer.setRate(1.5) sets the speed of the video
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<bottom>
<VBox alignment="CENTER" prefHeight="57.0" prefWidth="900.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" prefHeight="58.0" prefWidth="900.0">
<children>
<Button mnemonicParsing="false" onAction="#chooseFile" prefHeight="30.0" prefWidth="30.0" style="-fx-background-radius: 5em;" text="+">
<padding>
<Insets right="1.0" />
</padding>
<HBox.margin>
<Insets left="100.0" right="500.0" />
</HBox.margin>
</Button>
<Button fx:id="playButton" mnemonicParsing="false" onAction="#play" prefHeight="30.0" prefWidth="30.0" style="-fx-background-radius: 5em; -fx-background-image: url('file:C:/Users/Maxy/Desktop/MyPrograms/videoPlayer/src/sample/play.png'); -fx-background-position: center; -fx-background-repeat: no-repeat; -fx-background-size: 20px;">
<padding>
<Insets right="1.0" />
</padding>
</Button>
<Button fx:id="pauseButton" mnemonicParsing="false" onAction="#pause" prefHeight="30.0" prefWidth="30.0" style="-fx-background-radius: 5em;" text="||">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</VBox>
</bottom>
<center>
<StackPane prefHeight="38.0" prefWidth="900.0" BorderPane.alignment="CENTER">
<children>
<MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" />
<Slider fx:id="slider" StackPane.alignment="BOTTOM_CENTER" />
</children>
</StackPane>
</center>
</BorderPane>