我是JavaFX的新手,我需要在按下按钮时播放视频,但由于某些原因我无法理解,只播放声音。这是我的代码:
package trailer;
import javafx.event.ActionEvent;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Trailer extends Application implements EventHandler<ActionEvent>{
Button button;
BackgroundImage image;
MediaPlayer player = new MediaPlayer(new Media(getClass().getResource("videonamehere.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("title content");
button = new Button("button name");
button.setOnAction(this);
Label label = new Label("label content");
label.setTextFill(Color.WHITE);
VBox layout = new VBox();
layout.setPadding(new Insets(200));
layout.setSpacing(20);
layout.getChildren().add(button);
layout.getChildren().add(label);
Scene scene = new Scene(layout, 1920, 1080);
primaryStage.setScene(scene);
scene.getStylesheets().add(Trailer.class.getResource("cssfilename.css").toExternalForm();
primaryStage.show();
}
@Override
public void handle(ActionEvent event){
if(event.getSource() == button){
player.play();
}
}
}
为了澄清,带有按钮,标签和CSS效果的界面确实显示,但是当我按下按钮时,只播放视频中的声音。