Java视频播放器代码
package com.example.window;import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import javax.swing.*;
import java.awt.*;
import java.io.File;
public class ManualWindowSwitching extends Application {
private JFrame frame;
private JFXPanel fxPanel;
private MediaPlayer mediaPlayer;
private Button playButton;
@Override
public void start(Stage primaryStage) {
frame = new JFrame("手动窗口切换");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// 设置内容面板背景为黑色
JPanel panel = new JPanel();
panel.setBackground(new java.awt.Color(0, 0, 0));
frame.getContentPane().add(panel, BorderLayout.CENTER);
// 添加 JavaFX 面板
fxPanel = new JFXPanel();
frame.getContentPane().add(fxPanel, BorderLayout.CENTER);
// 初始化 JavaFX 环境,加载媒体播放器
Platform.runLater(() -> initFX(primaryStage));
}
private void initFX(Stage primaryStage) {
// 创建 StackPane 作为场景的根节点
StackPane root = new StackPane();
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, Color.BLACK);
fxPanel.setScene(scene);
// 创建圆形的播放按钮,去除字体
playButton = new Button();
playButton.setStyle("-fx-background-radius: 50%; -fx-background-color: #4CAF50;");
playButton.setPrefSize(50, 50);
playButton.setVisible(false);
// 添加文件选择按钮,并设置中文文本
JButton chooseFileButton = new JButton("选择文件");
chooseFileButton.addActionListener(e -> {
Platform.runLater(() -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("选择媒体文件");
File file = fileChooser.showOpenDialog(null);
if (file != null) {
playMedia(file, primaryStage);
}
});
});
// 将按钮添加到窗口中间(居中位置)
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(chooseFileButton);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// 显示窗口
frame.setVisible(true);
}
private void playMedia(File file, Stage primaryStage) {
// 创建 Media 和 MediaPlayer
Media media = new Media(file.toURI().toString());
mediaPlayer = new MediaPlayer(media);
// 创建 MediaView 并添加到 StackPane 中
MediaView mediaView = new MediaView(mediaPlayer);
StackPane root = (StackPane) fxPanel.getScene().getRoot();
root.getChildren().clear();
root.getChildren().add(mediaView);
// 设置 MediaView 的适应和保持比例
mediaView.fitWidthProperty().bind(root.widthProperty());
mediaView.fitHeightProperty().bind(root.heightProperty());
mediaView.setPreserveRatio(false);
// 创建一个新的 StackPane 来放置播放按钮
StackPane buttonPane = new StackPane();
buttonPane.setAlignment(Pos.CENTER);
buttonPane.getChildren().add(playButton);
root.getChildren().add(buttonPane);
// 播放和暂停按钮
playButton.setOnAction(e -> {
if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
mediaPlayer.pause();
updateButtonToPause();
} else {
mediaPlayer.play();
updateButtonToPlay();
}
playButton.setVisible(false); // 点击按钮后再次隐藏
});
// 添加屏幕点击事件
root.setOnMouseClicked(e -> {
if (e.getClickCount() == 2) {
toggleFullScreen();
} else {
if (playButton.isVisible()) {
playButton.setVisible(false);
} else {
playButton.setVisible(true);
}
}
});
// 添加按钮点击动画效果
playButton.setOnMouseEntered(e -> {
FadeTransition fadeTransition = new FadeTransition(Duration.millis(200), playButton);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.7);
fadeTransition.play();
});
playButton.setOnMouseExited(e -> {
FadeTransition fadeTransition = new FadeTransition(Duration.millis(200), playButton);
fadeTransition.setFromValue(0.7);
fadeTransition.setToValue(1.0);
fadeTransition.play();
});
mediaPlayer.play();
}
private void updateButtonToPlay() {
Polygon triangle = new Polygon();
triangle.getPoints().addAll(
40.0, 25.0,
10.0, 25.0,
25.0, 5.0
);
triangle.setFill(Color.WHITE);
playButton.setGraphic(triangle);
}
private void updateButtonToPause() {
Polygon equalSign = new Polygon();
equalSign.getPoints().addAll(
10.0, 10.0,
40.0, 10.0,
40.0, 40.0,
10.0, 40.0
);
equalSign.setFill(Color.WHITE);
playButton.setGraphic(equalSign);
}
private void toggleFullScreen() {
if (frame.getExtendedState() == JFrame.NORMAL) {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
} else {
frame.setExtendedState(JFrame.NORMAL);
}
}
public static void main(String[] args) {
launch(args);
}
}
页:
[1]