|
- 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、本站资源来自互联网用户收集发布,仅供用于学习和交流。
2、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。
3、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决。
4、联系邮箱: [email protected]
5、官方网址:www.panjdzy.com
6、备用网址:www.panjd.top
上一篇:Java网页m3u流媒体播放器代码下一篇:英语练习题
|